简体   繁体   English

使用shell脚本执行mailx

[英]execute mailx using shell script

I am trying to run the below script. 我正在尝试运行以下脚本。

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

But I am getting this error. 但我收到此错误。

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

You can't have spaces around = : =周围不能有空格:

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

and should have been 并且应该是

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

or preferrably 或最好

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

Also

["$valCount" -gt 1]

should have been 本来应该

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

Sidenote 边注

You may use [ shellcheck ] to check your scripts. 您可以使用[shellcheck]检查脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM