Source From Here
Preface
Can you explain bash exit status code? How do I set bash exit status in my Linux shell scripts?
Each Linux or Unix command returns a status when it terminates normally or abnormally. You can use value of exit status in the shell script to display an error message or run commands. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sysadmins.
More on Linux bash shell exit status codes
1. Every Linux or Unix command executed by the shell script or user, has an exit status.
2. The exit status is an integer number.
3. For the bash shell’s purposes, a command which exits with a zero (0) exit status has succeeded.
4. A non-zero (1-255) exit status indicates failure.
5. If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.
6. All of the Bash builtins return exit status of zero if they succeed and a non-zero status on failure.
How to store the exit status of the command in a shell variable
You can use special shell variable called $? to get the exit status of the previously executed command. Then Assign $? to a shell variable. The syntax is:
Linux exit status and the conditional/list constructs
A simple shell script to locate host name (findhost.sh)
How to use the && and || operators with exit codes
The syntax is:
If a dir named “/tmp/foo” not found create it:
For example, show usage syntax when filename not passed as the command line arg:
Here is another shell script that shows usage:
Can you explain bash exit status code? How do I set bash exit status in my Linux shell scripts?
Each Linux or Unix command returns a status when it terminates normally or abnormally. You can use value of exit status in the shell script to display an error message or run commands. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sysadmins.
More on Linux bash shell exit status codes
1. Every Linux or Unix command executed by the shell script or user, has an exit status.
2. The exit status is an integer number.
3. For the bash shell’s purposes, a command which exits with a zero (0) exit status has succeeded.
4. A non-zero (1-255) exit status indicates failure.
5. If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.
6. All of the Bash builtins return exit status of zero if they succeed and a non-zero status on failure.
How to store the exit status of the command in a shell variable
You can use special shell variable called $? to get the exit status of the previously executed command. Then Assign $? to a shell variable. The syntax is:
- date
- status=$?
- echo "The date command exit status : ${status}"
A simple shell script to locate host name (findhost.sh)
- #!/bin/bash
- # set var
- FILE="/etc/hosts"
- # get host name
- read -p "Enter a hostname : " hostname
- # try to locate hostname/computer name in $FILE
- grep -q -w "${hostname}" "${FILE}"
- # store exit status of grep
- # if found grep will return 0 exit status
- # if not found, grep will return a nonzero exit status
- status=$?
- if test $status -eq 0
- then
- echo "Host '$hostname' found in $FILE file."
- else
- echo "Host '$hostname' not found in $FILE file."
- fi
The syntax is:
- command && echo "success"
- command || echo "failed"
- command && echo "success" || echo "failed"
- [ ! -d "/tmp/foo" ] && mkdir -p "/tmp/foo"
- #!/bin/Bash
- _files="$@"
- ## fail safe ##
- [[ "$_files" == "" ]] && { echo "Usage: $0 file1.png file2.png"; exit 1; }
- ## continue below ##
- #!/bin/bash
- set -e
- I=~/bin/tags.deleted.410
- O="/tmp/https.www.cyberciti.biz.410.url.conf"
- t="$1"
- [ ! -f "$I" ] && { echo "$I file not found."; exit 10; }
- [ "$t" == "" ] && { echo "Usage: $0 number-of-urls-to-purge-from-$I"; exit 11; }
- >$O
- cat "$I" | sort | uniq | while read -r u
- do
- uu="${u##https://www.cyberciti.biz}"
- echo "~^$uu 1;" >>"${O}"
- done
- echo "* Config file created at ${O} ..."
- echo "* Installing ${O} file on utls-wp-mg-www ..."
- ~/bin/install.py "${O}"
- echo "* Send config to rest of cluster nodes ... "
- ~/bin/install.py --sync --cluster --reload cbz-www
- echo "* Building list purge urls for Cloudflare CDN ..."
- sleep 1
- url=""
- while IFS= read -r u
- do
- url="$url $u"
- done <<<"$(tail -${t} ${I})"
- [ "$url" != "" ] && ~/bin/cloudflare.purge.urls.sh "$url"
- [ -f "$O" ] && rm -f "$O"
沒有留言:
張貼留言