Shell-7.shell Use of strings
1. View the length of the string
${#var} to calculate the length of the variable value:
[Email protected] ~]# var= ' Yuanji '
[Email protected] ~]# echo ${#var}
6
2. Further discussion of single quotation marks, double quotation marks Difference
1> Single quotation mark
The variable name, or any other text, will be passed out without modification.
Single quotation marks cannot appear in single quote strings (not after using escape characters for single quotes).
2> Double Quotes
The variable name, or any other text, will be passed out without modification.
(You can have variable names in double quotes, and you can have escape characters in double quotes)
3. Compare echo $ variable name and echo "$ variable name"
Instance
[[email protected] html]# mystr= "A B F exit"
[Email protected] html]# echo $mystr
a B F exit
[Email protected] html]# echo "$mystr"
a B F exit
Can see
echo $ variable name differs from echo "$ variable name"
4. Use "$ variable name" or ${variable name}, try not to use the $ variable name, in many places will be error or unexpected results
Instance
[Email protected] home]# name= "Yuan Ji"
[[Email protected] home]# if [-Z $name];then echo "empty"; fi
-bash: [: yuan:binary operator expected
-Z: Determine if the established variable has a value
Change it to normal.
If [-z] $name "];then echo" empty "; fi
When name= "Yuan Ji", [-Z $name] becomes [-Z yuan Ji], then-Z has two parameters, and-Z only need one parameter. At this point the shell interpreter will error: [: Too many arguments and other similar errors.
So it's best to use the "$ variable name" method when this kind of condition is judged, so whatever the value of the variable is? Values are considered to be a whole when executed.
5. Stitching characters
"String to concatenate" ${variable name} "string to concatenate"
Instance
[Email protected] home]# name= "Yuanji"
[Email protected] home]# address= "ShenZhen"
[Email protected] home]# info= "Name is:" $name ". Address is: "$address
[Email protected] home]# echo $info
Name Is:yuanji. Address Is:shenzhen
${variable name}${variable name}
Instance
[Email protected] home]# name= "Yuanji"
[Email protected] home]# address= "ShenZhen"
[[email protected] home]# info=${name}${address}
[Email protected] home]# echo $info
Yuanjishenzhen
6. String interception
Method One:
1> start with the first few characters on the left and the number of characters, use: Start:len
[Email protected] log]# mystr= "Hello World"
[[email protected] log]# echo ${mystr:1:4}
Ello
1 represents the first character on the left and 4 indicates the total number of characters.
2> from the left to the beginning of the first few characters until the end, the usage is: start
[Email protected] log]# mystr= "Hello World"
[[email protected] log]# echo ${mystr:6}
World
Where 6 means the 7th character on the left starts
3> starting with the first few characters on the right and the number of characters, usage: 0-start:len
[Email protected] log]# mystr= "Hello World"
[[email protected] log]# echo ${mystr:0-5:3}
Wor
0-5 means that the 5th character starts at the right, and 3 indicates the number of characters.
4>, starting from the first few characters on the right to the end, usage: 0-start
[Email protected] log]# mystr= "Hello World"
[[email protected] log]# echo ${mystr:0-5}
World
Note: (The first character on the left is denoted by 0, and the first character on the right is denoted by 0-1)
Method Two:
Url= "http://yuanji6699.blog.51cto.com/11568362/1771741"
1> # Intercept, delete the left character, and leave the right character.
Echo ${url#*//}
where Var is the variable name and the # is an operator, *//indicates that the first//number and all characters on the left are deleted from the left.
Delete http://
The result is: yuanji6699.blog.51cto.com/11568362/1771741
2> # # Intercept, delete the left character and leave the right character.
Echo ${url##*/}
##*/indicates that the last (rightmost) one/number and all characters to the left are deleted from the left.
That is, delete http://yuanji6699.blog.51cto.com/11568362/
The result is 1771741.
3>% Intercept, delete right character, leave left character
Echo ${url%/*}
%/* indicates that the first/second and right characters are deleted from the right.
The result is: http://yuanji6699.blog.51cto.com/11568362
4>, delete the right character, leave the left character
Echo ${url%%/*}
%%/* indicates that the last (leftmost) one/number and right character are deleted from the right.
The result is: http:
Instance
[Email protected] log]# file_path= "D:/log/192.168.10.222/syslog.txt"
[[email protected] log]# echo ${file_path##*/}
Syslog.txt
[[email protected] log]# echo ${file_path%/*}
d:/log/192.168.10.222
${file_path##*/},${file_path%/*} is the method of getting the file name, and the directory address, respectively
${variable name #substring regular expression} starts with substring from the beginning of the string and deletes the expression on the match.
${variable name%substring regular expression} starts with substring from the end of the string and deletes the expression on the match.
Method Three:
Use the Cut,sed,awk command "follow-up instructions"
[Email protected] log]# file_path= "D:/log/192.168.10.222/syslog.txt"
[Email protected] log]# echo ${file_path} | Cut-d "/"-F 4
Syslog.txt
[Email protected] log]# echo ${file_path} | Awk-f '/' {print nf-=1} '
Syslog.txt
7. String substitution
${variable/find/Replace value} One "/" means replace the first, "//" means replace all, when the lookup appears: "/" add escape character "\ " to indicate
[Email protected] log]# file_path= "D:/log/192.168.10.222/syslog.txt"
[[email protected] log]# echo ${file_path/\//\\}
D:\log/192.168.10.222/syslog.txt
[[email protected] log]# echo ${file_path//\//\\}
D:\log\192.168.10.222\syslog.txt
Also available with awk,sed
[Email protected] log]# echo $file _path | Sed-e ' s/\//\\/'
D:\log/192.168.10.222/syslog.txt
[Email protected] log]# echo $file _path | Sed-e ' s/\//\\/g '
D:\log\192.168.10.222\syslog.txt
[Email protected] log]# echo $file _path | awk ' {sub ("/", "\ \", $);p rintf ("%s\n", $)} '
D:\log/192.168.10.222/syslog.txt
[Email protected] log]# echo $file _path | awk ' {gsub ("/", "\ \", $);p rintf ("%s\n", $)} '
D:\log\192.168.10.222\syslog.txt
[Email protected] log]# echo $file _path | TR "/" "\ \"
Tr:warning:an unescaped backslash at end of string are not portable
D:\log\192.168.10.222\syslog.txt
8. Finding substrings
[[email protected] log]# string= "This is my shell script"
[Email protected] log]# echo ' expr index ' $string ' is '
3
[[email protected] log]# echo ' expr index ' $string ' h '
2
Can only return a character or position where the first character appears in multiple characters"Very good"
Use the following method
[[email protected] log]# string= "This is my shell script"
[Email protected] log]#echo $string | awk ' {printf ("%d\n", Match ($, "shell"));} '
12
The following sections are described in follow-up content
Sed,awk,cut, et
The above is a personal point of summary and understanding, the level is not high, the level of writing is very bad, please greatly forgive me.
Can exchange learning together.
My qq:610851588.
Can join my build group (now very few people, hope slowly more up)
Linux Clusters: 183932302
Python, Shell AC Group: 469094734
This article from the "Go to the Origin dimension" blog, reproduced please contact the author!
Shell-7.shell Use of strings