Bash's built-in string processing tool:
String slices:
${var:offset:number}
Takes a substring of a string;
Take the rightmost few characters of the string: ${var:-length}
Note: There must be a white space character after the colon;
To take a substring based on a pattern:
${var#*word}: Where word is the specified delimiter; function: From left to right, look for the var variable stored in the string, the first occurrence of the word separator, delete all characters from the beginning of the string between this delimiter;
${var##*word}: Where word is the specified delimiter; function: From left to right, look for the var variable stored in the string, the last occurrence of the word delimiter, delete all characters from the beginning of the string to this delimiter;
Mypath= "/etc/init.d/functions"
${mypath##*/}: Functions
${mypath#*/}: Etc/init.d/functions
${var%word*}: Where word is the specified delimiter; function: From right to left, look for the string stored in the var variable, the first occurrence of the word delimiter, and remove the delimiter to all characters between the trailing of the string;
${var%%word*}: Where word is the specified delimiter; function: From right to left, look for the last occurrence of the word delimiter in the string stored by the var variable, removing the delimiter to all characters between the trailing end of the string;
Mypath= "/etc/init.d/functions"
${mypath%/*}:/ETC/INIT.D
Url=http://www.magedu.com:80
${url##*:}
${url%%:*}
Find Replacements:
${var/pattern/substi}: Find the string represented by Var, the first string to be matched by PATTERN, and replace it with the string represented by Substi;
${var//pattern/substi}: Finds all strings that are matched by PATTERN in the string represented by Var, and replaces them all with the string represented by Substi;
${var/#PATTERN/substi}: Finds the string represented by Var, and replaces it with the string represented by the Substi by the string to which the beginning of the line is matched by the PATTERN;
${var/%pattern/substi}: Find the string represented by Var, the string that the end of the line is matched to by PATTERN, and replace it with the string represented by Substi;
Note: Use Glob style and wildcard characters in pattern;
Find Delete:
${var/pattern}: Finds the first match in the Var string in pattern mode and deletes it;
${var//patern}
${var/#PATTERN}
${var/%pattern}
Character-Case Conversions:
${var^^}: Converts all lowercase characters in var to uppercase;
${var,}: Converts all uppercase characters in var to lowercase;
Variable assignment:
${var:-value}: Returns value if the var variable is empty or not set, otherwise returns the var variable;
${var:=value}: If the var variable is empty or not set, then return value and assign value to the var variable; otherwise, the value of the Var variable is returned;
${var:+value}: Returns VALUE if the var variable is not empty;
${var:? Error_info}: If Var is empty, or not set, then return error_info as error; otherwise, return var value;
Write a script:
ping command to see whether all the hosts in the 172.16.1.1-172.16.67.1 range are online, the online display is up, not the line display down, respectively, the online host, and not the number of the line host;
Use for, respectively, while loop implementation.
#!/bin/bash#declare-i uphosts=0declare-i downhosts=0for i in {1..17}; Do if ping-w 1-c 1 172.16. $i. 1 &>/dev/null; Then echo "172.16. $i. 1 are up." Let uphosts+=1 else echo "172.16. $i. 1 are down." Let Downhosts+=1 Fidoneecho "up the hosts: $uphosts, down hosts: $downhosts."
Use function and while to implement:
#!/bin/bash#declare-i uphosts=0declare-i downhosts=0declare-i i=1hostping () {if ping-w 1-c 1 $ &>/dev/null; Then echo "are up." Return 0else echo "is down." return 1fi}while [$i-le 67]; Do hostping 172.16. $i. 1 [$?-eq 0] && let uphosts++ | | Let downhosts++ to I++doneecho "up hosts: $uphosts, down hosts: $downhosts."
Write a script that implements:
Be able to detect whether all the hosts in Class C, Class B, or a network are online;
#!/bin/bash#cping () {local i=1while [$i-le 5]; do if ping-w 1-c 1 $ $i &>/dev/null; then echo "$. $i is up "else echo" $ $i is down. " Fi let i++done}bping () {local j=0while [$j-le 5], do cping $ $j let j++done}aping () {local x=0while [$x-le 255]; Do bping $ $x let X++done} prompts the user to enter an IP address or network address, obtains its network, and scans its network segment;
This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1916084
Built-in string processing tool for Shell script programming