2016-07-28 123 views
0

我試圖運行下面的腳本。使用shell腳本執行mailx

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 [email protected] [email protected]<<-EOF 
Hi , 
Validation has failed. Please check. 
EOF 
elif [ "$valCount" -gt 1 ] 
then 
    mailx -s "Validation pass" -r [email protected] [email protected]<<-EOF 
Hi Team, 
Validation success. 
EOF 
fi 

但我得到這個錯誤。

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

假設這是bash:您在賦值運算符周圍有空格。分別使用'val ='和'valCount ='。 – Robert

回答

1

你不能有空格周圍=

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

,應該已經

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

或preferrably

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 ]來檢查你的腳本。