I’ve recently been doing a fair amount of bash scripting so it seems like a good time to document what i’ve learned / re-learned (god bless the first year of university!).
System commands
If you wish to use a system command in bash, then the best way i’ve found to make it work is by declaring a variable and then setting it to the output. In my script, i wanted to log an error to /var/log/errorfile.log with a date/time stamp at the beginning. The output of “date” would be perfect for this.
To do this in bash we can do the following:
now=$(date)
Where “now” is our bash variable, and $( ) is the wrapper we added around our system command ‘date’. This allows us to now use the date output as below:
echo "$now : this is an error message"
Which would show in the event log as:
Wed Jun 19 10:13:50 BST 2013 : This is an error message
IF Statements
Simple IF statements are a life saver in Bash scripting, in my scripts they were mainly used for sanity checking or running different scripts based upon the version of Red Hat, Ubuntu, etc.
To setup an IF statement, we need to first know what we are checking, and what to do if it is correct and not correct (met / unmet) as below:
... user=$(whoami) if [ "$user" != 'root' ]; then echo "Running as $user - this script should be ran as root". exit 1; else
echo "Please enter your OS: [Red Hat, CentOS, Ubuntu, Debian]" read os ...
This sample script gets the value of “whoami” (who is running the script), checks that it is root – if its not root, it gives an error message – if it is root, it carries on with the installation / configuration of the server.
ELIF Statements (Else If)
Using ELIF statements, we can run through different scenarios based upon the given value entered by the user. In my example script, we are asking the user to enter their version of Linux from the above 4 options.
echo "Please enter your OS: Red Hat, CentOS, Ubuntu, Debian:" read os if [ "$os" = 'Red Hat' ]; then echo "Red Hat script will run, need to disable SE Linux." .redhat-script.scri elif [ "$os" = 'Ubuntu' ]; then echo "Ubuntu script will run now." .ubuntu-script.scri elif [ "$os" = 'CentOS' ]; then echo "CentOS script will run, need to disable SE Linux." ./centos-script.scri elif [ "$os" = 'Debian' ]; then echo "Debian script will run now." ./debian-script.scri else echo "You did not specify a valid OS: CentOS, Red Hat, Debian, Ubuntu." fi
This script reads the “os” value in from the user input, and then runs through the scenarios using “elif” and jumps off into the relevant script when a match is found. If no match is found, then the catch-all error message is sent to terminal.
IF Statements: OR
Using OR’s in an IF statement is important in complex scripts. In our environment, we want to check that the architecture is supported, and that the version of software (RHEL in this example) is supported also. To do this, we can use OR’s as below:
if [ "$rhelversion" = '5' ] || [ "$rhelversion" = '6' ] ; then if [ "$architecture" = 'i386' ] || [ "$architecture" = 'x86_64' ] ; then
...
else echo "Your architecture is not compatible. Please select either i386 or x86_64 and try again" echo "$now : Attempted to install using the architecture variable: $architecture - failed. Please use i386 or x86_64" >> /var/log/installer-script.log exit 1; fi else echo "Your specified OS is not valid: Please try again using either '5' or '6'" echo "$now Attempted to install using the OS variable: $rhelversion - failed. Please use 5 or 6" >> /var/log/installer-script.log exit 1; fi;
This script will run through the first IF statement; checking the RHEL Version – if this is a match for what we want (5 or 6), we go into the 2nd IF statement. If its not a match, we jump down to the last else, and error to the terminal / log file.
If the first IF is a match, we go onto the next one: checking if the architecture is i386 or x86_64. Again, we use the OR statement denoted by ” || “. If this is a match, as above, we dive into the script, else we error again.
End Notes
So thats all for now, i’ll try and add more and more as i pick up different tidbits of information!
Sam