帮忙改个文件
时间: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语句,根本没有解压动作,
请各位大侠解救!!!
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时, 变量的值要相等才会执行相应分支,否则*
你使用了中文的!
其实我更喜欢这样使用, 不使用read
使用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
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 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
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就可以
这是为什么呢?
不过还有一个小问题,不用read l的话,直接用foo.sh foo.gz解压不了
用read l,输入foo.gz就可以
这是为什么呢?

作者: dongzhenxu 发布时间: 2011-08-25
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28