As I was explaining in the previous lesson we will be taking what appears to be a complicated script like this one below and breaking the legos apart to explain what they really do for us.
#!/bin/bash check_exit_status() { if [ $? -eq 0 ] then echo echo "Success" echo else echo echo "[ERROR] Process Failed!" echo read -p "The last command exited with an error. Exit script? (yes/no) " answer if [ "$answer" == "yes" ] then exit 1 fi fi } greeting() { echo echo "Hello, $USER. Let's update this system." echo } update() { sudo apt-get update; check_exit_status sudo apt-get upgrade -y; check_exit_status sudo apt-get dist-upgrade -y; check_exit_status } housekeeping() { sudo apt-get autoremove -y; check_exit_status sudo apt-get autoclean -y; check_exit_status sudo updatedb; check_exit_status } leave() { echo echo "--------------------" echo "- Update Complete! -" echo "--------------------" echo exit } greeting update housekeeping leave
We will begin at the top of the script, because scripts go in order of appearance top to bottom if there is an issue between the top and the bottom there will be an error code presented.
#!/bin/bash check_exit_status() { if [ $? -eq 0 ] then echo echo "Success" echo else echo echo "[ERROR] Process Failed!" echo read -p "The last command exited with an error. Exit script? (yes/no) " answer if [ "$answer" == "yes" ] then exit 1 fi fi }
We have defined the check_exit_status() function using the proper naming convention of a function and if you have studied coding at least a little you may have noticed that none of the functions start with a number we is very important to note as you move further along in scripting. Immediately following the declaration of said function we have an if statement which will be checking the exit status so the $? will contain the exit status of the last command executed. You may also notice the fi at the bottom of the script
if [ expression ] <<--beginning to test the expression then Statement(s) to be executed if expression is true. else Statement(s) to be executed if expression is false. fi <<---this is how to close an if statement.
Dude! What the crap? That script has a whole bunch of stuff right the in the middle.
check_exit_status() { if [ $? -eq 0 ] then echo echo "Success" echo else echo echo "[ERROR] Process Failed!" echo read -p "The last command exited with an error. Exit script? (yes/no) " answer
The "echo" is telling the terminal to display whatever information is next to it, you'll notice that the first is blank and this is mainly for the look of the script so it reads well on the terminal to the user. the else displays the process or processes failure then the "read" with the -p option will prompt you for an answer. If you were to type a no then it would exit with a 0 due to this next portion.
if [ "$answer" == "yes" ] then exit 1 fi fi
However, should you type a yes it would exit with a code 1 which means that one of the commands returned an error when attempting to complete. Would indicate when you're/have attempted to kill the Dock process, but there wasn't a process running found by that name when the command executed.
So, that is the first function/method in this script and as promised I will continue to break down this script. Happy learning.
Comment here
You must be logged in to post a comment.