LPI 101-500 – 105.2: Adapt or write simple scripts Part 1
July 18, 2023

1. Chained commands

This chapter 105 two has the heading Adept or write simple scripts. That sounds a lot worse than it is. Because the nice thing about Bash programming is that you can use any Linux command here. So you can use all commands that you have have learned in this course so far, and that you will still learn. So you can see that you already know a lot of commands. Now the only thing left to do is to meaningfully combine the corresponding commands. Unfortunately, we cannot go through a complete Bash scripting from A to Z in that course, because then this course will probably take 10 hours longer.

In my opinion, after this chapter, you will not be able to master Besh scripting either. It’s just about teaching a few basics. And of course, that you know what the commands the IPI ask for in the exam. Let us first take a look at the Socalled chained commands. For this example, I will briefly install the Nginx web server with pseudo apt install Nginx. Usually nginx starts by itself. After the installation, I will check that briefly with systemctl status Nginx. And here we have the confirmation. Nginx is active. Now I want to stop ngenix with Pseudo system ctl. Stop nginx? Ngenx should be stopped now, but we are not receiving any confirmation.

We could now query the status again with System Ctl status, but we can also use echo dollar question mark. This command shows us the status of our last command. Our last command was systemctop Nginx and the status is zero. Zero means that the last command was successful. One means that the last command was not successful, so the Nginx should be stopped. We will check it again with Systems Ctl status and Genix. And here we have the confirmation that Nginx has stopped. We can now write two commands in one line, one after the other within Bash. To do this, one command must be separated from the other.

With a semicolon, both commands are executed one after the other. Let’s try it by turning the Nginx back on pseudosystem Ctl dark nginx semicolon echo dollar question mark adenix will start and we will immediately receive confirmation that the start was successful with the zero for repeat. A zero means that the last command, this one was successful. We can connect, let’s say connect two or later, more commands in other ways. The semicolon says that we simply string two commands together, both of which are executed definitely and always. But it is, for example, also possible to say only execute command two if command one was successful.

Instead of a semicolon, you simply use two consecutive ampersands. As an example, let’s create a directory and then output a success text. So, for example, mkdir test directory and echo. The creation of the directory was successful. As a result, we immediately receive the text that we have specified. Since this is only output if the directory was actually created, we can assume that this worked, but we are happy to check it again and the directory has been created. Let’s do the counter test. We take the same command, but try to create the directory in route, which will not work because we as a normal user do not have permissions for that.

And then test directory. So in root test directory and then echo. The creation of the directory was successful. As expected. We get the message that we are not authorized to do so. Our set rate is not displayed to us at this time. We can still rewrite this command a little. Reprovously programmed a success message. So if it works, write out a text that says Hooray. It worked. Let’s do it the other way around. We want to output a test a text if the first command does not work. To do this, instead of two consecutive ampersands, we use two consecutive types, which then say that the second command is only executed if the first command fails.

MK deer test directory echo this did not work. And this time our text is output. Because of course it did not work this time either. We will do the test again. What happens when we create the directory in our home directory again? For example, let’s see how it works. To do this, I quickly delete the test directory beforehand. Delete it now. And now mkdir test directory pipe pipe echo this did not work. And here too, what we expected. The directory is created, but our error message does not appear. We check if the directory really was created and we see it here. Test directory.

2. Variables

Let’s talk briefly about variables. Again, a variable is a container that can contain a certain value. A variable can contain a number, a letter or a sequence of letters. So whole words or sentences. The nice thing about Bash program is that we don’t have to tell besch whether it’s a variable that contains a number or a string. If you’ve ever had to do with programming, you probably know it. There are, for example, so called integer variables, which can then only certain numbers. Then there are string variables that can contain text and so on. We don’t have to specify that when programming Bash, we just assign our value to a variable.

So we can say for example, price equals 25. In this case, price is the variable name which we can almost freely choose. So here we create a variable called price and assign the value 25 to this variable. If we now want to output what the value of the variable is, we select the echo command followed by a dollar sign and the variable name echo dollar price. The dollar sign means that the content of the variable is output to us. If we forget the dollar sign, the result is different. So echo price and the output is just price. As I just said, we can also specify letters in the variable value. For example, letters equals ABC.

Here too, we have freely chosen the variable with the name letters and assigned the content ABC. As before, we can use echo to output the content of the variable echo dollar letters ABC. In addition, the variable names do not have to be written in capital letters. Not everyone does that either, in my opinion. However, it is then a little easier to immediately see in complex programs that this is a variable. But we can, for example, also do something like this here hello world. So we created a variable called hello. This time in lower case, we have assigned the text hello world to the variable hello echo hello. And here we have hello world.

It is important that the text that we assign to a variable is enclosed in single quotation marks, as shown here. This is not necessary for individual words or letters, but as soon as there is space in the text, the single quotation marks must be used. Otherwise it will not work. We can try this hello world and we get an error message. By the way, you should also make sure that no space is inserted before or after the equal sign. Even then it won’t work. For example, price 25. So this is not working. If we want to output variables, there are a few more things to keep in mind.

We learned that we can put whole sentences in both single quotes and in normal quotes. However, this is only partially correct and depends on the application. Suppose we create another variable with the value 25 and want to output the value of this variable, but this time including a sentence. For example price equals 25 and then echo this philly stick cost dollar price today we see that the single quote does not work if we want to put out the contents of a variable at the same time. Here we now have two options. Either we close the single quotation marks in front of the variable, or we use normal quotation marks.

So echo this Philly stick cross today and then we are closing the quotation mark. And then dollar price. So that is working fine. And the other way echo quotation mark is filly stake costs today donna price quotation mark. Now what if we wanted to quote the price in US dollars and we would also like to use the dollar symbol. We have seen that the dollar sign is used to output the content of a variable. How would that work? Now let’s use the following example price equals 25 and then echo price for one kilograms. Filling stake today is dollar US dollar price. And you can see US dollar is ignored here, or a gap is left.

Bash interprets the command in such a way that the content of the US variable is to be output. But there is no such thing, so no US variable in the system. So we have to tell Bash that it should also output the dollar sign as a dollar sign and not interpret it. This works by putting a backslash in front of the dollar sign. A backslash tells the interpreter that the next character should not be interpreted, but simply output. So we try it again with echo today the price for one kilo filly stick is backslash US dollar price. So just to repeat that the backslash means that the next sign so the dollar sign is not interpreted by Bash and the besh should only output the dollar sign, not interpret it.

So now it should work. And yes, it is working fine. Now one more interesting thing I wanted to show you is this text equals hello world. I used several spaces here, and now I output the content of the variable with echo dollar text. And we see that the interpreter has removed the supposedly too much set spaces. If these are two remain, we have to use the quotation marks again. So with echo dollar text now it’s working. You can also put a variable in curly brackets in an output. This is to prevent mix ups. In that case the result remains the same. Echo the price for 1 kg finish stake today is dollarus dollar rice.

What is also still possible is that we use the result of a command in the programming and then process them further or simply output them. We will try it as follows clear the screen first. Echo. Here is a list of files and directories Lsla. As expected, it doesn’t work that way. It just prints Lsla. So we have to tell the Bash again that a command should be executed here and not just the text output. All we have to do is put the command in brackets and put a dollar sign in front of it. So again, echo. Here is a list of files and directories dollar bracket, lsla and bracket and quotation mark. And here our text and the result of the command lsminus la are printed out.

3. if, then, elif, else, read

As a rule, bash scripts are of course much more extensive. Accordingly, it makes sense to save the commands and a file in order to execute them later. At this point, we will simply create a very small edition program that is not really user friendly either. But that is not the point here. So I use vi to create a file called with a hash mark. We can leave comments that will not be taken into account later when the script is executed. In this case, maybe this is a small edition program. When it comes to bash scripts and files, it is important that we also inform the system that this is a bash script. Otherwise it would be a normal text file. We do this by putting the so called shaping line at the beginning.

It looks like this. As I just said, the hash symbol indicates that the following is a comment in combination with the call sign. However, it means that this is an executable file. Then the path to the so called interpreter follows. Usually this is bin bash, but it can also be binsh or something completely different if you are using a special programming language. But that’s not the issue here. Let me first save that and get back to the file, because now we have some color changes here. Okay, so let’s try with our addition program. The Let command can output arithmetic expressions from variables. When the program is called, we have to give the program two numbers.

The first number is added to the variable one and the second number is added to the variable two. The two numbers are added and assigned to the new variable result, and the result is then output in the next line. So it is a little edition program. Okay, so first, save the file and exit vi. How can we now run the script? First of all, we need to make it executable. The file currently has a ch mod value of six six four. So let’s make it seven six four because we need the execution right here. So chmod seven six four addition now, we can already see from the color that this is now an executable file. Now we can call the program, for example, as follows addition to three.

So we start the script edition here and at the same time pass the numbers two and three which are used for the addition. The result is five, so the script works. We could also start the script like this with bash and then addition 23 or just the dot here and then addition 54, the result of nine. So it works too. By the way, if we omitted Let, then the script would not return the result of the sum, but only show what can be found in the individual variables. The results would then read here. The result is two plus three, but you are welcome to try that out yourself. However, we can also create such an addition script withoutlet for this we use the so called arithmetic expression.

I am creating a new file here called addition two and setting the two numbers as variables directly. So first the shipping line. Please keep that in mind for the exam. Another comment. This is second edition program. First, save it back to the program. And then for example a equals two and B equals twelve. C equals thank you. So that is the addition and I think that would carry out correctly. It is not enough to just write dollar A plus dollar B, but the addition must be enclosed in double brackets. And another dollar sign here. Just simply keep that in mind. Then I think the addition should work. So A plus B 14. The result should be 14. Let’s try it out. First we have to make it executable.

The sum of A and B is 40. Of course we can make a subtraction problem out of it in the same way. And multiplication problem, no matter. But let’s stick with the addition for now. How can we make our program better? For example, when the program asks two numbers from us and then adds them up. So far the numbers are either already given or we pass them during the program call. This is where read comes into play. First we create a query and ensure that the user’s input is then saved in a variable. So we create a new file here with the name edition three. First of all, how long she’s shabaneline and our comment another addition program.

Save it back to it. Please enter the first number. That’s the question that the program will ask us. Then read a that means when we will enter a number, for example five, then the five will automatically put into the variable A. Now we ask for the second number so that’s the same. When we for example enter number six. Then the six is automatically put into the variable B. Then echo. Now comes our calculation. The result is and here we have to use our calculation and it is plus dollar B. So I think that’s it. Let’s save it and let’s make the program executable. Now let’s try it out. Addition three, please enter the first number maybe 23. Please enter the second number maybe 60.

And you can see we have an error here. Addition Three line ten unexpected end of file. While looking for matching, we get a hint here. What is wrong? Unexpected no fire. So let’s look again at this fire. And you can see here. I have forgotten the quotation mark here. Here is the first, but I’ve forgotten the second. So I enter it here. And now it will work. So again 23 for example. And here number 60. And the result is 83. So it’s the correct result. The calculation is correct. So it’s still not great and also not visually appealing. But we wrote a little edition program that works we just do the whole thing with a multiplication.

To do this I simply copy the existing edition three program and name it. For example multi one. So CP addition three to multi one. And now let’s edit the multi one file. And here we only have to change the calculation. So instead of plus here, we use the star here for multiplication. So it’s already executable. Let’s try it out. Sorry. Multi one, enter the first number four, second number ten. The result is 40. So the result is right. We saw that we can use our program for all four basic aromatic operations. It is best to design our program in such a way that we are asked beforehand whether we want to add or subtract.

So we are going to create a new file called Calculator. First the shipping, then comment clear is not necessary. Clear just clears the screen beforehand, so it looks a little bit nicer. Okay, echo with the option E. What basic arithmetic would you like to perform? The E option of echo is used so that the backslash N is interpreted and not just written down. Backslash N means new line. Let’s first save and open it again. It looks better the color. And we get some ins when something is missing. For example, like in the addition three program where we had a missing quotation mark. Okay, now we can create a kind of menu with the help of echo.

It could look like this for example echoey and then for example M for multi application and the D for division. So the user must choose between asm or D. We have to save this selection in a variable. So we use read again. And for example we name the variable Calc for calculation. Now we ask the user to enter two numbers. You have already seen that. What is the first number? And then again read. And in this case I use the variable name x and not A. Because it is possible that you will get a little bit confused when I use a capital A here and you have a capital A here in the addition. So just to separate it better, I use the x and then echo. What is the second number? Read.

Why? Depending on what the user is selected at the beginning, the calculation now looks different in principle. It says if the user has chosen for example A for addition, then we will add these two numbers. If the user has chosen S for subtraction, then we subtract these two numbers and so on. So now let’s use if and then for our programming if and then dollar kelk equals x A. Sorry. So if someone presses then and now our calculation takes place c equals and then x dollar x plus dollar. So if the variable Calc contains the value A, which we have queried with read here. So A is for addition, we have query it with read. So the A is stored in the calc variable now. So if the calc variable equals A, then do this calculation.

It is c equals X plus Y. And here is the result echo. The result of the addition is C. Note that with if we have a square bracket here followed by a semicolon. And then at the end the if is closed with an Fi. Fi is the opposite of if. And if you forget the Fi, the script will not work and you will get an error message. Now we have to program the other basic arithmetic operations. theoretically, you could now do three more if blocks, just like now. But in practice you use else if or L if for short. Else if could be translated as otherwise. We said before, if someone presses a use do addition, else if someone presses S use subtraction. And now we have to program that.

We add this block before the Fi and it looks exactly like the event statement dollar calc equals S semicolon then and our calculation follows. Okay, that’s it. Let’s try it out. Save it here. And let’s make the file file executable and then calculator. And you can see we have our wonderful menu here now. And I use A for addition. What is the first number? It is for example, five. And what is the second number? It is ten. The result of the addition is 15. So it’s correct. Let’s try out the subtraction S. The first number is 20, the second number is six. And the result of this subtraction is 14. So it works. Now we can add multiplication and division in the same way.

Let me just copy copy and paste it here. So now we use the M for multiplication. And then we have to just change the calculation here. And here we have to use the D for division. Okay, that’s it. So let’s try the other ones. I use M for multiplication. First number is 7th, 2nd number is ten. For example, the result is 70. It’s correct. And let’s try the division. First number is ten, second number is five. The result of the division is two. Great. everything’s correct. And at this point we should still program what happens when someone presses another key. So if I would press an L now, for example, then we have no result, because the program does not know what it has to do with the L.

So we have to program this. And here else comes into play. This time only else else echo you pressed the wrong key. So if you press A, then this calculation is done. If you press S this calculation and so on. And if you press any other key on the keyboard, then the program will do this here. Echo you press the wrong key. Let’s try out I use the L again, you press the wrong key. It’s not a solution for our problem, but it is a hint for a user that he has pressed the wrong key. Okay, that’s it for this lesson. In the next lesson, we will just edit our calculator program and use case instead of if then and then we can see how it works.

Leave a Reply

How It Works

img
Step 1. Choose Exam
on ExamLabs
Download IT Exams Questions & Answers
img
Step 2. Open Exam with
Avanset Exam Simulator
Press here to download VCE Exam Simulator that simulates real exam environment
img
Step 3. Study
& Pass
IT Exams Anywhere, Anytime!