Can you explains and provide us "Bourne Shell Exit Status Code" examples?
Each UNIX command returns a status when it terminates. If it is not successful, it returns a code which tells the shell to print an error message. You can use the
exit command to leave a shell program with a certain exit status.
Typical Values Of Exit Status
- 0 Success.
- 1 A built-in command failure.
- 2 A syntax error has occurred.
- 3 Signal received that is not trapped.
How Do I Print Exit Status?
Type the command:
$ ls
To print its exit status type the command:
$ echo $?
Try a few more examples:
date1
echo $?
date
echo $?
cat /etc/passwd
echo $?
To store exit status of the last executed command to a shell variable called status, enter:
command1
status=$?
You can use exit status with test command or if command too.
#!/bin/sh user="$1" if grep "$user" /etc/passwd; then echo "$user has an account" else echo "$user doesn't have an account" fi
Run it as follows:
./script-name vivek
The grep command is used as condition but it can actually be any command. If it returns a zero exit status, the condition is true; otherwise, it is false. In this example, the while loop executes given commands as long as condition is true. Again, condition can be any command, and is true if the command exits with a zero exit status.
while condition; do commands done
Here is another simple example:
#!/bin/sh x=0 while [ $x != 3 ] do let x=x+1 echo $x done
Here is another example:
#!/bin/sh
while [ -r "$1" ]
do
cat $1 >> output
shift
doneRun it as follows:./script-name file1 file2 fil3
cat output You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 0 comments… add one now }