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//pattern}
${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;
Exercise: Write a script to complete the following functions
(1) Prompt the user to enter the name of an executable command;
(2) Get a list of all library files that this command relies on;
(3) Copy the command to a target directory (for example,/mnt/sysroot, which treats this directory as the root) in the corresponding path
Bash,/bin/bash ==>/mnt/sysroot/bin/bash
Useradd,/usr/sbin/useradd ==>/mnt/sysroot/usr/sbin/useradd
(4) Copy all the library files that this command relies on to the corresponding path under the target directory;
/lib64/ld-linux-x8664.so.2 ==>/mnt/sysroot/lib64/ld-linux-x8664.so.2
Further:
After each copy completes a command, do not exit, but prompt the user to continue to enter the other commands to copy, and repeat the function described above, until the user input "quit" Exit script;
Built-in string processing tool for Shell programming