Shell-shell example

Source: Internet
Author: User
Tags echo command ssh server

From: http://www.csdn.net/article/2013-08-15/2816581-What-I-learned-from-other-s-shell-scripts

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. colors your echo

In most cases, you want to output echo color. For example, Green indicates success, red indicates failure, and yellow indicates warning.

12345678910111213141516171819202122232425 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.

II. To print debug information (print debugging information)

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

123456789 function
debug() {     if
[[ $DEBUG ]]     then
        echo
">>> $*"    fi
}  # For any debug message
debug "Trying to find config file"

Some geeks also provide online debugging:

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

Iii. To check specific executable exists or not (check whether a specific executable file exists)

123456789101112 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 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 (this means there is nothing to print on the control panel ).

Some geeks suggest returning Code directly by returning which.

123 # From cool geeks at hacker news
function
require_curl() { which "curl"
&>/dev/null; }
function
require_curl() { which -s "curl"; }

4. To print usage of scripts (print the script used)


When I start writing shell scripts, I will use echo to print the scripts in use. When a large amount of text is in use, the echo command will become messy, so you can use Cat to set the command.

1234567891011121314151617 cat << EOF   Usage: myscript <command> <arguments>
  VERSION: 1.0   Available Commands       install - Install package
      uninstall - Uninstall package
      update - Update package
      list - List packages
  EOF

The

5. User configured value vs default value (User Configuration value vs default value)

Sometimes, if you do not set a value, the default value is used.

1 URL=${URL:-http://localhost:8080}

Check the URL environment variables. If it does not exist, you can specify it as localhost.

6. Check the length of the string to check the string length.

12345 if
[ ${#authy_api_key} != 32 ]
then   red
"you have entered a wrong API key"  return
$FAIL fi

Use $ {# variable_name} to define the length of the variable value.

VII. To read inputs with timeout (read input timeout)

12345678 READ_TIMEOUT=60 read -t "$READ_TIMEOUT"
input  # if you do not want quotes, then escape it
input=$(sed "s/[;\`\"\$\' ]//g"
<<< $input)  # For reading number, then you can escape other characters
input=$(sed 's/[^0-9]*//g' <<< $input)

8. To get directory name and file name (get directory name and file name)

12345678 # To find base directory
APP_ROOT=`dirname
"$0"`  # To find the file name
filename=`basename
"$filepath"`  # To find the file name without extension
filename=`basename
"$filepath"
.html`

From: fizerkhan

 

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.