To really understand how the shell works, you need to see it in action. What follows is a simple examples of real shell script. See if you can understand how it works.
#!/bin/sh ############################################################################# # FILE : process # AUTHOR : Peter Chapin # LAST REVISED: September 1999 # SUBJECT : Sample shell script. # # The following shell script accepts a list of filenames on the command # line and presents the user with a menu of possible actions for each file in # that list. Once the user has decided about the disposition of a file, the # next file is presented. ############################################################################# if [ ! -w . ] ; then echo "No write access to the current directory: Can't continue" else # Loop over all the files specified on the command line. for FILENAME in $* ; do # Assume that we will stay on this file. Set to "no" below if otherwise. RETRY=yes # Ignore directories, etc. if [ ! -f $FILENAME ] ; then echo "$FILENAME is not a plain file: ignoring." echo "Strike RETURN to continue...\c" read junk else # Keep presenting the menu until user does something with the file. while [ $RETRY = "yes" ] ; do echo " " echo "\033[2J\033[1;1HFILE: $FILENAME\n" echo " 0) No action" echo " T) Type" echo " V) View" echo " N) reName" echo " R) Remove" echo " C) make a new Copy" echo " D) copy to a Directory" echo " Q) Quit" echo "Enter command digit: \c" read RESPONSE junk # Handle each command. case $RESPONSE in 0) # No action. RETRY=no;; T|t) # Type of file. file $FILENAME;; V|v) # View file. more $FILENAME;; N|n) # Rename file. Be sure new name is free for use. echo "Enter the new name: \c" read NAME junk if [ -f $NAME -o -d $NAME ] ; then echo "$NAME already exists" else mv $FILENAME $NAME RETRY=no fi;; R|r) # Remove file (no questions asked). rm $FILENAME RETRY=no;; C|c) # Copy file. Be sure new name is free for use. if [ ! -r $FILENAME ] ; then echo "Can't read $FILENAME" else echo "Enter the name of the new copy: \c" read NAME junk if [ -f $NAME -o -d $NAME ] ; then echo "$NAME already exists" else cp $FILENAME $NAME RETRY=no fi fi;; D|d) # Copy to a directory. Be sure name is really that of a dir. if [ ! -r $FILENAME ] ; then echo "Can't read $FILENAME" else echo "Enter the name of the destination directory: \c" read NAME junk if [ ! -d $NAME ] ; then echo "$NAME is not a directory" elif [ ! -w $NAME ] ; then echo "Can't write in the directory $NAME" else cp $FILENAME $NAME RETRY=no fi fi;; Q|q) exit;; *) echo "I don't understand $RESPONSE" esac echo "Strike RETURN to continue\c" read junk done # End of while... loop which waits for RETRY to become "no" fi # End of if...else... which ignores special files. done # End of while... loop which processes all the filenames. fi # End of if...else... which checks for writability to .