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:
- NORMAL = $ (tput sgr0)
- GREEN = $ (tput setaf 2; tput bold)
- YELLOW = $ (tput setaf 3)
- RED = $ (tput setaf 1)
- Function red (){
- Echo-e "$ RED $ * $ NORMAL"
- }
- Function green (){
- Echo-e "$ GREEN $ * $ NORMAL"
- }
- Function yellow (){
- Echo-e "$ YELLOW $ * $ NORMAL"
- }
- # To print success
- Green "Task has been completed"
- # To print error
- Red "The configuration file does not exist"
- # To print warning
- 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
- Function debug (){
- If [[$ DEBUG]
- Then
- Echo ">>>$ *"
- Fi
- }
- # For any debug message
- Debug "Trying to find config file"
Some geeks also provide online debugging:
Shell code
- # From cool geeks at hacker news
- Function debug () {(DEBUG) & echo ">>> *";}
- Function debug () {["$ DEBUG"] & echo ">>> *";}
3. check whether a specific executable file exists?
Shell code
- OK = 0
- FAIL = 1
- Function require_curl (){
- Which curl &>/dev/null
- If [$? -Eq 0]
- Then
- Return $ OK
- Fi
- Return $ FAIL
- }
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.