2017-10-20 2 views
0

こんにちは、私はこれに新しいです。 私はfiletypeがtxtかzipかどうかをチェックするためにこの(単純なはずです)bashスクリプトを書いています。しかし、それはバッシュ:ifとtypecheck

動作しないコードは次のとおりですので、

if [ ${file: -4} == ".txt" ]
と最後の4つの文字

または使用比較:

#!/bin/sh 
echo "please insert file" 
read file 
if [ $file == *.txt ] 
then 
emacs $file 
elif [ $file == *.zip ] 
then 
zip $file 
fi 
+1

「sh」は通常「bash」ではありません。 – Cyrus

+1

'sh' *が' bash'であっても '['はパターンマッチングを行いません。 – chepner

+1

ご覧ください:http://www.shellcheck.net/ – Cyrus

答えて

1

case文を使用します。

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

thx!私の問題を解決した –

0

あなたは、使用のいずれかのことができますif [[ $file == *.txt ]]
です。したがって、 gular式(二重括弧は一部のシェルでのみ利用可能ですが、bashで利用可能です)。