0
ファイルを行単位で検索し、文字列が一致したときに特定のメッセージを画面に出力したい文字列が一致していない場合は画面に別のメッセージが表示されます。'for'ループ内の 'if'文をネストしてファイル内の一致する/一致しない文字列を検出する
ファイルは、スクリプトが実行されている同じディレクトリに配置されのNetwork.txtと呼ばれる:これは私がこれまで持っているものである
。これは、ファイルの内容は次のとおりです。
am.12345
XXXXXXXX
am.67890
XXXXXXXX
これはスクリプトです:
#!/bin/bash
file="network.txt"
for line in `cat $file`
do
if [ $line == am.* ]
then
echo $line
elif [ $line != am.* ]
then
echo "We couldn't find what you were looking for"
fi
done
これは私が、bashのデバッグから受け取る出力されます:私は実行時に
+ file=network.txt
++ cat network.txt
+ for line in '`cat $file`'
+ '[' am.12345 == 'am.*' ']'
+ '[' am.12345 '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
+ for line in '`cat $file`'
+ '[' XXXXXXXX == 'am.*' ']'
+ '[' XXXXXXXX '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
+ for line in '`cat $file`'
+ '[' am.67890 == 'am.*' ']'
+ '[' am.67890 '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
+ for line in '`cat $file`'
+ '[' XXXXXXXX == 'am.*' ']'
+ '[' XXXXXXXX '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
しかし、スクリプト、私は期待された動作を取得しません:
macbook:~ enduser$ ./network.sh
We couldn't find what you were looking for
We couldn't find what you were looking for
We couldn't find what you were looking for
We couldn't find what you were looking for
私は、出力は次のようになりますと信じて:
am.12345
We couldn't find what you were looking for
am.67890
We couldn't find what you were looking for
私はこの問題を解決するために変更する必要がありますか?