Shell編程之shift使用

來源:互聯網
上載者:User

#!/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 "
   exit 0
   ;;
  -*) echo "usage: `basename $0` -[l][u] 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.