LPI 102-400 – 105.2 Customize or write shell scripts

  1. Objectives

Hello and welcome, students. So we are still in the shell scripting section, which is section 105. Today we’ll begin the subsection “customize” or “write shell script.” all script. Hour years. t is four. The key knowledge areas that to be going to be covered in this sari section use how to use the Shot and add Shy syntax, including loops and tests Use command substitution, test return values for success or failure, and other information provided by a command; execute change commands; perform conditional mailing to the super user; correctly select the script interpreter through the shebang line; and manage the location, ownership, execution, and SUID rights of a script.

And then we have a list of the used files, terms, and utilities. So far, “while test if read” has been one of the terms, followed by “sequence” or “SCQ exact test,” and then “double pipes” and “double impersonation.”

  1. Hello World

Hello students. In this lecture, we are going to start creating our first script. And, as you might expect, first script is always a hello world program, regardless of whether you’re learning a programming language or a scripting language. It’s just like an unwritten rule that pretty much explains things and lays the foundation for what’s to come. So let’s start. We’re going to a VI file called “scripts” or “script one.” Our first line will contain a hash exclamation bin bash. This is referred to as the command interpreter. It interprets what is going to be used as a language for our script. In this case, we are using bash, but it doesn’t have to be bash. We could have used just regular shell and shy, or you could have used CSH. If it had been a Perl script, we could have used user bin Perl. So depending on the kind of script that you’re using, that’s what the interpreter tells the script: that it will be using such and such a language, and the binary resides in such and such a path.

So in our case, it’s Bin Bash. Any other time a line is going to be followed by a hash sign, that will be considered a comment. and it’s not executable. That’s where you put comments about your script. This is the only time it is used with an exclamation. It’s called a shebang. This whole line, or these two, are called a shebang. This is the only time when it’s not used as a comment. Okay, we’re going to make our script very simple, and we’re going to say “echo.” That means whatever we’re going to say is just simply going to print out on the screen, and what we’re going to say, we’re going to put it in double codes, and we’re going to just simply say, “Hello, world!” and the code. Okay. And we’re going to remove X from the equation.

So that’s another way of saving and exiting the program. We are used to WQ bank from our previous lectures, but you can do a colon x enter, and that will save and quit. Also, just to make sure that our programme is safe, I’m going to look into it, and yeah, it is saved. So now, if we were to run this programme, it’s going to say permission denied. because this programme is not executable yet. In order to make it executable, you have to run a command called chimod, or change mode. After that, you can perform a U plus X. That means the user will have execute access, or you can simply do x plus 1. That means user groups and others. Everybody will have executable access, or it will assign executable to everybody who’s going to be running this script. So, just to make things simple, we’ll give it x and then script one. And now, when we run the program, it runs “Hello, world.” Now let’s look at our path.

Our current path is this. And what I want to do is export; I want to create a directory in my home directory called, let’s say, “make directory bin” for binaries. And what I’m going to do is copy the script that we have created to hat we has then I’ll go ahead and remove it from here. It has been removed, and the CD has been discarded. It has also been moved to the bin subdirectory. And what I’m going to do now is export my path. The export path equals the dollar sign path. The colon acts as a path delimiter, followed by the dollar sign at home. So as you can see now, I am in my home directory. And my script doesn’t even reside here. It’s under the bin. Okay? But I will be able to run it from my home e bin. Okay? Because homeschooling is now on my agenda. And that’s why that path can be detected from anywhere as long as I’m logged in as this user, which is a student. We’re going to continue our discussion of the script in the next lecture.

  1. Using input parameters

Okay, so far we have created one script, and let’s go back into it. Remember, I created a bin directory and placed it there. So one sh is bin scripted. So, aside from displaying that one line, “hello, world,” we’re not doing much right now. So we’re going to try to make it a little bit more interesting. I’m going to add one line here and create another line that will say “Echo e is included here” because I’m going to add an escape character on this line. and I’ll show you what that means. Enter your name, and then put a few dots here. This is my escape character.

End of code. and the line is continuing. So don’t put a blank line after that read. And I’m going to refer to a variable as S. Okay? In my hello world, I’ll also replace the word world with CW, which is the change word. And I’m going to put my variable here as “name,” and I’m going to call them X. And now when you’re going to run the script, which is a script, it asks for your name. And suppose if I put in “student,” or let’s give it a real name, say Jim. It says, “Hello, Jim.” That name variable that we created has been replaced by our entry on the command line or our entry on the screen, which is Jim. So now our script is not only echoing what we are saying, but it is also asking for input from the keyboard. And we just provided it with that.

  1. seq command

Okay, in this lecture, we’re going to talk about a very important command, which is the sequence command, and how to use it in a shell script. So if I were to type just “sequence five,” it’d give me five sequential numbers. If I typed “sequence 20,” I’d get 20 different numbers; if I typed “sequence 2020,” I’d get 20 different numbers. And how we intend to employ it in a shell. Let me show you how.

And instead of doing a VI this time, what I’m going to do is run it from the command line. So let me become a super user first. And I’d like to become a super user because I’ll be using the user ad command. You cannot run a user ad with a regular or standard user. You have to be a super user, or you should be using pseudo, in order to use the user at command. So our script starts like this for you, in dollar signs and parentheses sequence. Assume I want to create nine users, semi Colombo, so do something for this task. And what are we going to do? User add.

We’re going to use the user add command, and then we’ll say user dollar u. So we’re going to create sequentially nine users, and in the next line, we’re going to say “echo user-dollar sign” in parentheses, or, sorry, in curly brackets. We’ll start with a U and then a colon. So that way, we can separate the username from the password. And our password is going to be just one simple word password one. That’s what we’re going to call it, okay? And then we’re going to use the command “ch password.” This straight line is called a pipe. So we are piping whatever information we have here to Password C. And the description we’ll use is SHA 512. And this is our script.

And then we are done. And we’ll check to see if these users have been created. I’m going to do a tail on the last ten lines of etcshadow. Remember, the Etsy password file has a shadow file as well, which has the encrypted passwords in it. And if you look at the users here, we have user nine, user eight, user 765432, and user one. So all of our nine users are created in one shot with, like, three different lines. And that’s it. That could be our script. Actually, we ran it from the command line, but it could very well be put into a script, and you can run it from there. So this is the use of a sequence command.

  1. for loop

Okay, we’re going to create a new script this time, and we are going to call it script two. So, in our bin directory, Scripttwo.sh, we’ll use the If Then Else loop to test a situation to see if it’s true. If not, then we’re going to use the else, and then we’re going to end our script.

So as usual, we’re going to start with the Shebang Bin Bash. The next line will be if and this is conditioned equals D, which we will test for and close the brackets, then find etc. So we are using the find command of Linuxmaxdep because we don’t want to go under, etc. We just want to look in, etc. We don’t want to go to any of the subdirectories of type D. So anything where we’re going to find a directory, d is for directory in, etc., and otherwise we’re not testing anything in this condition. If you can’t find it in the previous statement, then do this. So there is no test involved here. mex depth one type L and L are for the SIM link or the soft link, and we’re going to end our loop. Okay.

Now we’re going to make our script executable. So chimed bin plus x bin script two script, script two shy, and we’re going to give it an argument of D, which is part of our script, and it shows you all the directories that are under, etc. So this was our first test, if you remember from the script for the for loop, and this is being tested. And now, if I were to give it no argument, then we’ll go to the other statement, which was to show the link or the soft links. And these are the soft links. If you recall, there was an option for L in our script that said show the soft link if there were no directories. So this is how you run the script, and if we were to confirm that there is a soft link, we can do an LSL and etc. in local time. It’s one of the directories, and this is just to confirm that you do see an L here. So this is a soft link.

  1. while loop

Hello students. We are going to learn about while loops in this lecture, just like we did about loops, which we use for testing a certain condition. We’re going to do the same thing with the while loop this time. So I’m in my home directory, and I’m going to VIPT three, which is located in bind rectory, so binscript three sh. And we’re going to start off with the shabash. And then what I’m going to do is introduce a variable called count. I’m going to give it a score of ten. This is prior to starting the while loop. Then you’re going to start our while loop. And in the while loop, we’re going to assign two parentheses at the beginning and then count. As long as the count is greater than zero, we’re going to close these parentheses. We’re going to do some stuff, and then we’re going to be done. Remember, we started off with “if” and then “fi” at the end. So do and done are the closing arguments for the while loop, starting and closing.

And then in between, we’re going to say “echo” and “escape character,” and then “dollar sign count C,” which is going to introduce an escape character. And then we’ll take a very slight pause, giving it a sleep 1 value. That means our answer, whatever it is, will be delayed by 1 second. Of course, depending on your script, you can change that value from sleep one to whatever you want. And then we’re going to have to do something to the count variable; otherwise, it’s going to keep going indefinitely. So we have to add some kind of increment, decrement, or something to the count. So I’ll go with count. Leave a little bit of space in there. R less minus This is like saying “count minus one.” I could have written count minus one, but instead I’m using count minus minus.

And then we have our done statement and delete all the white space. And then I’ll go to the next and echo; let’s give it a couple of blank lines and then say “V success” at the end. Okay, close the and then sand now we’re going to do our chimed to make the shell executable with a bin script ending in three dots (.sh). And then we’re going to run script three sh.

And here you can see the output, and as you can see, it is delayed by a second, so it’s not coming right away. Then it says, after all, success. Actually, I should take out those slash ends because they’re within the double coats. That’s why they’re getting printed there. I’m not going back into the script, script three sh. And the mistake I made here is that there should have been no space here. And also, in order to have the escape character, I have to have her no space hand then we’re going to run it. And let’s see if our output this time is going to look slightly different from the last one. So the countdown is still going on. And this time, as you can see, the slash ends didn’t actually print because I had a space between the two at the slash ends, which is also called the carriage return. Just kidding. Enter, enter. Because of these three ends, you see these three spaces in between. So you double-check that there is no space between the two inside the shell. And this is how it works. So now we have learned how the while loop works in shell.

  1. mail alerts

Okay, we are getting ready to start on this script now. And this time we are going to create a script that sends an email or mail to the root user, alerting them about something. So let’s check this out. Script four sentences. Start with the shebang bin bash, not the hash bin not the hand we’re going to create a variable called DF, which stands for disc free or disc space. And what we’re going to do is create a file DF text in my home directory. How are we going to create it? I’ll show you later in the script. If we meet our condition of “escape character dollar DF,” the file exists. And then, if it doesn’t exist, we’re going to give it an exit code, which is to let us know there’s an error and that the file doesn’t exist. Normally, if you don’t specify exit 2, the default is exit 0.

That’s without the error message. And then we’re going to end it with Fi. And then after this, we’re going to give it the actual Linux command that’s going to provide us this output, which is DFH, and we’re going to give it a capital T, and we get to redirect it to our variable DF and mail it to S. subject is going to be a disc-free dollar sign. And then we’re going to put the date here. So date. And we’re going to format the date with a percent, which is going to put it in year first, in that format, and the codes in bin script four. So, although we didn’t echo anything to the screen, we are sending out a message to the super user. So let’s become super users, clear it, and do mail. And, as you can see, our super user has received the disk free message, which has already been received. And actually, this is the whole message that gives you the exact information about the disk. So this is the whole message.

Okay, so now we’re going to look at that exit code that I mentioned earlier, which is inside our script. And as you remember from the last statement of the script, we actually removed the text file that we created. Our script was executed exactly as before. However, because the final line of that removes the script, if we do echo dollar sign, question mark, we will get an exit code of 0, which is the default exit code, which means there is no error and no message. However, let’s create the file. This time, I’m going to create the file Daft TXT by hand in my home directory. And then we’re going to run script four shad it says the file exists. And now if I run echo dollar sign, question mark, my error code is our exit code two this time because the file does exist. So that’s the difference. So while you’re doing your scripting or programming, it’s a very good idea to have an exit code, so that way you will actually see the message that the system puts out if there is an error or if there’s a condition that you’re checking. And finally, a little bit about the process called Exec or. It doesn’t create a new process. It replaces the current process with the new command.

If you do this on the command line, then it’ll effectively end your shell session and maybe log you out or close the window. So what I’m going to do is, while I don’t have KSH installed, I’ll just show you what the exact command does. So if I were to run Exec and, let’s say, BinEcho, and then, hello, as you can see, my shell script is gone, the terminal window is on, but you lose the session. You lose the terminal session, and in some circumstances, in a shell script, that might be something that you want to do. Once you have done this, you want the script to be removed, and that’s why Exact is used. Or you want the windows to be closed, and that’s something you want in a script, and that’s why it’s used.

img