I am a beginner with bash so I apologize if this is a basic question or has syntax issues but I couldn't find the answer anywhere. I want to run a command, and if it fails, check if the error message matches a certain string. Here is what I have:
err_msg=`./command input.txt 2>&1`
if [$? -eq 1]
then
if [err_msg -eq "Error: timeout"]
then
#do something
fi
fi
Is this the correct way to do this? If I'm using the first line to define the error message err_msg, will $? still store the pass/fail status of the command? Also, what will happen to if the command passes? Will err_msg just be blank?
[and]: these are builtins and need to have a space around them to be recognized as such. 2. Don't use-eqto test for equality of strings. Use=instead. Re. your question: yes,$?will expand to the return code of the command.err_msg. You also need to quote this expansion:"$err_msg", unless you use the more robust keyword[[.