I know I can do this...
if diff -q $f1 $f2
then
echo "they're the same"
else
echo "they're different"
fi
But what if I want to negate the condition that I'm checking? i.e. something like this (which obviously doesn't work)
if not diff -q $f1 $f2
then
echo "they're different"
else
echo "they're the same"
fi
I could do something like this...
diff -q $f1 $f2
if [[ $? > 0 ]]
then
echo "they're different"
else
echo "they're the same"
fi
Where I check whether the exit status of the previous command is greater than 0. But this feels a bit awkward. Is there a more idiomatic way to do this?
If you are looking to suppress or hide all the output of a bash shell script from Linux command line as well as from the crontab then you can simply redirect all the output to a file known as /dev/null . This file is known as Black Hole which will engulf everything you give without complaining.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.
To display the exit code for the last command you ran on the command line, use the following command: $ echo $?
if ! diff -q "$f1" "$f2"; then ...
If you want to negate, you are looking for !
:
if ! diff -q $f1 $f2; then
echo "they're different"
else
echo "they're the same"
fi
or (simplty reverse the if/else actions) :
if diff -q $f1 $f2; then
echo "they're the same"
else
echo "they're different"
fi
Or also, try doing this using cmp
:
if cmp &>/dev/null $f1 $f2; then
echo "$f1 $f2 are the same"
else
echo >&2 "$f1 $f2 are NOT the same"
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With