[问题]请教一个Shell脚本中的问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
zhao760722
帖子: 84
注册时间: 2007-01-31 1:26
送出感谢: 0
接收感谢: 0

[问题]请教一个Shell脚本中的问题

#1

帖子 zhao760722 » 2007-12-25 21:31

#!/bin/bash
#tr_case
FILES=""
TRCASE=""
EXT=""
OPT=no

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(s) to uppercase from lowercase"
echo "convert a file(s) from lowercase to uppercase"
echo "will convert all characters according to the"
echo "specified command option"
echo "-l convert to lowercase"
echo "-u convert to uppercase"
echo "The original file(s) is not touched. A new file(s)"
echo "will be created with eicher a .UC or .LC extension"
echo "usage: $0 -[l|u] file [file..]"
exit 0
;;
-*) echo "usage: `basename $0` -[l|u] file [file..]"
exit 1
;;
*) if [ -f $1 ]
then
FILES=$FILES" "$1 ==================================>如何解释?
else
echo "`basename $0`:Error cannot find the file $1"
fi
shift
;;
esac

done

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

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 called $LOOP$EXT"
fi
;;
upper) cat $LOOP |tr "[a-z]" "[A-Z]" >$LOOP$EXT
if [ $? != 0 ];then
error_msg $LOOP
else
echo "converted file called $LOOP$EXT"
fi
;;
esac
done
头像
bones7456
论坛版主
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
送出感谢: 0
接收感谢: 8 次
联系:

#2

帖子 bones7456 » 2007-12-26 9:09

FILES=$FILES" "$1
就是在 FILES 这个变量后面加个空格,再加上 $1 ,然后把结果再赋值给 FILES
关注我的blog: ε==3
zhao760722
帖子: 84
注册时间: 2007-01-31 1:26
送出感谢: 0
接收感谢: 0

再问?

#3

帖子 zhao760722 » 2007-12-26 21:44

echo $FILES 后没有值,是什么回事?能不能连接上下文说的细一点?
头像
iblicf
帖子: 3766
注册时间: 2007-01-15 17:15
送出感谢: 0
接收感谢: 0

#4

帖子 iblicf » 2007-12-26 22:09

? FILES=$FILES" "$1 是要默认处理某些文件+参数输入的文件 ... 因为 FILES="" 是初始化的 , 所以只处理参数输入的文件 ...

比如 : 如果 FILES="b" 初始化的 , 那么

代码: 全选

./tr_case -u c
converted file called b.UC
converted file called c.UC
回复

回到 “Shell脚本”