Knowledge learned from Shell scripts

Source: Internet
Author: User

The author Fizer Khan is a Shell script fan who is so obsessed with novel and interesting things about Shell scripts. He recently encountered an authy-ssh script. He learned a lot of useful and cool things to alleviate the problem of dual authentication on the ssh server. He wants to share this with you.

1. color the output

In most cases, you want to output results with colors. For example, Green indicates success, red indicates failure, and yellow indicates warning.

Shell code

 
 
  1. NORMAL=$(tput sgr0) 
  2. GREEN=$(tput setaf 2; tput bold) 
  3. YELLOW=$(tput setaf 3) 
  4. RED=$(tput setaf 1) 
  5. function red() { 
  6. echo -e "$RED$*$NORMAL" 
  7. function green() { 
  8. echo -e "$GREEN$*$NORMAL" 
  9. function yellow() { 
  10. echo -e "$YELLOW$*$NORMAL" 
  11. # To print success 
  12. green "Task has been completed" 
  13. # To print error 
  14. red "The configuration file does not exist" 
  15. # To print warning 
  16. yellow "You have to use higher version." 

Here, tput is used to set the color and text and reset to the normal color. For more information about tput, see prompt-color-using-tput.

2. Output debugging information

To output debugging information, you only need to debug and set the flag.

Shell code

 
 
  1. function debug() { 
  2. if [[ $DEBUG ]] 
  3. then 
  4. echo ">>> $*" 
  5. fi 
  6. # For any debug message 
  7. debug "Trying to find config file" 

Some geeks also provide online debugging:

Shell code

 
 
  1. # From cool geeks at hacker news 
  2. function debug() { ((DEBUG)) && echo ">>> $*"; } 
  3. function debug() { [ "$DEBUG" ] && echo ">>> $*"; } 

3. check whether a specific executable file exists?

Shell code

 
 
  1. OK=0 
  2. FAIL=1 
  3. function require_curl() { 
  4. which curl &>/dev/null 
  5. if [ $? -eq 0 ] 
  6. then 
  7. return $OK 
  8. fi 
  9. return $FAIL 

Here we use the which command to find the executable curl path. If the execution succeeds, the executable file exists, and vice versa. Set &>/dev/null in the output stream, and the error stream will display to/dev/null, which means there is nothing to print on the control panel ).

Some geeks suggest returning Code directly by returning which.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.