Shell编程之shift运用

Shell编程之shift使用

#!/bin/sh
FILES=""
TRCASE=""
EXT=""
OPT=no

#gets called when a conversion fails
error_msg()
{
_FILENAME=$1
echo "`basename $0`:Error the conversion failed on $_FILENAME"
}


if [ $# -eq 0 ]
then
 echo "For more info try `basename $0` --help"
 exit 1
fi

while [ $# -gt 0]
do
 case $1 in
  -u) TRCASE=upper
   EXT=".UC"
   OPT=yes
   shift
   ;;
  -l) TRCASE=lower
   EXT=".LC"
   OPT=yes
   shift
   ;;
  -help) echo "convert a file(files) to uppercase from lowercase"
   echo "convert a file(files) from lowercase to uppercase"
   echo "will convert all characters according to the specified command option."
   echo "where option is"
   echo "-l Convert to lowercase"
   echo "-u Convert to uppercase"
   echo "The originak files is not touched,A new file will be created with either a .UC or .LC extension"
   echo "usage:$0 -[l][u] file [file..]"
   exit 0
   ;;
  -*) echo "usage: `basename $0` -[l][u] file [file..]"
   exit 1
   ;;

  #collect the files to process
  *) if [ -f $2 ]
   then
    #add the filenames to a variable list
    FILES=$FILES" "$1
   else
    echo "`basename $0`:Error can not find the file$1"
   fi
   shift
   ;;
 esac
done


#no options given ...help the user

if [ "$OPT" = "no" ]
then
 echo "`basename $0`:Error you need to specify an option no action taken"
 echo "try `basename $0` --help"
 exit 1
fi


#now read in all files
for loop in $FILES
do
 case $TRCASE in
  lower) cat $loop|tr "[a-z]" "[A-Z]" >$loop$EXT
   if [ $? != 0 ]
   then
    error_msg $loop
   else
    echo "Converted file call $loop$EXT"
   fi
   ;;
  upper) cat $loop|tr "[A-Z]" "[a-z]" >$loop$EXT
   if [ $? != 0 ]
   then
    error_msg $loop
   else
    echo "Converted file call $loop$EXT"
   fi
   ;;
 esac
done