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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值