What message should be displayed on ubuntu command line to show that a successful upgrade has been completed (via command)?
3 Answers
CLI programs normally use exit codes to specify if the command exited with success or something happened in the middle, apt-get
is no exception.
That means that instead of
sudo apt-get install foo
(being foo some package that you want to upgrade manually of even install) you can use
apt-get install foo && echo "Great success!" || echo "OMG do something! Dooooooooomed!"
If the first command fails somewhere with an error, the first one will be executed, else the second one.
Of course you can replace the commands I used in the example with something more practical, such as, eg: mail someone or send a warning to everyone logged in to the computer. Get creative.
Try the below command to check whether the previous command succeeded or not,
echo $?
If the output of the above command displays 0
, means your previous command was succeeded.If it displays any number other than 0, your previous command was not succeeded.
[ $(echo $?) == 0 ] && echo "Your previous command was succeeded" || echo "Your previous command was not succeeded"
OR
((! $?)) && echo "Your previous command was succeeded" || echo "Your previous command was not succeeded"
-
-
Moreover, you probably want either
=
orbash
conditional command[[
if you want to use==
.– devnullMar 7, 2014 at 12:49
After running
apt-get upgrade
run this:
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
fi
The exit status is stored in “$?” internal variable.
the exit status value varies from 0 to 255.
0 Successful execution of command
0 > command fails because of an error during expansion or redirection, the exit status is greater than zero.
2 Incorrect command usage
126 Command found but not executable
127 Command not found
-
1When i answer your answer was just
echo $?
. I provide another syntax and some explanation. It's better to teach how to fish to just give a fish– MaythuxMar 7, 2014 at 12:03