分页: 1 / 1

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

发表于 : 2007-12-25 21:31
zhao760722
#!/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

发表于 : 2007-12-26 9:09
bones7456
FILES=$FILES" "$1
就是在 FILES 这个变量后面加个空格,再加上 $1 ,然后把结果再赋值给 FILES

再问?

发表于 : 2007-12-26 21:44
zhao760722
echo $FILES 后没有值,是什么回事?能不能连接上下文说的细一点?

发表于 : 2007-12-26 22:09
iblicf
? FILES=$FILES" "$1 是要默认处理某些文件+参数输入的文件 ... 因为 FILES="" 是初始化的 , 所以只处理参数输入的文件 ...

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

代码: 全选

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