See if the script syntax has errors:
Bash-n modify_suffix.sh
Track execution
Sh-x modify_suffix.sh AAA
1234. ${var:-string},${var:+string },${var:=string},${var:? String56. $ (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
SH] # aa='ajax'shechoshecho shecho ${aa}_aaajax_aa
Batch modify file name in a directory
[Email protected] ~]#CatModify_suffix.SH#!/bin/Bashdst_path=$1 for file inch`ls$dst _path ' Do if[-D $1/$file ] Then Echo`$0$1/$file` elif[-F $1/$file ] Then MV$1/$file$1/${file}._modElse Echo$1/${file} is Unknowfiletypefi Done;./modify_suffix.SH./F will.All file names under/F are added. MoD
[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)
ls 1. txt 2Echo $ (ls)12. txt
echo $ (LS) execution process
The shell scans the command line once, finds the $ (CMD) structure, executes the cmd in $ (cmd) once, and gets its standard output,
This output is then placed in the original command, the $ (LS) position in echo $ (LS), that is, replace the $ (LS), and then execute the echo command
As follows:
echo $ (LS) is replaced with Echo 1.txt 2.txt
Note here that the error output of the command in $ (CMD) is not replaced, only the standard output is replaced
[Email protected] t]# var=$ (cat3.txt)cat3echo $var $var is obviously 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 commands in () do not have to have spaces with parentheses, 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=echo $vartest [[email protected] t]# (var=notest; echoecho $vartest [[email protected] t]# {var=notest; echoecho $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]#CatAtest1[[email protected] t]# {var1=test1;var2=test2;Echo$var 1;Echo$var 2;} >A[[email protected] t]#CatAtest1test2 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:-< span style= "color: #000000" >bcc}bcc[[ Email protected] ~]# echo $a [[email protected] ~]# a=ajax[[email protected] ~]# echo ${a:-< span style=" color: #000000 ">bcc}ajax[[email protected] ~]# unset a[[email protected] ~]# echo $a [[[email protected] ~]# echo ${a:=bbc}bbc[[email protected] ~]# echo $ABBC
Found ${var:-string} and ${var:=string} when the latter found that the $var was empty, the string was 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
[Email protected] ~]# a=echoecho ${a:+echo~ echoecho ${a:+
(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 protected]~]#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 TuesdayTen: -: $Cst[[email protected]~]#Echo${a:-$ (Date)}2017 February 21 TuesdayTen: -: -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$a6
6. Four pattern matching replacement structures:
${var%pattern}${varpattern}${var#pattern}${var# #pattern}${var%pattern},${var%%Pattern} Match ${var#pattern},${var# from the right #pattern} match ${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.tarecho ${f##*echo ${f%%.*~]# var= echo ${var%%d*echo ${var%d*echo ${var#* echo ${var##*d}aa# discovers that the output is the value of the part of the string where var strips the pattern
Several syntax usages in the shell (), {}