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.
- How Many Exit Codes are there?
A. 10
B. 0-255
C. 45
D. 2
- How do you view an exit code after you complete a command?
A. exit code list
B. echo exit
C. echo $ecode
D. echo $?
- What exit code generally suggests the command was successful?
A. 10
B. 1
C. 3
D. 0
- What exit code generally means the command failed to execute?
A. 0
B. 2
C. 4
D. 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 …
Thank You,