Quiz: Using Exit Codes in Bash - A test for You

Here is a small test I found on the internet with a few modifications. If you dare, take the test and then run my bash script to see how you did.

  1. How Many Exit Codes are there?

A. 10
B. 0-255
C. 45
D. 2

  1. How do you view an exit code after you complete a command?

A. exit code list
B. echo exit
C. echo $ecode
D. echo $?

  1. What exit code generally suggests the command was successful?

A. 10
B. 1
C. 3
D. 0

  1. What exit code generally means the command failed to execute?

A. 0
B. 2
C. 4
D. 1

  1. How can you execute the command ls /etc and see the exit code on one line?

A. ls /etc;echo $?
B. ls /etc&&echo $?
C. ls /etc/||echo $?
D. ls /etc echo $?

Your Bash Script Ranking based on your score:

5 out of 5 or 100%: You are a bash master. Step to the head of the class.
4 out of 5 or 80%: That’s OK, You are almost there. Just a little more study is required.
3 out of 5 or 60%: Have you considered running Amarok or Banshee today? These are really nice media players.
2 out of 5 or 40%: Humm, not sure, but bash scripting may not be your thing. I hear they don’t use scripting in Ubuntu.
1 out of 5 or 20%: I don’t know, but its better to take the test sober as a general rule.
0 out of 5 or 0%: I hear Windows needs just a few more users to make a trillion dollars.

To find the answers, you can create the following script and then run it to see what you get.

#!/bin/bash

#: Title       : ExitCodeTest
#: Date Created: Sat Oct 22 13:52:06 CDT 2011
#: Last Edit   : Sat Oct 22 13:52:06 CDT 2011
#: Author      : James D. McDaniel
#: Version     : 1.00
#: Description : Print Quiz Answers
#: Options     : None today

TITLE="Quiz: Using Exit Codes - The Answers"

declare -a answer

answers=5

answer 1 ]="1.A 1.B 1.C 1.D 4"
answer 2 ]="2.A 2.B 2.C 2.D 8"
answer 3 ]="3.A 2.B 3.C 3.D 10"
answer 4 ]="4.A 3.B 4.C 4.D 12"
answer 5 ]="5.A 4.B 5.C 5.D 11"


clear
echo $TITLE
echo

inc=1
while  $inc -le $answers ] ; do
  seed=$(echo ${answer$inc]} | awk '{print $5}')
  let code=2*inc
  let code=seed-code
  echo -n "Answer to Question $inc is: "
  echo ${answer$inc]} | awk '{print $'$code' }'
  let inc=inc+1
done

echo
echo "Thanks for taking the time to run this script!"
echo

exit 0

# End Of Script

Copy and past the text above into your favorite text edit and save it as the bash script ExitCodeTest in your ~/bin folder, (/home/username/bin/ExitCodeTest):

To use the script, it must be marked executable. Open a terminal session and run the command:

chmod +x ~/bin/ExitCodeTest

To get the results, open a terminal session and run the command:


ExitCodeTest

That is all … As always, I want to hear any comments that you may have.

Thank You,

Blogs: asroot : Bash : Packet Filter : C.F.U. : GPU’s : fewrup : F.S.M. : H.I. : nVIDIA : LNVHW : N.S.F. : S.A.K.C. : MMCHECK
S.A.S.I. : S.C.L.U. : S.G.T.B. : S.K.I.M. : S.L.A.V.E. : S.L.R.C. : S.T.A.R.T. : S.U.F.F. : SYSEdit : systemd : YaST Power

That was fun, although I’ve got only three answers right :slight_smile:

That was fun, although I’ve got only three answers right :slight_smile:

Glad you liked the test. So, do you like Amarok and Banshee as good media players?

Thank You,

No, I use xmms2. :slight_smile:

No, I use xmms2. :slight_smile:

It is just a joke riderplus. Quoting myself “3 out of 5 or 60%: Have you considered running Amarok or Banshee today? These are really nice media players.” Of course, some may say that based on what I think is funny, I should not quit my day job. :slight_smile:

Thank You,