Bash:if和typecheck

Bash:if和typecheck,bash,Bash,嗨,伙计们,我是新来的。希望你能帮助我 我写这个(应该很简单)bash脚本来检查文件类型是txt还是zip。但它不起作用 代码是: #!/bin/sh echo "please insert file" read file if [ $file == *.txt ] then emacs $file elif [ $file == *.zip ] then zip $file fi 您可以使用: 如果[${file:-4}==“.txt”] 然后比较最后4个字符 或使用: if[[$file

嗨,伙计们,我是新来的。希望你能帮助我 我写这个(应该很简单)bash脚本来检查文件类型是txt还是zip。但它不起作用

代码是:

#!/bin/sh
echo "please insert file"
read file
if [ $file == *.txt ]
then
emacs $file
elif [ $file == *.zip ]
then 
zip $file
fi
您可以使用:

如果[${file:-4}==“.txt”]

然后比较最后4个字符

或使用:

if[[$file==*.txt]

因此使用正则表达式(双括号仅在某些shell中可用,但在
bash
中可用)。

使用case语句

case "$file" in
*.txt) emacs $file ;;
*.zip) zip   $file ;;
esac

sh
通常不是
bash
。即使
sh
bash
[
也不会进行模式匹配。请看一看:thx!解决了我的问题