【发布时间】:2018-05-07 18:08:25
【问题描述】:
我的问题是关于 Bash,Shell。我正在写一个脚本,我有以下问题: 当用户声明他或她会将文件提取到目录中时,我有一个案例。但是我必须测试是否存在,如果存在需要检查该文件是否为 *.tar 文件。我在检查文件是否可执行时搜索了类似的东西:
if [ -x "file" ]; then
echo "file is executable"
else
echo "file is not executable"
# will this if test work?
case $1
"--extract")
if [ -e $2 ] && [ tar -tzf $2 >/dev/null ]; then
echo "file exists and is tar archive"
else
echo "file either does not exists or it is not .tar arcive"
fi
;;
esac
上面的代码不起作用,它被完全忽略了。有什么想法吗?
【问题讨论】:
-
那个链接是个好东西。您可以解析
file yourfile.tar的输出,也可以查看扩展名"${filename##*.}",但我认为您尝试提取tar 的最佳答案是最安全的方法。 -
代码缺少结束
fi。代码不应运行。另见How to use Shellcheck、How to debug a bash script? (U&L.SE)、How to debug a bash script? (SO)、How to debug bash script? (AskU)、Debugging Bash scripts 等。
标签: linux bash shell if-statement