使用shell脚本执行mailx

时间:2016-07-28 03:17:28

标签: shell

我正在尝试运行以下脚本。

val = `wc -l /home/validate.bad | awk '{print $1}' | tail -n1`
valCount = `wc -l /home/validation.txt | awk '{print $1}'`
if [ "$val" -gt 1 ] && ["$valCount" -gt 1]
then
    mailx -s "Validation failed" -r xyz@abc.com xyz@abc.com<<-EOF
Hi ,
Validation has failed. Please check.
EOF
elif [ "$valCount" -gt 1 ]
then
    mailx -s "Validation pass" -r xyz@abc.com xyz@abc.com<<-EOF
Hi Team,
Validation success.
EOF
fi

但我收到了这个错误。

Error:
val: comand not found
valCount: command not found
line 3[: : integer expression expected

1 个答案:

答案 0 :(得分:1)

=周围不能有空格:

val = `wc -l /home/validate.bad | awk '{print $1}' | t` # wrong

应该是

val=`wc -l /home/validate.bad | awk '{print $1}' | t`

或者最好

val=$(wc -l </home/validate.bad) 
#`..` is legacy , $() supports nesting, one good reason to go for it
# You use awk and tail uselessly 

另外

["$valCount" -gt 1]

应该是

[ "$valCount" -gt 1 ] # mind the spaces for the test constructie
# [spaceSTUFFspace] is the correct form

<强>旁注

您可以使用[ shellcheck ]检查脚本。