Linux Shell Programming
This section on shell programming, is a brief introduction to shell programming, and only talks about the bash shell. For more complete information, refer to "The CTDP Linux Programmer's Guide".
When variables are used they are referred to with the $ symbol in front of them. There are several useful variables available in the shell program. Here are a few:
- $$ = The PID number of the process executing the shell.
- $? = Exit status variable.
- $0 = The name of the command you used to call a program.
- $1 = The first argument on the command line.
- $2 = The second argument on the command line.
- $n = The nth argument on the command line.
- $* = All the arguments on the command line.
- $# The number of command line arguments.
The "shift" command can be used to shift command line arguments to the left, ie $1 becomes the value of $2, $3 shifts into $2, etc. The command, "shift 2" will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth.
Iteration, control and if statements
- if - Used to execute one or more statements on a condition. An example:
if [ ! -d /mnt ] # be sure the directory /mnt exists
- case - Used to execute specific commands based on the value of a variable. An example:
case $NUM of
echo The number is 1
echo The number is 2
echo The number is not 1 or 2
- for - Used to loop for all cases of a condition. In the example below, it is used to copy all files found in /mnt/floppy to the /etc directory. The lines were numbered for reference with descriptions:
- The for loop statement will loop until all files have been found.
- A test to be sure the file is a normal file and not a directory.
- A comment line.
- This line extracts the name of the file from its full path pointed to by the variable $i and puts it in the variable $filename. The method used here is called parameter expansion and is documented in the bash man page. For more information on parameter expansion read the "Linux Programmer's Guide".
- This line sends a statement to the standard output, telling what file is being copied.
- This line performs the copy command using the -p option to preserve file attributes. Note: Much ability to perform script programming is couched in the ability to know and use the various commands, programs and tools available in Linux rather than a strict understanding of syntax. This is obvious to anyone who reads the system startup script files in /etc/rc.d and associated directories.
- This line ends the if statement.
- This line ends the for statement.
1. for i in /mnt/floppy/*; do
2. if [ -f $i ]; then
3. # if the file is there
5. echo copying $i to /etc/$filename
6. cp -p $i /etc/$filename
- until - Cycles through a loop until some condition is met. The syntax for the command is shown below:
until [ expression ]
- while - Cycles through a loop while some condition is met. The below example will cycle through a loop forever:
while [ 1 ]
There is a function provided by bash called test which returns a true or false value depending on the result of the tested expression. Its syntax is:
It can also be implied as follows:
[ expression ]
The tests below are test conditions provided by the shell:
- -b file = True if the file exists and is block special file.
- -c file = True if the file exists and is character special file.
- -d file = True if the file exists and is a directory.
- -e file = True if the file exists.
- -f file = True if the file exists and is a regular file
- -g file = True if the file exists and the set-group-id bit is set.
- -k file = True if the files' "sticky" bit is set.
- -L file = True if the file exists and is a symbolic link.
- -p file = True if the file exists and is a named pipe.
- -r file = True if the file exists and is readable.
- -s file = True if the file exists and its size is greater than zero.
- -s file = True if the file exists and is a socket.
- -t fd = True if the file descriptor is opened on a terminal.
- -u file = True if the file exists and its set-user-id bit is set.
- -w file = True if the file exists and is writable.
- -x file = True if the file exists and is executable.
- -O file = True if the file exists and is owned by the effective user id.
- -G file = True if the file exists and is owned by the effective group id.
- file1 –nt file2 = True if file1 is newer, by modification date, than file2.
- file1 ot file2 = True if file1 is older than file2.
- file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
- -z string = True if the length of the string is 0.
- -n string = True if the length of the string is non-zero.
- string1 = string2 = True if the strings are equal.
- string1 != string2 = True if the strings are not equal.
- !expr = True if the expr evaluates to false.
- expr1 –a expr2 = True if both expr1 and expr2 are true.
- expr1 –o expr2 = True is either expr1 or expr2 is true.
The syntax is :
arg1 OP arg2
where OP is one of –eq, -ne, -lt, -le, -gt, or –ge. Arg1 and arg2 may be positive or negative integers or the special expression "–l string" which evaluates to the length of string.
An example script
The file below is an example file which demonstrates some of the testing as talked above along with several looping and control statements.
# Use the bash shell to run the script
# This is an example file to take entries from the user
# entries Version 1.0 May 22, 2000
ENTRIES="hello bye ls 1"
while [ $DONE = no ]
echo Valid entries are: $ENTRIES
read ENTRY # Read the variable ENTRY from the user
case $ENTRY in
echo How are you?
ls -al |more
echo $ENTRY is an unrecognized command.
gdb debugger commands
See the gdb(1) man page for more information on the gdb program. Below are listed some commands:
- file - Loads the exe file.
- kill - Terminates the program being debugged.
- list - List sections of the source code.
- next - Advances one line of source code in the current function and steps into other functions.
- run - Executes the program that is being debugged.
- quit - Ends gdb.
- watch - Enables you to tell the value of a variable when it changes.
- break - Set a break point.
- make - Remake the exe program without quitting gdb.
- shell - Run a UNIX shell command without leaving gdb.