ファイルが存在する場合は、ファイル内に異なる単語 "bot、spyware、virus"の特定の配列を見つけるスクリプトです。配列内にgrepを使用
#!/bin/bash
#strings to find in file
NAME[0]="bots"
NAME[1]="spyware"
NAME[2]="virus"
#location of file
LOGS=/home/testing.txt
#define function to grep any of the above mentioned array strings in file
func(){
if `grep "${NAME[*]}" $LOGS`; then
echo "CRITICAL: ${NAME[*]}"
else
echo "errors not found"
exit
fi
}
#file exist or not exist
if [ -f $LOGS ]; then
echo " File Found"
#call functions
func
modified
else
echo "File Not Found"
exit
しかし、grep "${NAME[*]}" $LOGS
は機能しません。以下のエラーが表示されます。
grep: virus: No such file or directory
grep: bots: No such file or directory
'set -x 'を使ってシェルスクリプトのデバッグトレースを有効にすることを学ぶ(' set + x'をオフにする)。そして、arrを 'grep 'a | b | c' file'のような検索ターゲットに変換する必要があることがわかります。そして2番目の素晴らしいQですが、 'func()'コードしか見る必要がないので、Qを非動作コードにすることを学んでください。がんばろう。 – shellter