Bash Programming
Here's the most common shell types:- bash = Bourne again shell
- sh = shell
- csh = C shell
- tcsh = Tenex C shell (not tab-completion-extended C shell)
- tclsh = Tcl shell
- ksh = korn shell
- ash = a shell
- bash = bourne shell ? (in most Linux distributions it's a link to /bin/ash)
- zsh = the Z shell (it's what it's manual page tells about it .. :/ )
- Normal interactive: Login shells run commands in /etc/profile. The first of ˜/.bash_profile, ˜/.bash_login, and ˜/.profile that is found is executed. This stage is skipped if −−noprofile is used. Upon logout, bash runs ˜/.bash_logout if it exists. Interactive non-login shells execute ˜/.bashrc, if it exists. The −−rcfile ifile option changes the file that is used.
- Normal non-interactive: Non-interactive shells do variable, command, and arithmetic substitution on the value of $BASH_ENV, and if the result names an existing file, that file is executed.
- Invoked as sh: Interactive login shells read and execute /etc/profile and ˜/.profile if they exist. These files are skipped if −−noprofile is used. Interactive shells expand $ENV and execute that file if it exists. Non-interactive shells do not read any startup files. After the startup files are executed, bash enters POSIX mode.
- POSIX mode: When started with −−posix, interactive shells expand $ENV and execute the given file. No other startup files are read.
- Invoked via rshd: If run from rshd and not invoked as sh, bash reads ˜/.bashrc. The −−norc option skips this step, and the −−rcfile option changes the file, but rshd usually does not pass these options on to the shell it invokes. If $SHELLOPTS exists in the environment at startup, bash enables the given options.
#!/bin/bashThis indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen.
A simple example
It just runs a few simple commands.#!/bin/bash echo "hello, $USER" echo "listing files in the current directory, $PWD" ls # list filesFirstly, notice the comment on line 4. In a bash script, anything following a pound sign # (besides the shell name on the first line) is treated as a comment. ie the shell ignores it.
$USER and $PWD are variables (environment variables.. can be found when u run 'printenv' command).
Variables
You define a variable as follows:
X="hello"and refer to it as follows:
$XMore specifically, $X is used to denote the value of the variable X.
Some things to take note of:
- bash gets unhappy if you leave a space on either side of the = sign. For
example, the following gives an error message:
X = hello
- Quotes are not always necessary. Where you need quotes is when your variable names include spaces. For example,
X=hello world # error X="hello world" # OK
This is because the shell essentially sees the command line as a pile of commands and command arguments seperated by spaces.
X=hello is considered a command.
the problem with the command X=hello world is that the shell interprets X=hello as a command, and the word "world" does not make any sense (since the assignment command doesn't take arguments).
Basically, variable names are expanded within double quotes, but not single quotes. If you do not need to refer to variables, single quotes are good to use as the results are more predictable.
An example
#!/bin/bash echo -n '$USER=' # -n option stops echo from breaking the line echo "$USER" echo "$USER=$USER" # this does the same thing as the first two linesThe output looks like this (assuming your username is root):
$USER=root $USER=rootso the double quotes still have a work around. Double quotes are more flexible, but less predictable. Given the choice between single quotes and double quotes, use single quotes.
Using Quotes to enclose your variables
Sometimes, it is a good idea to protect variable names in double quotes.
This is usually the most important if your variable's value either
(a) contains spaces or (b) is the empty string. An example is as follows:
#!/bin/bash X="" if [ -n $X ]; then # -n tests to see if the argument is non empty echo "the variable X is not the empty string" fiThis script will give the following output:
the variable X is not the empty stringWhy? Because the shell expands $X to the empty string. The expression [ -n ] returns true (since it is not provided with an argument).
A better script would have been:
#!/bin/bash X="" if [ -n "$X" ]; then # -n tests to see if the argument is non empty echo "the variable X is not the empty string" fiIn this example, the expression expands to [ -n "" ] which returns false, since the string enclosed in inverted commas is clearly empty.
Variable Expansion in action
Just to convince you that the shell really does "expand" variables in the sense I mentioned before, here is an example:
#!/bin/bash LS="ls" LS_FLAGS="-al" $LS $LS_FLAGS $HOMEThis looks a little enigmatic. What happens with the last line is that it actually executes the command:
ls -al /home/root (assuming that /home/cvsroot is your home directory)That is, the shell simply replaces the variables with their values, and then executes the command.
Using Braces to Protect Your Variables
OK. Here's a potential problem situation. Suppose you want to echo the value of the variable X, followed immediately by the letters "abc". Question: how do you do this? Let's have a try:
#!/bin/bash X=ABC echo "$Xabc"This gives no output. What went wrong?
The answer is that the shell thought that we were asking for the variable Xabc, which is uninitialised. The way to deal with this is to put braces around X to seperate it from the other characters. The following gives the desired result:
#!/bin/bash
X=ABC
echo "${X}abc"
Conditionals, if/then/elifSometimes, it's necessary to check for certain conditions. Does a string have 0 length ? does the file "foo" exist, and is it a symbolic link , or a real file ? Firstly, we use the if command to run a test. The syntax is as follows:
if condition then statement1 statement2 .......... fiSometimes, you may wish to specify an alternate action when the condition fails. Here's how it's done.
if condition then statement1 statement2 .......... else statement3 fiAlternatively, it is possible to test for another condition if the first "if" fails. Note that any number of elifs can be added.
if condition1 then statement1 statement2 .......... elif condition2 then statement3 statement4 ........ elif condition3 then statement5 statement6 ........ fiThe statements inside the block between if/elif and the next elif or fi are executed if the corresponding condition is true. Actually, any command can go in place of the conditions, and the block will be executed if and only if the command returns an exit status of 0 (in other words, if the command exits "succesfully" ). However, in the course of this document, we will be only interested in using "test" or "[ ]" to evaluate conditions.
The Test Command and Operators
The command used in conditionals nearly all the time is the test command.
Test returns true or false (more accurately, exits with 0 or non zero status)
depending respectively on whether the test is passed or failed. It works like this:
test operand1 operator operand2For some tests, there need be only one operand (operand2). The test command is typically abbreviated in this form:
[operand1 operator operand2 ]To bring this discussion back down to earth, we give a few examples:
#!/bin/bash if [ $USER = "gnulamp" ]; then echo "User is gnulamp" fiA brief summary of test operators
Here's a quick list of test operators. It's by no means comprehensive, but its likely to be all you'll need to remember (if you need anything else, you can always check the bash manpage ... )
| operator | produces true if... | number of operands |
| -n | operand non zero length | 1 |
| -z | operand has zero length | 1 |
| -d | there exists a directory whose name is operand | 1 |
| -f | there exists a file whose name is operand | 1 |
| -eq | the operands are integers and they are equal | 2 |
| -neq | the opposite of -eq | 2 |
| = | the operands are equal (as strings) | 2 |
| != | opposite of = | 2 |
| -lt | operand1 is strictly less than operand2 (both operands should be integers) | 2 |
| -gt | operand1 is strictly greater than operand2 (both operands should be integers) | 2 |
| -ge | operand1 is greater than or equal to operand2 (both operands should be integers) | 2 |
| -le | operand1 is less than or equal to operand2 (both operands should be integers) | 2 |
Loops
Loops are constructions that enable one to reiterate a procedure
or perform the same procedure on several different items.
There are the following kinds of loops available in bash
- for loops
- while loops
The syntax for the for loops is best demonstrated by example.
#!/bin/bash for X in red green blue do echo $X doneThe for loop iterates the loop over the space seperated items. Note that if some of the items have embedded spaces, you need to protect them with quotes. Here's an example:
#!/bin/bash colour1="red" colour2="light blue" colour3="dark green" for X in "$colour1" $colour2" $colour3" do echo $X doneCan you guess what would happen if we left out the quotes in the for statement ? This indicates that variable names should be protected with quotes unless you are pretty sure that they do not contain any spaces. Globbing in for loops
The shell expands a string containing a * to all filenames that "match". A filename matches if and only if it is identical to the match string after replacing the stars * with arbitrary strings. For example, the character "*" by itself expands to a space seperated list of all files in the working directory (excluding those that start with a dot "." ) So echo * lists all the files and directories in the current directory. echo *.jpg lists all the jpeg files. echo ${HOME}/www/*.jpg lists all jpeg files in your public_html directory.
As it happens, this turns out to be very useful for performing operations on the files in a directory, especially used in conjunction with a for loop. For example:
#!/bin/bash
for X in *.html
do
grep -L 'gif' "$X"
done
While LoopsWhile loops iterate "while" a given condition is true. An example of this:
#!/bin/bash X=0 while [ $X -le 20 ] do echo $X X=$((X+1)) doneThis raises a natural question- why doesn't bash allow the C like for loops for (X=1,X<10; X++) As it happens, this is discouraged for a reason: bash is an interpreted language, and a rather slow one for that matter. For this reason, heavy iteration is discouraged.
Command Substitution
Command Substitution is a very handy feature of the bash shell. It enables you to take the output of a command and treat it as though it was written on the command line. For example, if you want to set the variable X to the output of a command, the way you do this is via command substitution.
There are two means of command substitution: brace expansion and backtick expansion.
Brace expansion works as follows:
$(commands)expands to the output:
commandsThis permits nesting, so commands can include brace expansions
Backtick expansion expands
commands`to the output:
commandsAn example is given:
#!/bin/bash
files="$(ls )"
web_files=`ls www`
echo $files
echo $web_files
X=`expr 3 * 2 + 4` # expr evaluate arithmatic expressions.
#man expr for details.
echo $X
Note that even though the output of ls contains newlines, the variables do not. Bash variables can not contain newline characters (which is a pain in the butt. But that's life) Anyway, the advantage of the $() substitution method is almost self evident: it is very easy to nest. It is supported by most of the bourne shell varients (the POSIX shell or better is OK). However, the backtick substitution is slightly more readable, and is supported by even the most basic shells (any #!/bin/sh version is just fine).