Previous Page | Next Page

  1. Introduction
  2. About Linux
  3. Installation and getting started
  4. Logging in and out
  5. Basic Linux Commands
  6. Linux Files and File Permissions
  7. Linux Directory Structure
  8. Finding Files
  9. Linux Help
  10. Setting Time
  11. Devices
  12. Tips
  13. Accessing Other Filesystems
  14. Accessing Removable Media
  15. Making and Managing Filesystems
  16. Emergency Filesystems and Procedures
  17. LILO and Runlevels
  18. Init
  19. Environment, Shell Selection, and Startu
  20. Linux Kernel
  21. Package Installation and Printing
  22. Configuration, Logging and CRON
  23. Keys and Terminal Configuration
  24. Sound Configuration
  25. Managing Users
  26. Passwords
  27. Process Control
  28. Configuration and Diagnostic Tools
  29. Overall Configuration
  30. Using PAM
  31. Basic Network Setup
  32. Tools and Terms
  33. Novell and Printing
  34. Inetd Services
  35. Xinetd Services
  36. Other Network Services
  37. FTP and Telnet
  38. Samba
  39. Identd (auth)
  40. X Configuration
  41. X Use
  42. Using X Remotely
  43. X Documentation
  44. DNS
  45. DHCP and BOOTP
  46. Apache
  47. NFS
  48. PPP
  49. Mail
  50. Routing
  51. IP Masquerading
  52. Proxy Servers and ipchains
  53. UUCP
  54. News
  55. NIS
  56. Network Security
  57. Secure Shell
  58. Text Processing
  59. Shell Programming
  60. Emacs
  61. VI
  62. Recommended Reading
  63. Credits

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".

Linux Variables

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
    then
       mkdir /mnt
    fi
    
  • case - Used to execute specific commands based on the value of a variable. An example:
    case $NUM of
    1)
       echo The number is 1
       ;;
    2)
       echo The number is 2
       ;;
    *)
       echo The number is not 1 or 2
       ;;
    esac
    
  • 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:
    1. The for loop statement will loop until all files have been found.
    2. A test to be sure the file is a normal file and not a directory.
    3. A comment line.
    4. 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".
    5. This line sends a statement to the standard output, telling what file is being copied.
    6. 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.
    7. This line ends the if statement.
    8. This line ends the for statement.
    1.  for i in /mnt/floppy/*; do
    2.     if [ -f $i ]; then
    3.     # if the file is there
    4.        filename=${i#/mnt/floppy/}
    5.        echo copying $i to /etc/$filename
    6.        cp -p $i /etc/$filename
    7.     fi
    8.  done
    
  • until - Cycles through a loop until some condition is met. The syntax for the command is shown below:
    until [ expression ]
    do
       statements
    done
    
  • while - Cycles through a loop while some condition is met. The below example will cycle through a loop forever:
    while [ 1 ]
    do
       statement(s)
    done
    

Tests

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:

test expression

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.

#! /bin/bash	
# 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
DONE=no
ENTRIES="hello bye ls 1"
while [ $DONE = no ]
do
   echo Valid entries are: $ENTRIES
   read ENTRY             # Read the variable ENTRY from the user
   case $ENTRY in
   1)
       pwd
       ;;
   hello)
       echo How are you?
       ;;
   bye)
       echo exiting...
       DONE=yes
       ;;
   ls)
       ls -al |more
       ;;
   *)
       echo $ENTRY is an unrecognized command.
       ;;
   esac
done

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.