See if the script syntax has errors:
Bash-n modify_suffix.sh
Track execution
Sh-x modify_suffix.sh AAA
1.${var} 2.$ (cmd) 3. () and {} 4.${var:-string},${var:+string},${var:=string},${var:?string} 5.$ ((exp)) 6.$ (Var%pattern), $ (var%%pattern), $ (Var#pattern), $ (var# #pattern)
The prototype of the variable in 1.Shell: ${var}
But when you want to show the variable value plus random characters (I use _AA here), it's going to go wrong.
This should be the prototype of the variable: ${var}, that is, a brace to limit the scope of the variable name
[[email protected] sh]# aa= ' Ajax ' [[email protected] sh]# echo $aaajax [[email protected] sh]# echo $aa _aa[[email protected] sh]# Echo ${aa}_aaajax_aa
Batch modify file name in a directory
[[email protected] ~]# cat modify_suffix.sh#!/bin/bashdst_path=$1for file in ' ls $dst _path ' do if [-D $1/$file] t Hen echo ' $ $1/$file ' elif [-F $1/$file] then mv $1/$file $1/${file}._mod else echo $1/${file} is U Nknow file type fidone;. /modify_suffix.sh ./f adds a. MoD to all file names under./F.
[Email protected] ~]# file= "modify_suffix.sh.tar.gz" [[email protected] ~]# echo "${file%%.*}" Modify_suffix[[email Protected] ~]# echo "${file%.*}" Modify_suffix.sh.tar[[email protected] ~]# echo "${file#*.}" Sh.tar.gz[[email protected] ~]# echo "${file##*.}" Gz
2.$ (CMD)
[[email protected] t]# ls1.txt 2.txt[[email protected] t]# echo $ (LS) 1.txt 2.txt
echo $ (LS) execution process the shell scans the command line once, finds the $ (CMD) structure, executes the cmd in $ (cmd) once, gets its standard output, and puts this output in the original command, the $ (LS) position in echo $ (LS), which is replaced by the $ (LS), Then execute the echo command as follows: Echo $ (LS) was replaced with Echo 1.txt 2.txt here to note that the error output of the command in $ (CMD) is not replaced, only the standard output is replaced
[[email protected] t]# var=$ (cat 3.txt) cat:3.txt: No file or directory [[email protected] t]# echo $var $var apparently empty.
3, a string of command execution () and {}
() and {} are executed on a string of commands, but differ:
Same point:
() and {} both put a string of commands inside the parentheses, and the commands are separated by a number;
Different points
() just re-open a sub-shell for a sequence of commands to execute, {} Execute on a string of commands in the current shell
() The last command can be used without a semicolon, {} The last command to use a semicolon
() The first command and the left parenthesis do not have spaces, there must be a space between the first command and the opening parenthesis of {}
The redirection of one of the commands inside the brackets () and {} only affects the command, but redirects outside the brackets affect all the commands in the parentheses
[Email protected] t]# Var=test[[email protected] t]# echo $vartest [[email protected] t]# (Var=notest;echo $var) notest[[e Mail protected] t]# echo $vartest [[email protected] t]# {Var=notest;echo $var;} Notest[[email protected] t]# echo $varnotest [[email protected] t]#
There must be a space between the first command and {} in {}, and the end must have;
{} has modified the $var value description in the current shell execution
[[email protected] t]# {var1=test1;var2=test2;echo $var 1>a;echo $var 2;} Test2[[email protected] t]# cat Atest1[[email protected] t]# {var1=test1;var2=test2;echo $var 1;echo $var 2;} >a[[email protected] t]# cat Atest1test2 Script instance ( echo "1" echo "2") | awk ' {print nr,$0} '
4. Several special replacement structures:
${var:-string},${var:+string},${var:=string},${var:?string}
(1)
${var:-string} and ${var:=string}
If the variable var is empty or undefined, use string in the command line to replace ${var:-string}
Otherwise the variable var is not empty, then replace ${var:-string} with the variable var value
[Email protected] ~]# echo $a [[email protected] ~]# echo ${a:-bcc}bcc[[email protected] ~]# echo $a [[email protected] ~]# A=ajax[[email protected] ~]# echo ${a:-bcc}ajax[[email protected] ~]# unset a[[email protected] ~]# echo $a [[Email Protec Ted] ~]# echo ${a:=bbc}bbc[[email protected] ~]# echo $ABBC
Found
${var:-string} and ${var:=string}
When the latter discovers that $var is empty, the string is assigned to Var
The latter is a common practice for assigning default values
(2) ${var:+string}
The rules are exactly the opposite of the above.
That is, when Var is not empty, it is replaced with a string, and if Var is empty, it is not replaced or substituted for the variable var value, that is, the null value
(3). ${var:?string}
Substitution rule: If VAR is not NULL, replace ${var:?string} with the value of the VAR variable
If the variable var is empty, the string is output to the standard error and exited from the script.
This attribute can be used to check whether the value of a variable is set
[[email protected] ~]# echo $a [[email protected] ~]# echo ${a:?bbc}-bash:a: Bbc[[email protected] ~]# a=ajax[[email prote CTED] ~]# echo ${a:?bbc}ajax[[email protected] ~]# a=ajax[[email protected] ~]# echo ${a:-' date '}ajax[[email protected] ~] # unset A[[email protected] ~]# echo ${a:-' Date '} February 21, 2017 Tuesday 10:13:46 cst[[email protected] ~]# echo ${a:-$ (date)}20 February 21, 17 Tuesday 10:13:59 cst[[email protected] ~]# b=bbc[[email protected] ~]# echo ${a:-$b}BBC
5.$ ((exp)) POSIX standard extended calculation
This calculation is a C-compliant operator, meaning that operators that conform to C are available in $ (exp), including the three-mesh operator
Note: This extended calculation is an integer-type calculation and does not support floating-point types and strings.
If the logical judgment, the expression exp is true is 1, false is 0
[[email protected] ~]# echo $ (3+2)-bash:3+2: Command not found [[email protected] ~]# echo $ ((3+2)) 5[[email protected] ~]# echo $ ((3.5 +2)-bash:3.5+2: syntax error: Invalid arithmetic operator (error symbol is ". 5+2") [[email protected] ~]# echo $ ((3>2)) 1[[email protected] ~]# echo $ ((3> ; 2? ' A ': ' B '))-bash:3>2? ' A ': ' B ': syntax error: expected operand (error symbol is "' A ': ' B '") [[email protected] ~]# echo $ ((3>2?A:B)) 0[[email protected] ~]# echo $ ((a=3+2)) 5[[ Email protected] ~]# echo $ ((a++)) 5[[email protected] ~]# echo $a 6
6. Four pattern matching replacement structures:
${var%pattern}${var%%pattern}${var#pattern}${var# #pattern}${var%pattern},${var%%pattern} to match ${var#pattern} from the right, ${var# #pattern} matches ${var%pattern} from the left, ${var#pattern} represents the shortest match, matches to stop, non-greedy ${var%%pattern},${var# #pattern} is the longest match
Only wildcard characters are used in pattern to have the longest and shortest match, otherwise there is no maximum minimum match
Pattern in the structure supports wildcard characters
* Denotes 0 or more arbitrary characters
? represents 0 or one arbitrary character
[...] Matches the characters inside the brackets
[!...] Indicates a mismatch between the characters in brackets
[[email protected] ~]# F=a.tar.gz[[email protected] ~]# echo ${f##*.} Gz[[email protected] ~]# echo ${f%%.*}a[[email protected] ~]# var=abcdccbbdaa[[email protected] ~]# echo ${var%%d*}abc[[e Mail protected] ~]# echo ${var%d*}abcdccbb[[email protected] ~]# echo ${var#*d}ccbbdaa[[email protected] ~]# echo ${var##* D}aa# finds that the output is the value of the part of the string that var strips out of pattern
Several syntax usages in the shell (), {}