分页: 1 / 1

求教!关于编写一个smartzip的程序

发表于 : 2011-08-25 10:01
dongzhenxu
#!/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语句,根本没有解压动作,

请各位大侠解救!!!

Re: 求教!关于编写一个smartzip的程序

发表于 : 2011-08-25 10:27
我就是我2
shell的处理空格时会比较头痛.可以这样来修改,依file命令结果的第二域来判断或者使用非空格等作为分隔符
使用case时, 变量的值要相等才会执行相应分支,否则*
:em20 :em20 你使用了中文的!

代码: 全选

#!/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

Re: 求教!关于编写一个smartzip的程序

发表于 : 2011-08-25 11:08
tusooa
两边加星号

Re: 求教!关于编写一个smartzip的程序

发表于 : 2011-08-25 13:01
dongzhenxu
问题解决啦,谢谢2楼!!

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

Re: 求教!关于编写一个smartzip的程序

发表于 : 2011-08-25 14:33
eexpress
重复工作。
现成的脚本,到处都是。

Re: 求教!关于编写一个smartzip的程序

发表于 : 2011-08-25 15:09
我就是我2
先:em70
作为练习还是可以的.
dongzhenxu 写了:问题解决啦,谢谢2楼!!

不过还有一个小问题,不用read l的话,直接用foo.sh foo.gz解压不了
用read l,输入foo.gz就可以
这是为什么呢? :em06
:em20 我也不知道为什么.
后面两个解压语句使用只tar可以

代码: 全选

tar xvf $1
rar和7z也可以添加上.
不过ee说了,现成的脚本多得是.

Re: 求教!关于编写一个smartzip的程序

发表于 : 2011-08-25 16:25
dongzhenxu
嗯嗯,谢谢大家了 :em01