I have a unix script that occasionally errors out with the message "test: argument expected". The following line of code is the only if statement in the script
if [ -z `gr开发者_高级运维ep ">Success<" $OUTFILE` ]
The $OUTFILE is a file created when the script starts to run. The script calls a web service that writes the output to the OUTFILE. If the outfile has "Success" in it, then it means that the web service completed successfully.
This script is called every 10 minutes and the logic above works perfectly fine for most of the cases. But on occasionally, the script errors out with the test argument expected error and I am unable to figure out the reason for this error. Has anyone else faced a similar problem? It would be great if someone can provide pointers to this issue.
Thanks.
You didn't wrap the `command`
string in double quotes, so if the string isn't found, the command reduces to if [ -z ]
which is an error (missing parameter). (It expands to nothing, not to an empty parameter.) And it's doing things the hard way.
if grep ">Success<" $OUTFILE
You may want the -s
and (if GNU grep
) -q
options on grep
.
You should quote the argument to -z
!
if [ -z "$(grep ">Success<" $OUTFILE)" ]
Of course, to test whether >Success<
is found, there's an even easier way:
if grep -q ">Success<" $OUTFILE
精彩评论