Reprint: http://www.51testing.com /? Uid-225738-action-viewspace-itemid-218002
ShellProcessing
StringMethod (for future reference)
1. Construct a string
Direct Construction
Str_zero = Hello
Str_first = "I Am a string"
Str_second = 'success'
Repeated multiple times
# Repeat the first parm ($1) by $2 times
Strrepeat ()
{
Local x = $2
If ["$ X" = ""]; then
X = 0
Fi
Local str_temp = ""
While [$ X-ge 1];
Do
Str_temp = 'printf "% S % s" "$ str_temp" "$1 "'
X = 'expr $ X-1'
Done
Echo $ str_temp
}
Example:
Str_repeat = 'strrepeat "$ user_name" 3'
Echo "Repeat = $ str_repeat"
Ii. Assignment and copy
Direct assignment
Same as constructing a string
User_name = Terry
Assign values from variables
Aliase_name = $ user_name
Iii. Join
Directly join two strings
Str_temp = 'printf "% S % s" "$ str_zero" "$ user_name "'
Use printf for more complex connections
4. Length
Get the length of the string variable: $ {# string}
Evaluate the number of characters (char)
Count_char = 'echo "$ str_first" | WC-m'
Echo $ count_char
Number of bytes)
Count_byte = 'echo "$ str_first" | WC-C'
Echo $ count_byte
Word Count)
Count_word = 'echo "$ str_first" | WC-W'
Echo $ count_word
V. Comparison
Equal comparison str1 = str2
Not equal to str1! = Str2
Example:
If ["$ user_name" = "Terry"]; then
Echo "I am Terry"
Fi
Less than comparison
# Return 0 if the two string is equal, return 1 if $1 <$2, else 2 strcompare () {local x = 0 if ["$1 "! = "$2"]; then X = 2 localtemp = 'printf "% s/n % s" "$1" "$2" 'local temp2 = '(echo "$1 "; echo "$2") | sort 'If ["$ Temp" = "$ temp2"]; then x = 1 fi echo $ x}
VI,Test
Empty-Z Str
Unempty-n str
Digit or not
# Return 0 if the string is num, otherwise 1
Strisnum ()
{
Local ret = 1
If [-n "$1"]; then
Local str_temp = 'echo "$1" | SED's/[0-9] // g''
If [-z "$ str_temp"]; then
Ret = 0
Fi
Fi
Echo $ RET
}
Example:
If [-n "$ user_name"]; then
Echo "my name is not empty"
Fi
Echo 'strisnum "9980 "'
7. Segmentation
Take the symbol + as the standard. Separate the characters into the left and right parts.
Use SED
Example:
Command date -- the output of the rfc-3339 seconds is
15:09:47 +
Take the + Left Part
Date -- rfc-3339 seconds | SED's/+ [0-9] [0-9]: [0-9] [0-9] // G'
The output is
2007-04-14 15:09:47
Take the + part on the right
Date -- rfc-3339 seconds | SED's/. * + // G'
The output is
08:00
String separated by Spaces
Use awk
Example:
Str_fruit = "banana 0.89 100"
Take field 3rd
Echo $ str_fruit | awk '{print $3 ;}'
8. substring
Whether string 1 is a substring of string 2
# Return 0 is $1 is substring of $2, otherwise 1
Strissubstring ()
{
Local x = 1
Case "$2" in
* $1 *) x = 0 ;;
Esac
Echo $ x
} Shell string TruncationI,LinuxShell intercepts the first 8 characters of a character variable. The method is as follows:
1. expr substr "$ A" 1 8
2. Echo $ A | awk '{print substr (, 1, 8 )}'
3. Echo $ A | cut-c1-8
4. Echo $
5. expr $ :'/(.//).*'
6. Echo $ A | dd BS = 1 COUNT = 8 2>/dev/null
2. truncate by specified string
1. Method 1:
- $ {Varible # * string} captures the string after the last string from left to right.
- $ {Varible # * string} captures the string after the first string from left to right.
- $ {Varible % string *} captures the string after the last string from the right to the left
- $ {Varible % string *} captures the string after the first string from the right to the left.
"*" Is only a wildcard.
Example:
$ Myvar1_foodforthought.jpg
$ Echo $ {myvar # * fo}
Rthought.jpg
$ Echo $ {myvar # * fo}
Odforthought.jpg
2. Method 2: $ {Varible: N1: N2}: intercept variable varble N2 characters starting from N1 to form a substring. You can use another form of variable extension to select a special character string based on the specific character offset and length. Enter the following lines in Bash:
$ Exclaim = Cowabunga
$ Echo $ {exclaim: 0: 3}
Cow
$ Echo $ {exclaim: 3: 7}
Abunga
String Truncation in this form is very simple. You only need to use a colon to separate the start character and the length of the substring.
3. Split according to specified requirements:
For example, get the suffix
Ls-Al | cut-d "."-F2
Shell (BASH) comparison operator
Operator |
Description |
Example |
File comparison operator |
-EFilename |
IfFilenameYes, true |
[-E/var/log/syslog] |
-DFilename |
IfFilenameIs a directory, it is true |
[-D/tmp/mydir] |
-FFilename |
IfFilenameIs a regular file, it is true |
[-F/usr/bin/grep] |
-LFilename |
IfFilenameIs a symbolic link, it is true |
[-L/usr/bin/grep] |
-RFilename |
IfFilenameReadable, true |
[-R/var/log/syslog] |
-WFilename |
IfFilenameWritable, true |
[-W/var/mytmp.txt] |
-XFilename |
IfFilenameExecutable, true |
[-L/usr/bin/grep] |
Filename1-NTFilename2 |
IfFilename1RatioFilename2New, true |
[/Tmp/install/etc/services-nt/etc/services] |
Filename1-OtFilename2 |
IfFilename1RatioFilename2Old, true |
[/Boot/bzimage-ot ARCH/i386/boot/bzimage] |
String comparison operator[Size =-1] (please note the use of quotation marks, this is a good way to prevent space from disturbing the code) |
-ZString |
IfStringIf the length is zero, it is true. |
[-Z "$ myvar"] |
-NString |
IfStringNon-zero length, true |
[-N "$ myvar"] |
String1=String2 |
IfString1AndString2Same, true |
["$ Myvar" = "One Two Three"] |
String1! =String2 |
IfString1AndString2Otherwise, true |
["$ Myvar "! = "One Two Three"] |
Arithmetic comparison operator |
Num1-EQNum2 |
Equal |
[3-EQ $ mynum] |
Num1-NeNum2 |
Not equal |
[3-ne $ mynum] |
Num1-LtNum2 |
Less |
[3-lt $ mynum] |
Num1-LeNum2 |
Less than or equal |
[3-Le $ mynum] |
Num1-GTNum2 |
Greater |
[3-GT $ mynum] |
Num1-GeNum2 |
Greater than or equal |
[3-ge $ mynum] |