I'm refactoring my scripts and am trying to convert all if cmd; then fi;
statements to if [[cmd]]; then fi
. I know how to convert it by invoking the command in previous line and using its exit code in current line, but I want to know how to do it inline.
Example:
# I have this
if grep "some-string" some_file.txt > /dev/null; then
echo "non zero exit status"
fi
# I know how to do this
$(grep "some-string" some_file.txt > /dev/null)
if [[ $? -ne 0 ]]; then
echo "non zero exit status"
fi
# I want to know how to do something like this?
if [[ grep "some-string" some_file > dev/null ]]; then
echo "non zero exit status"
fi
Please let me know how to do inline execution and check of status code for a command inside if [[...]]; then fi
statement?
I'm referencing bash from this and trying to follow styling from this