Linux—shell$() and ``; segmentation, replacement and interception of strings

Reference link:

  1. https://blog.csdn.net/number_0_0/article/details/73291182
  2. https://blog.csdn.net/u013402321/article/details/80333272

$()And ``
they are the same effect, used for command substitution. It is recommended to use$()

[kafka@kafka001 kafka_2.11-0.8.2.2]$ echo $(ls)
bin config libs LICENSE log logs NOTICE
[kafka@kafka001 kafka_2.11-0.8.2.2]$ ls
bin  config  libs  LICENSE  log  logs  NOTICE
[kafka@kafka001 kafka_2.11-0.8.2.2]$ `ls`
-bash: bin: command not found
[kafka@kafka001 kafka_2.11-0.8.2.2]$ echo `ls`
bin config libs LICENSE log logs NOTICE
[kafka@kafka001 kafka_2.11-0.8.2.2]$ 


${}Used for variable substitution, var and var andV A R & lt and {var} the same meaning, but more accurate $ {var} indicates variable names. Conducive to splicing

[dps@ccod131 bak]$ a=123 && echo $ab

[dps@ccod131 bak]$ a=123 && echo ${a}b
123b

#%*

[dps@ccod131 bak]$ file=file1/file2/file3/file.txt && echo ${file##/}
file1/file2/file3/file.txt

# 实际有效的写法
[dps@ccod131 bak]$ file=file1/file2/file3/file.txt && echo ${file##*/}
file.txt
[dps@ccod131 bak]$ file=file1/file2/file3/file.txt && echo ${file##/*}
file1/file2/file3/file.txt

[dps@ccod131 bak]$ file=file1/file2/file3/file.txt && echo ${file%%/}
file1/file2/file3/file.txt

#实际有效的写法
[dps@ccod131 bak]$ file=file1/file2/file3/file.txt && echo ${file%%/*}
file1
[dps@ccod131 bak]$ file=file1/file2/file3/file.txt && echo ${file%%*/}
file1/file2/file3/file.txt

As can be seen from the above example:

  • In the above example, the separator is /;
  1. #Counting from the left, after reaching the separator, divide the entire field into two parts; (# is on the left side of the keyboard, left)
  2. %Counting from the right, after reaching the separator, divide the entire field into two; (% is on the right side of the keyboard, right)
  3. *Match all the places that have been walked and delete; (so in #, it is valid on the left of the separator; in %, it is effective on the right of the separator)
  4. For #,%a single character is the minimum match, two characters are the maximum match (# means starting from the first separator on the left, ## means starting from the last separator on the left;% means starting from the first separator on the right Split, %% means split from the last separator on the right;)

Actual use:
remove the same suffix of the file

#查看符合条件的文件
dps@ccod131 bak]$ ls *.txt
aa.txt  awk.txt  bb.txt  eof.txt  err.txt  file.txt  test11.txt

#批量给这些文件添加后缀
[dps@ccod131 bak]$ ls *.txt | xargs -i mv {
    
    } {
    
    }.bak
[dps@ccod131 bak]$ ls *.bak
aa.txt.bak  awk.txt.bak  bb.txt.bak  eof.txt.bak  err.txt.bak  file.txt.bak  test11.txt.bak

#批量去除后缀的执行效果
[dps@ccod131 bak]$ bash for_string.sh 
aa.txt awk.txt bb.txt eof.txt err.txt file.txt test11.txt

#批量去除后缀的代码
[dps@ccod131 bak]$ cat for_string.sh 
#!/bin/bash 

for file in $(ls *.bak)
  do
    mv $file ${file%.*}
  done
echo $(ls *.txt*)


${string:position:length}

  • Indicates that the string is intercepted from position position to the right, and the interception length is length;
  • When length is empty, that is, in ${string:position}this form, it means that the string is intercepted from position until the end
  • When position is equal 0-1, it means that the string is intercepted from the first character from the bottom;
[dps@ccod131 bak]$ 
[dps@ccod131 bak]$ abc=1234567890 && echo ${abc:0:3}
123
[dps@ccod131 bak]$ abc=1234567890 && echo ${abc:3:3}
456
[dps@ccod131 bak]$ abc=1234567890 && echo ${abc:0-3:3}
890
[dps@ccod131 bak]$ abc=1234567890 && echo ${abc:0-9:3}
234
[dps@ccod131 bak]$ abc=1234567890 && echo ${abc:5}
67890


${#var}Get variable length

[dps@ccod131 bak]$ abc=1234567890 && echo ${#abc}
10


replace

command Description
${string/substring/replacement} Use replacement, to replace the first matching replacement, to replace the first matchingreplacement,By substituting for the first a th matching with the substring
${string//substring/replacement} Use replacement, instead of all matching replacements, instead of all matchingreplacement,Substituting for the has matched with the substring
${string/#substring/replacement} If the prefix of string matches the prefix of stringS T R & lt I n- G of the front prefix match with substring, then byreplacement in place to match the replacement to replace the matchedR & lt E P L A C E m E n- T by substituting for the matching feature to the substring
${string/%substring/replacement} If the suffix of string matches the suffix of stringS T R & lt I n- G of the conjugate match with substring, then byreplacement in place to match the replacement to replace the matchedR & lt E P L A C E m E n- T by substituting for the matching feature to the substring
dps@ccod131 bak]$ var=abc1abc2abc3 && echo ${var/abc/ABC}
ABC1abc2abc3
[dps@ccod131 bak]$ var=abc1abc2abc3 && echo ${var//abc/ABC}
ABC1ABC2ABC3
[dps@ccod131 bak]$ var=abc1abc2abc3 && echo ${var/#abc/ABC}
ABC1abc2abc3
[dps@ccod131 bak]$ var=abc1abc2abc3 && echo ${var/%abc/ABC}
abc1abc2abc3
[dps@ccod131 bak]$ var=abc1abc2abc3 && echo ${var/%abc3/ABC}
abc1abc2ABC


shift

  • Move the command parameter to the left, the default is 1 bit. Can add parameters
  • Mostly used in startup scripts;

Reference link: https://blog.csdn.net/sdustliyang/article/details/9731277

[dps@ccod131 bak]$ cat shift.sh 
#!/bin/bash 

until [ $# -eq 0 ]
do
echo "当前第一的参数值${1},参数总数$#"
shift
done
[dps@ccod131 bak]$ ./shift.sh 1 2 3 4 5 6
当前第一的参数值1,参数总数6
当前第一的参数值2,参数总数5
当前第一的参数值3,参数总数4
当前第一的参数值4,参数总数3
当前第一的参数值5,参数总数2
当前第一的参数值6,参数总数1


[dps@ccod131 bak]$ cat shift.sh 
#!/bin/bash 

until [ $# -eq 0 ]
do
echo "当前第一的参数值${1},参数总数$#"
shift 3
done
[dps@ccod131 bak]$ ./shift.sh 1 2 3 4 5 6
当前第一的参数值1,参数总数6
当前第一的参数值4,参数总数3

Guess you like

Origin blog.csdn.net/jjt_zaj/article/details/113056275