1.Linux Shell intercepts the first 8 bits of a character variable
There are several ways to implement this:
- Expr substr "$a" 1 8
- echo $a |awk ' {print substr (, 1,8)} '
- Echo $a |cut-c1-8
- echo $
- Expr $a: ' \ (. \ \).
- echo $a |dd Bs=1 count=8 2>/dev/null
2. Intercept by the specified string
(1) The first method:
A string after the last string is truncated from left to right
${varible##*string}
A string after the first string is truncated from left to right
${varible#*string}
A string after the last string is intercepted from right to left
${varible%%string*}
A string after the first string is intercepted from right to left
${varible%string*}
"*" is just a wildcard can not
Take a look at the following example:
$ myvar=foodforthought.jpg$ echo ${myvar##*fo}rthought.jpg$ Echo ${myvar#*fo}odforthought.jpg
(2) The second method:
${VARIBLE:N1:N2}: Intercepts the variable varible the N2 character starting from N1, forming a substring. You can select specific substrings by using a different form of variable extension, depending on the specific character offset and length. Try entering the following line in bash:
$ exclaim=cowabunga$ echo ${exclaim:0:3}cow$ Echo ${exclaim:3:7}abunga
This form of string truncation is simple, with only a colon separated to specify the starting character and substring length.
3. Split according to the specified requirements:
For example, get the suffix name
Ls-al | Cut-d "."-f2
Summary: Shell corresponding string processing method, according to the needs of flexible choice.
String-related operations are often involved in the shell batch process. There are a lot of command statements, such as: awk,sed can do string various operations. In fact, the shell built a series of operating symbols, you can achieve similar effects, you know, using internal operators will omit the start of external programs and other time, so the speed will be very fast.
A grammar
?
1234 |
cut [选项] 文件名 选项: -f 列号:提取第几列 -d 分隔符:按照指定分隔符分割列 |
[[$name =~ xx]];then
Xx
"#" stands for root permissions
"$" on behalf of ordinary users
[Email protected]: Indicates the contents of all script parameters
$#: Indicates the number of all script parameters returned.
$# 是传给脚本的参数个数
$
0
是脚本本身的名字
$
1
是传递给该shell脚本的第一个参数
$
2
是传递给该shell脚本的第二个参数
[email protected] 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过
9
个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,
0
表示没有错误,其他表示有错误
Example: Write the following shell script, saved as test.sh
#!/bin/sh
echo "number:$#"
echo "Argume:[email protected]"
Execute script:
./test.sh First_arg Second_arg
First, Judge read the string value
Meaning of an expression
${var} |
Variable var with the same value as $var |
|
|
${var-default} |
If Var is not declared, then use $default as its value * |
${var:-default} |
If Var is not declared, or its value is empty, then $default is used as its value * |
|
|
${var=default} |
If Var is not declared, then use $default as its value * |
${var:=default} |
If Var is not declared, or its value is empty, then $default is used as its value * |
|
|
${var+other} |
If Var is declared, then its value is $other, otherwise it is a null string |
${var:+other} |
If Var is set, then its value is $other, otherwise it will be a null string |
|
|
${var? ERR_MSG} |
If Var is not declared, print $err_msg * |
${var:? ERR_MSG} |
If Var is not set, then print $err_msg * |
|
|
${!varprefix*} |
Matches all previously declared variables beginning with Varprefix |
${[email protected]} |
Matches all previously declared variables beginning with Varprefix |
Adding "*" does not mean: Of course, if the variable var is already set, then its value is $var.
Second, string operation (length, read, replace)
Meaning of an expression
${#string} |
Length of $string |
|
|
${string:position} |
In $string, start extracting substrings from position $position |
${string:position:length} |
In $string, a substring of length $length is extracted starting from position $position |
|
|
${string#substring} |
Delete the substring of the shortest match $substring from the beginning of the variable $string |
${string# #substring} |
Delete the substring of the longest matching $substring from the beginning of the variable $string |
${string%substring} |
Delete the substring of the shortest match $substring from the end of the variable $string |
${string%%substring} |
Delete the substring of the longest matching $substring from the end of the variable $string |
|
|
${string/substring/replacement} |
Use $replacement to replace the first matching $substring |
${string//substring/replacement} |
Use $replacement instead of all matching $substring |
${string/#substring/replacement} |
If the $string prefix matches the $substring, then the $replacement is used instead of the matching $substring |
${string/%substring/replacement} |
If the $string suffix matches the $substring, then the $replacement is used instead of the matching $substring |
|
|
Description: "* $substring" can be a regular expression .
Linux in ~/cut/argus/