It would seem that I have not got around to writing a post on control operators in bash, so today I will be writing a thing or two about this little aspect of bash scripts, and working with in a Command Line Interface in Linux. Control operators are just one of many little details that one will need to learn a thing or two about in order to become more professional with bash, and operating system automation tasks in Linux systems by way of bash scripts.
There are three general things to be aware of when it comes to this, one is to end a command, and start a new one which can be done with a semicolon (\;). Another thing to be aware of is to use (&&) as a way to run another command after a command, but only if the first one ends with a status code of zero. There is then using the (||) control operator if I want to run another command, but only if the command before it failed or for one reason or another exited with a non zero exit code status.
In this section I will be starting out with just a few simple and safe examples that can be used in the command line interface right away just to get an idea of what the deal is with control operators. I assume that you have at least a little experience when it comes to working within the Command Line Interface of a Linux system. If not you might want to start out with some basic commonly used commands such as echo and test, at lest those are some main commands of interest that I will be using in these examples.
The Semicolon Control Operator is what to use in order to end one command, and start another in a single line. For example there is using the test command to check if a file exists or not, and then using the echo command to print the value of the \$\? special parameter which should be the exit code status of the last command in this case the test for a file.
So if I want to test if a foo.txt file is in my home folder or not I can do something like this:
The test command will check if the file is there or not, if it is it will exit with a status of 0, if not the status will be 1. However the test command alone will not print anything to the standard output of the console. So if I just want to confirm that this is working in the command line in a single line, after test I can place a semicolon, and then use echo to print the value of the \$\? spacial parameter to the standard output of the console.
Another control operator of interest would be the \&\& operator that will call the next command after the first command, but only if the exit code status of the first command is 0. Say I want to write a line of code to put in a bash file that will test for the existence of a Documents folder, and if it is there, and only if it is there list the contents of the folder using the Linux ls command.
The || operator is then similar to \&\& only the next command will only fire if the first command DOES NOT END with a 0 exit status code. So once again say I test for the presence of a foo.txt file, then use the || operator, and then test again for a bashrc file in the home folder. After that I then use the semicolon and echo the exit status code. If the situation is that I do not have a foo.txt file there, but I do have a bashrc file, that will result in an exit code status of 1, which will then result in the next firing which will exit with a 0 status. So then in other words the final result will only be an exit status of 1 if I not not have ether of the files in the home folder.