/home/errorcodes
包含:
421 RP-001
421 RP-002
421 RP-003
550 SC-001
550 SC-002
550 SC-003
550 SC-004
550 DY-001
550 DY-002
550 DY-001
550 OU-001
550 OU-002
脚本:
#!/bin/bash
Elogs=/home/elogs.txt
Errors=/home/errorcodes
for i in `cat $Errors`; do
#Get Error Logs
grep "$i" /home/eximlog >> $Elogs
done
调试:
+ cat /home/errorcodes
+ for i in '`cat $Errors`'
+ grep 421 /home/eximlog
+ for i in '`cat $Errors`'
+ grep RP-001 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 421 /home/eximlog
+ for i in '`cat $Errors`'
+ grep RP-002 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 421 /home/eximlog
+ for i in '`cat $Errors`'
+ grep RP-003 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep SC-001 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep SC-002 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep SC-003 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep SC-004 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep DY-001 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep DY-002 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep DY-001 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep OU-001 /home/eximlog
+ for i in '`cat $Errors`'
+ grep 550 /home/eximlog
+ for i in '`cat $Errors`'
+ grep OU-002 /home/eximlog
为什么循环会分别读取“421”和“RP-001”等所有代码?并带来重复的结果。 Grep 应该将代码视为单个字符串,例如:"421 RP-001"
调试应该如下所示:
+ for i in 'cat $Errors'
+ grep 421 RP-001 /home/eximlog
︙
答案1
shell将执行分词在$(cat $Errors)
。这就是为什么你一次只能得到一个单词而不是一次一行。
你想要一个while read...
循环:
while read -r line; do
#Get Error Logs
grep "$line" /home/eximlog >> $Elogs
done <"$Errors"
read
是面向行的:它一次读取一行。
或者,这可能对您有用,并且根本不需要任何循环:
grep -f "$Errors" /home/eximlog
这些-f
选项指示grep
从文件中读取模式,每行一个模式。
另外,在我看来,您的错误代码模式是固定字符串而不是正则表达式。在这种情况下,为了避免令人不快的意外,请将选项添加-F
到grep
:
grep -Ff "$Errors" /home/eximlog