+ -
当前位置:首页 → 问答吧 → 帮忙改个文件

帮忙改个文件

时间:2011-08-25

来源:互联网

#!/bin/sh
read l
ftype=`file "$l"`
case "$ftype" in
"$l:Zip archive") unzip "$l";;

"$l:gzip compressed") gunzip "$l";;

"$l:bzip2 compressed") bunzip2 "$l";;

*) echo "$l cannot be uncompressed!"
esac
-----------------------------------------------------------------------
这段代码运行结果是,无论我解压任何压缩文件,都只是显示最后一句echo语句,根本没有解压动作,

请各位大侠解救!!!

作者: dongzhenxu   发布时间: 2011-08-25

shell的处理空格时会比较头痛.可以这样来修改,依file命令结果的第二域来判断或者使用非空格等作为分隔符
使用case时, 变量的值要相等才会执行相应分支,否则*
你使用了中文的!
代码:
#!/bin/sh
read l
ftype=`file "$l"|awk '{print $2}'`
case "$ftype" in
"Zip") unzip "$l";;

"gzip") gunzip "$l";;

"bzip2") bunzip2 "$l";;

*) echo "$l cannot be uncompressed!"
esac

代码:
#!/bin/sh
read l
ftype=`file "$l"|awk '{print $1$2"-"$3}'`
case "$ftype" in
"$l:Zip-archive") unzip "$l";;

"$l:gzip-compressed") gunzip "$l";;

"$l:bzip2-compressed") bunzip2 "$l";;

*) echo "$l cannot be uncompressed!"
esac


其实我更喜欢这样使用, 不使用read
代码:
foo.sh foo.zip

代码:
#!/bin/sh
ftype=`file "$1"|awk '{print $2"-"$3}'`
case "$ftype" in
"Zip-archive") unzip "$1";;

"gzip-compressed") gunzip "$1";;

"bzip2-compressed") bunzip2 "$1";;

*) echo "$1 cannot be uncompressed!"
esac

作者: 我就是我2   发布时间: 2011-08-25

两边加星号

作者: tusooa   发布时间: 2011-08-25

问题解决啦,谢谢2楼!!

不过还有一个小问题,不用read l的话,直接用foo.sh foo.gz解压不了
用read l,输入foo.gz就可以
这是为什么呢?

作者: dongzhenxu   发布时间: 2011-08-25