We talked about logical operators (&& (and) and || (or)) and got to understand how they work. You can also use them in tests.
If you use &&, then if the first test is not successful, its exit code is used for the if statement and if the first test is successful, then the exit code of the second test is used for the if statement. If you use ||, if the first test is not successful, the second test’s exit code is used as the exit code for the if statement and if the first test is successful, the first test’s exit code is used as the exit code for the if statement. (Ward, 2014)
For example, you can write:
if [ “$1” = ‘Hi’ ] || [ “$1” = ‘Hello’ ]
if you were to test if the first argument is either “Hi” or “Hello”.
Think of it like this: && says “Both of the conditions have to be true” while || says “Only one of the conditions must be true”. This holds because only the exit value of 0 is used to indicate success. Re-read the second paragraph and make sure you understand this.
You can combine more than just two conditions with logical operators.
Hope you learned something useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 258
Let’s talk about logical operators outside of the context of shell scripts, at first.
If I run two commands like this:
touch someFile && emacs someFile
what would happen is that I would create a file named someFile and then I would immediately open it with Emacs (if you don’t have Emacs, you can use less). However, try this:
mislav@mislavovo-racunalo:~/Linux_folder$ ls -l nonExistentFile && emacs someFile
ls: cannot access 'nonExistentFile': No such file or directory
What happened here is that we tried to use ls on a non existent file and we got an error message. But our Emacs didn’t open up as well. What is happening here?
See, when you use the && operator (also known as the “and” operator) you are saying “Execute the first command and then, if it succeeds, execute the second command”. How does the shell know if a command executed successfully? Why, by exit code, of course! (Ward, 2014)
When a command is successful, its exit code is 0. && says “I will execute the second command only if the exit code of the first command (to the left of the && sign) is 0”. ||, another logical operator, says just the opposite: “I will execute the second command only if the exit code of the first command (to the left of the || sign) is not 0”. You can remember it like this, but this makes more sense if you know about logical gates, which you don’t need to to understand Linux, so I won’t explain those.
So if I write:
mislav@mislavovo-racunalo:~/Linux_folder$ ls -l nonExistentFile || emacs someFile
this actually opens up Emacs, because || works in the way I described above.
Hope you learned something useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 258
You can use elif to test for multiple conditions. But there is also another construct, called case, which is more appropriate than just a bunch of elifs. We will talk about case later on.
Hope you learned something new!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 258
The else statement in combination with the if statement is used to tell the following: “If this condition is true do this, if this condition is not true, do that”. That’s pretty much the gist of it.
Below is the entire shell script (with the else statement), modeled after (Ward, 2014):
#!/bin/bash
if [ “$1” = ‘Hello’ ]
then
echo 'Hello back to you!'
else
echo 'You are rude.'
fi
And let’s run this script with the first argument being Hello and then it being something different:
What is happening here? We are testing if the first argument to our script is Hello. If it is, we echo Hello back to you! and if it is not, we echo You are rude.
What happens is the following:
The test in the if condition is tested – that is, we check if $1 really equals Hello.
If the test from step 1 is true (meaning that the exit code is 0), we execute the code starting at then until else – that is, we echo “Hello back to you!”
If the test from step 1 is false (meaning that the exit code is not 0), we execute the code starting at else until fi – that is, we echo “You are rude.”
This process above follows the logic of: “If the condition is true, then this, if not (else), then the other thing”.
Hope you learned something useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 256-257
The if statement allows us to test for a condition and execute a certain set of commands based on the result of that test. (“If Statements!,” n.d.)
An example script:
#!/bin/bash
if [ "$1" = 'Hello' ]
then
echo 'Hello back to you!'
fi
What is happening here? We are testing if the first argument to our script is Hello. If it is, we echo Hello back to you! and if it is not, we don’t do anything.
More technically, the [ is a command that performs tests for Unix conditionals. (Ward, 2014) The if, then and fi are shell keywords and everything else is a command. So what happens is that we check if the first argument to our shell script is indeed equal to Hello. If it is, then we echo Hello back to you!. If it is not, we don’t echo out anything.
A detail – the [ command returns an exit code. If the exit code is 0 (as we learned) that means that the check went well and we go on to echo Hello back to you! and if the exit code is non-zero we know that something went wrong and we don’t echo out anything.
Another detail: why did we surround the $1 in quotes above? Because if we don’t pass the first argument (it is empty), we get an error like so:
./tutorialScript2.sh: line 2: [: =: unary operator expected
because we don’t supply anything for the missing argument and then we have a gap between the [ and the =. However, when we surround the argument with quotes and don’t supply the argument, we get a so-called empty string (meaning empty sequence of characters). If we use the quotes, we don’t get that error.
Exit codes answer the question above – How did it go? That is, exit codes tell you if the shell script finished successfully or has it encountered some problems and it didn’t finish successfully.
Exit code is stored in a special variable $?. Let’s look at that variable:
mislav@mislavovo-racunalo:~/Linux_folder$ ls allla
ls: cannot access 'allla': No such file or directory
mislav@mislavovo-racunalo:~/Linux_folder$ echo $?
2
If a command executes successfully, its exit code is 0. Anything other than 0 indicates a failure of some sort. To find out what exactly went wrong, you can look at EXIT VALUE or DIAGNOSTICS section in the man pages of the command that failed. (Ward, 2014)
I just wanted to leave a post saying that we will be using variables in shell scripts. We talked about variables and how they are essentially a named object that stores value in a piece of computer memory. We are going to be using them in our shell scripts for that exact purpose – storing values and then doing something based on the values of the variables.
Today, let’s talk about special variables. Special variables are contained within every shell script and can be useful, depending on what you are doing. It pays to know them.
The first type of special variables we will cover are individual arguments to the shell script. (Ward, 2014) Let’s open up our tutorialScript.sh and write (after the shebang):
echo $1
So our entire script looks like:
#!/bin/bash
echo $1
Make sure to save the script. Now, let us run our script by typing:
We see we get argument1. Try putting echo $2 instead of echo $1 and then pass two arguments to your script. If the second argument is argument2, you will get argument2 printed out. Try it out! No, really, try it out – it will take less than 1 minute of your time and it will solidify the concept, so it is a worthwhile investment based on time investment / gain ratio.
Here is a list of special variables you should try echoing out as well:
$# – number of arguments
$@ – all arguments
$0 – script name
$$ – process ID (of the shell running the script)
$? – exit code (will be covered later)
You can also shift arguments with the shift command, but I leave you to Google that if you ever need it.
Hope you learned something useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 253-255
Today let’s talk about quoting and string literals.
First, let’s take a trip back down the memory lane and look at what happens when you press Enter and thus signal the shell to run a command: (Ward, 2014)
Before running the command, the shell performs substitutions on variables and globs
The shell passes the results of the substitutions to the command
Let’s remember what happens here: In the first command, echo A*, A* gets printed out because there is not folder starting with capital a. Then, for echo a*, we get anaconda3, because there is a folder starting with zero or more lowercase a’s – that is anaconda3. Same for Calibre Library, only it starts with capital c.
But get this now:
mislav@mislavovo-racunalo:~$ echo "a*"
a*
So double quotes prevent globbing. However, double quotes don’t prevent variables from expanding:
Single quotes prevent both globbing and variables from expanding:
mislav@mislavovo-racunalo:~$ echo 'a*'
a*
mislav@mislavovo-racunalo:~$ echo '$PATH'
$PATH
So thus I propose a rule-of-thumb: In shell scripts, always use single quotes unless you have a strong reason not to do so. This is to prevent unintended globbing or variable expansion.
A sidenote: If you ever find yourself in a situation where you need to escape single quotes within a single-quoted string, see (“How to escape single quotes within single quoted strings,” n.d.).
We haven’t added anything to our tutorial script. I will tell you explicitly when to add something, so don’t worry.
In shell scripts, one-line comments start with a #. Comments are ignored by the interpreter, but they serve to instruct the human reading the script on what should the script do.
There also exist multi-line comments; see (“Shell Comments,” n.d.).