4. case
We can also program this calculation program with other commands instead of if else if and else we could also use the key, the case command here. So let’s edit the script again. And we can leave the upper part of the script like this. We remove everything from the first if here. Okay, let’s go. So case works in the same way as if, but in different notation that you simply have to remember. So in this case case and then the dollar calc in and here’s A and then the calculation C equals and you know the calculation. We just have to remember how the case statement is written. The calculation and the output here look exactly like the if statement.
And as with if, we also need to complete the case statement with if. This was possible with a command fi with case it is the esac command. So again, just written backwards. So here, just remember it and just try it out. Just practice it. Case calc so this won’t kelk in A and then the calculation and the output. Okay, let’s try it out. I use the A here. First number is six, second number is nine. The result is 15. So it is working. So let’s add the other basic arithmetic operations here. You don’t need to use this line here. Again, only these three lines here are important now. So I will copy these lines and paste it here three. And now I have to change it a little bit.
So we need S for subtraction and we change the calculation. And we need the M for multiplication. And we need the D for division. That’s it. We will try it again as 41. And you see we have an error here. calculator line 24 syntax error near unexpected token. This is because the interpreter does not know exactly when a case statement is completed. We therefore need to add two semicolons to the end of each case statement. And after that it will work. So here two semicolons. And here two semicolons. And here two semicolons. And here two semicolons. Let’s try it again. I use dm for multiplication. First number six, second number is eight.
The result is 48. Now it works as expected. Now all we need is the counterpart to elves from the if statement. We remember with else we specified what happens when the user presses a key that is actually not available for selection. Of course we can do that with case as well. Here we just use an asterisk like that and then echo. You press the wrong key. Let’s try it out. calculator and now I press, for example a B. You pressed the wrong key. Now we have exactly the same function as before. We’re the F statement, only in a different notation with a case statement.
5. while, do, done
We come to the so called loops. In a loop you can set up a condition and tell bash that something should be carried out as long or as often as a certain condition is met. Let’s write a small sample script that counts from zero to 20. We name the script count first, of course, we need the shipping line. And first we make sure that the value of the variable number is set to zero at the beginning of the program run. Now the while loop follows. Let me just write that down and then I will explain it. Thank you.
Okay, what does this loop say? At this point I would translate while with as long as so as long as the content of the variable number and before the first run, or in the first run, the content is zero is less or equal than 20. Do something. What you should do, or what the program the script should do, stands between do and done. So first, the value of the variable number is output. The value is zero, as we determined ourselves. So a zero is output. This is followed by a calculation that says that one should be added to the content of the variable number. So the content of the variable number is zero. So zero plus one is one.
So the new value of the variable number is no longer zero, but one. After the first run of the program, the while loop is now run through again, because the condition is still intact, because number so in the second loop, number is one is still less or equal than 20. So at the second run we have here number is one plus one is two. So the new number is two. Then we have the third loop. Then we have here number two is less or equal 20. And then we have two plus one here is three, and so on. If the value number is then 21, then the condition of the while loop is no longer fulfilled, because 21 is not less or not equal than 20, and thus it is no longer executed.
Let’s try the hot and let me make this file executable. By the way, you do not have to make the file executable. You can just use bash and then it will work too. So bash count and you see here from zero to 20. Okay, let’s replace the expression le with lt. What happens? Then we see that it only counts from zero to 19 here, not to 20 anymore. lt means less than. So previously it was called less than or equal to 20. Now only less than 20. So the condition of the loop would no longer be given if the value number is 20. You can find more so called comparison operators in the pdf file below this video.
6. for, seq
We can also program the whole thing in a different way here, namely with a for loop. Let’s rewrite the program accordingly count and then let’s delete these lines here and I will just write that down and I will explain it in a moment. In the brackets here, we basically find three instructions. The first statement specifies that the start value of the variable I is zero. Then it is asked whether I is smaller or less than 21, since this is the case with zero. So the zero is output here and after that a one is edit. The two plus finds just mean that a one is edit each time it is looped. And then after the first loop we have the second loop. Then I is one, then I is two and so on.
So let’s just look at the result and we see as a result that the bash script counts from zero to 20. Let’s look at another possibility with four. We could, for example, do it like that one to 20 in the curly brackets means or one dot dot 20 in the curly bracket means 1220. You can understand this for loop like this the first number in the curly brackets is one.The one is assigned to the variable I and then output with an echo instruction here. Then the for loop is running again. The next number would be the two. The two is assigned to variable I and then output. And this continues until 20. Let’s check the result again and you see that worked.
And you see, at this time we do not start with zero, but with one. incidentally, such a sequence of numbers can also be output in bash without further programming, namely with a SEC command. The SEC command outputs a sequence of numbers. So SEC 20, for example, and I get the numbers one to 20. And in this case, however, it starts at one and not at zero. You can also pass several numbers, for example, SEC ten and 20. And this command results in the counting from ten to 20. Here another example. This sequence means that the count is from ten to 100, but in steps of 510, 15, 2025, 30 and so on. Please keep that in mind for the exam.
7. test, exec
In bench scripting there is also the test command. Let’s take a look at the man page. Test check file types and compare values. So test can therefore check file types and compare values with one another. Here I would recommend working through the man page. There are really many ways in which tests can be used. Here with the option E, we can for example check whether a file is available. For example, we can see whether the count file that we created in the last two videos is available with test ecount the echo dollar question mark. This is important because when we don’t write that down, we cannot see any result.
So we have to use echo dollar question mark and we get a zero output, which as we know means that this command here was successful. So the file is available. We do the cross check with a nonexistent file. For example, test e count two this file does not exist. It could have a question mark, and here the result is one. So the file does not exist. Another example test u count echo dollar question mark the U option queries whether the files suid bit is set. So in this case it is not because we have one. So this is not okay, not given.
Another example test r account echo question mark the R option asks whether the file is readable and we have zero year, so this file is readable with further options. You could for example, also test whether a file is a directory, whether it is a symbolic link, whether the sued bit of a file is set, what kind of file it is, and so on and so on. And as I said, I recommend that you familiarize yourself with the individual options here. The last command is the exec command exec written. The exec command can be used within a script to integrate or use an external program or another script in the script.