2021年2月18日 星期四

[Linux 文章收集] Linux bash exit status and how to set exit status in bash

 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:
  1. date  
  2. status=$?  
  3. echo "The date command exit status : ${status}"  
Linux exit status and the conditional/list constructs
A simple shell script to locate host name (findhost.sh)
  1. #!/bin/bash  
  2. # set var   
  3. FILE="/etc/hosts"  
  4.   
  5. # get host name   
  6. read -p "Enter a hostname : " hostname  
  7.   
  8. try to locate hostname/computer name in $FILE  
  9. grep -q -w "${hostname}" "${FILE}"  
  10.   
  11. # store exit status of grep  
  12. if found grep will return 0 exit status  
  13. if not found, grep will return a nonzero exit status  
  14. status=$?  
  15.   
  16. if test $status -eq 0  
  17. then  
  18.     echo "Host '$hostname' found in $FILE file."  
  19. else  
  20.     echo "Host '$hostname' not found in $FILE file."  
  21. fi  
How to use the && and || operators with exit codes
The syntax is:
  1. command && echo "success"  
  2. command || echo "failed"  
  3. command && echo "success" || echo "failed"  
If a dir named “/tmp/foo” not found create it:
  1. [ ! -d "/tmp/foo" ] && mkdir -p "/tmp/foo"  
For example, show usage syntax when filename not passed as the command line arg:
  1. #!/bin/Bash  
  2. _files="$@"  
  3.   
  4. ## fail safe ##  
  5. [[ "$_files" == "" ]] && { echo "Usage: $0 file1.png file2.png"; exit 1; }  
  6.   
  7. ## continue below ##  
Here is another shell script that shows usage:
  1. #!/bin/bash  
  2. set -e  
  3. I=~/bin/tags.deleted.410  
  4. O="/tmp/https.www.cyberciti.biz.410.url.conf"  
  5. t="$1"  
  6. [ ! -f "$I" ] && { echo "$I file not found."; exit 10; }  
  7. "$t" == "" ] && { echo "Usage: $0 number-of-urls-to-purge-from-$I"; exit 11; }  
  8.   
  9. >$O  
  10.   
  11. cat "$I" | sort | uniq | while read -r u  
  12. do  
  13.     uu="${u##https://www.cyberciti.biz}"  
  14.     echo "~^$uu 1;" >>"${O}"  
  15. done  
  16. echo "* Config file created at ${O} ..."  
  17. echo "* Installing ${O} file on utls-wp-mg-www ..."  
  18. ~/bin/install.py "${O}"  
  19. echo "* Send config to rest of cluster nodes ... "  
  20. ~/bin/install.py --sync --cluster --reload cbz-www  
  21. echo "* Building list purge urls for Cloudflare CDN ..."  
  22. sleep 1  
  23. url=""  
  24. while IFS= read -r u  
  25. do  
  26.         url="$url $u"  
  27. done <<<"$(tail -${t} ${I})"  
  28. "$url" != "" ] && ~/bin/cloudflare.purge.urls.sh "$url"  
  29. [ -f "$O" ] && rm -f "$O"  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...