在 case 语句中使用 grep 命令
所以我有这个脚本,我试图确定文件的类型并相应地采取行动,我使用 file 命令确定文件的类型,然后使用 grep 查找特定字符串,例如,如果文件被压缩,则解压缩它,如果它gzipped然后gunzip它,我想添加很多不同类型的文件。
我试图用 case
替换 if
语句,但无法弄清楚
我的脚本如下所示:
##$arg is the file itself
TYPE="$(file $arg)"
if [[ $(echo $TYPE|grep "bzip2") ]] ; then
bunzip2 $arg
elif [[ $(echo $TYPE|grep "Zip") ]] ; then
unzip $arg
fi
感谢所有提供帮助的人:)
So I have this script which im trying to determine the type of the file and act accordingly, I am determining the type of the file using file command and then grep for specific string , for example if the file is zipped then unzip it, if its gzipped then gunzip it, I want to add a lot of different types of file.
I am trying to replace the if
statements with case
and can't figure it out
My script looks like this:
##$arg is the file itself
TYPE="$(file $arg)"
if [[ $(echo $TYPE|grep "bzip2") ]] ; then
bunzip2 $arg
elif [[ $(echo $TYPE|grep "Zip") ]] ; then
unzip $arg
fi
Thanks to everyone that help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
操作
对于您的代码,
如果要捕获
file
输出到变量,请先执行此操作,当然,请执行此 。但是避免使用私人变量的上层案例。bzip22 < /code>和
unzip
默认情况下修改其输入文件。也许您想避免这种情况?还请注意,外壳如何使您可以从(和进入)条件下管道。
The general syntax is
So for your snippet,
If you want to capture the
file
output into a variable first, do that, of course; but avoid upper case for your private variables.bzip2
andunzip
by default modify their input files, though. Perhaps you want to avoid that?Notice also how the shell conveniently lets you pipe out of (and into) conditionals.