Bash wrong output when using exit code

Hello everyone my name is ShintY and im fairly new to linux.

Recently i was trying to learn bash scripting with the help of learn linux tv tutorial. In the episode 6 i made a script almost same as the one shown in the tutorial and it worked for installing a package but when i wanted to see the results for non-existing package i got undesired results as you can see in the pic below.

Even though the exit code is 100, the output being typed is for exit code 0. Is this because the exit code contains 0 in the number 100? or is there some error in my code. Its not a very big deal but will be helpful for me to know what mistake i made.

Thank You

Welcome to the forum!

This looks like a wrong logic. Try using operators && and ||. And learn to put all variables in between curly brackets, you’ll thank me later.

sudo apt install -y ${pkg} >> pkg_installation.log && \
echo code $? && \
echo "${pkg} installed successfully, can be found at $(which ${pkg})" ||\
echo  "${pkr} installation failed"

I don’t like to edit, but had to fix a double quote missing in the code.

Thank you for your response.

From what i can see i should have used && (and) command after the sudo apt install line and included the $? in the same line.

from the code you have made i can see that you haven’t even used if statements which makes the script much smaller.

I will keep in mind of what you have said and try again.

The $? operator just gives you the exit code of the previous command (and thus only works once, immediately after it).

The thing is, if you run an “echo $?” then you will have a new “$?” when you run the if statement. Because “echo” has exited 0.

If you just comment the “echo” line, or if you save the “$?” in a variable, the script will work. Something like, immediately after sudo apt install line, aptexitcode=$?. Then you use echo code: ${aptexitcode} and if [ ${aptexitcode} -eq 0 ].

For simple scripts, I prefer && and || operators, because they guarantee they will work (due to code simplicity). But if I need something like: “do (this and this) or [(this and this) and (this or this)],” then I use if statements (or switches, they’re easier to deal with).

Okay i see what mistake i have done. That makes sense since echo is the new command i’ve used the $? gives output for echo and not the apt update command. Thanks for taking your time to help me out. It feels like i’ve asked a silly question.

There’s no silly question. I didn’t even found the root cause in the first comment, just given you the workaround (TBF, if you can keep the scripts simpler, it’s best you do that).

yes actually i was just trying the tutorial of if commands. Im very new to programming and i dont know any of the programming languages. I needed to learn bash scripts for some work and i just started few days ago. Thanks for the suggestion, i will keep in mind to keep my scripts as short as possible.

A bit of a nitpick, but short and simple are different. It can be a longer script, like a few hundred lines, but if the things it does are simple (particularly if you get into functions), then the scripts get better. You can have short, but complicated scripts, like doing a case switch statement, when all you need is a || operator.

Also, while I tell people to just do things how they know if it’s something that will only be used very few times, people can do inefficient stuff like pipe cat into grep, but I highly encourage that scripts get optimized (instead of cat, grep, cut and word count, just use awk and word count).

As an example:

cat test.txt
cat test.txt | grep SPA | cut -d ' ' -f 3 | wc -c


awk '/SPA/ {print $3}' test.txt  | wc -c

4 commands vs 2.

This is a bit more advanced. And this is what I’m talking about optimizing. But you can just go with the basics for right now and skip optimization, until you learn how to do things.

Sure, in many cases in life, there’s more than 1 way of doing things, but if you have the option to use an axe or a spoon to split wood, even though you could use the spoon, it’s best to use the axe, so to speak.

After reading through your reply, i can understand the difference between optimisation part and short script. Im still fairly new to this and my main objective is to edit a pre-existing script for automation of GROMACS so i can use the program in my way.
Thanks for helping me :slight_smile:

I am going to correct your code, not going to improve this script. Okay

The thing is you are evaluating the exit code of echo "exit code for the command is: $?" statement which is 0 because echo does it’s work of printing on the console.

One simple correction to your code is just put this echo statement inside if statement.

The whole code



sudo apt install -y $pkg >> pkg_installation.log

if [ $? -eq 0 ]
        echo "exit code for the command is: $?"
        echo "the program $pkg was installed successfully"
        echo "the package was installed at:"
which $pkg
        echo "$pkg failed to install. Log files of the failed installation is at: $pkf"