|
发表于 2005-8-27 00:45:01
|
显示全部楼层
Post by mantou
#!/usr/bin/env bash
2
3 echo $0
4 echo $1
5 echo $#
6 echo ${1##*.}
7 tartype=${1##*.}
8 file=$1
9
10 tarview() {
11 echo -n "Displaying contents of $1"
12 case "$tartype" in
13 tar)
14 echo "(uncompuressed tar)"
15 echo $(tar tv $file);;
16 gz)
17
18 echo "(gzip-compressed tar)"
19 echo $file
20 echo $(tar tzf $file);;
21 bz2)
22 echo "(bzip2-compressed tar)"
23 echo $(cat $file | bzip2 -d | tar tv -);;
24 esac
25 }
26
27 tarview
这是我将下面的脚本改成case的, 在函数中使用$1 ${1##*.}不能获得如期的值。
tarview() {
echo -n "Displaying contents of $1 "
if [ ${1##*.} = tar ]
then
echo "(uncompressed tar)"
tar tvf $1
elif [ ${1##*.} = gz ]
then
echo "(gzip-compressed tar)"
tar tzvf $1
elif [ ${1##*.} = bz2 ]
then
echo "(bzip2-compressed tar)"
cat $1 | bzip2 -d | tar tvf -
fi
}
这个脚本,执行的时候,提示错误:
Displaying contents of ./8: line 5: [: =: unary operator expected
./8: line 9: [: =: unary operator expected
./8: line 13: [: =: unary operator expected
以上是可以执行和有错误的脚本。 但是不能执行的脚本是IBM的文章提供的。
我按部就班的时候发现,$1,${1##*.},这些变量在函数中没有作用哦。 我才多转了一下。
产生这种情况的原因是什么,面对这样的情况该如何处理??
Since you used $1, so the last line will also need to be changed to "tarview $1". $1 is the position variable. |
|