Knowledge learned from Shell scripts

Source: Internet
Author: User
Tags authy
The author FizerKhan is a Shell script fan. he is so obsessed with novel and interesting Shell scripts. recently, he met authy-ssh scripts to ease dual authentication on ssh servers.

Fizer Khan is a Shell script fan. he is so obsessed with novel and interesting Shell scripts. recently he met authy-ssh script, he has learned many useful and cool things to alleviate the problem of double 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, yellow indicates warning, and 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. }
  8. Function green (){
  9. Echo-e "$ GREEN $ * $ NORMAL"
  10. }
  11. Function yellow (){
  12. Echo-e "$ YELLOW $ * $ NORMAL"
  13. }
  14. # To print success
  15. Green "Task has been completed"
  16. # To print error
  17. Red "The configuration file does not exist"
  18. # To print warning
  19. 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. }
  7. # For any debug message
  8. 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
  10. }

Here, we use the which command to find the executable curl path. if the path is successful, the executable file exists, and vice versa. Set &>/dev/null in the output stream, and the error stream will display to/dev/null (this means there is nothing to print on the control panel ), some geeks suggest returning code directly by returning which.

Related Article

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.