Bash string processing

Source: Internet
Author: User

1. Obtain the string length.

Method 1:

$ Echo $ {# variable}

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Echo $ {# x}
14 Method 2:

$ Expr length "$ variable"

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Expr length "$ X"
14 method 3:

$ Expr "$ variable ":".*"

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Expr "$ X ":".*"
142. Find the position of the string substring

Method:

$ Expr Index "$ variable" "substring"

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Expr Index "$ X" "is"
3
Zhyfly :~ $ Expr Index "$ X" "T"
1 (PS: if there are duplicates, it seems that you can only find the first, second, and third,... how can you find them ???)

3. Obtain the string substring.

Method 1:

$ Echo $ {Variable: Position: length}

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Echo $ {X: 1: 5}
His I Method 2:

$ Expr substr "$ variable" startposition Length

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Expr substr "$ X" 1 5
This (PS: note the difference between method 1 and method 2 !)

4. Length of matching Regular Expression

Method:

$ Expr match "$ X" "string"

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Expr match "$ X" "his"
0
Zhyfly :~ $ Expr match "$ X" "this"
4
Zhyfly :~ $ Expr match "$ X ""."
15. The beginning and end of the string

Method:

$ Echo $ {Variable # startletter * endletter} # indicates the headers, because on the keyboard, # indicates the minimum match before $.

$ Echo $ {Variable # tartletter * endletter} indicates maximum matching.

$ Echo $ {Variable % startletter * endletter} # % indicates Tail removal, because % is behind $ on the keyboard. One parameter indicates minimum matching.

$ Echo $ {Variable % startletter * endletter} indicates maximum matching.

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Echo $ {X # t}
His is a test
Zhyfly :~ $ Echo $ {X # T * h}
Is is a test
Zhyfly :~ $ Echo $ {X # T * s}
Is a test
Zhyfly :~ $ Echo $ {X # T * s}
T
Zhyfly :~ $ Echo $ {x % t}
This is a TES
Zhyfly :~ $ Echo $ {x % S * t}
This is a te
Zhyfly :~ $ Echo $ {x % E * t}
This is a t
Zhyfly :~ $ Echo $ {x % I * t}
Th

6. Replacement of characters (strings)

Method:

$ Echo $ {Variable/oldletter/newletter} # replace one

$ Echo $ {Variable // oldletter/newletter} # Replace all

Code:

PHP code: zhyfly :~ $ X = "this is a test"
Zhyfly :~ $ Echo $ {x/I/M}
Thms is a test
Zhyfly :~ $ Echo $ {x // I/M}
Thms MS A TES

========================================================== ==========================================================
From: http://www.chinaunix.net/jh/7/575412.html

[Excellent] bash Programming
Http://www.chinaunix.net Author: jiupima published on: 16:29:17
[Comment] [View Original] [sco unix discussion board] [close]

Bash Programming
I. Bash special characters
1. wildcard:
*: Match any string
? : Match any single character
Set operators: Use a single word, a continuous range, or intermittent character set to work as wildcards.
[Set]: match a single character with a wildcard character set, such as [aeiou], [A-O], [a-h, W-Z]
[! Set]: A set wildcard consisting of all characters except the set
2. curly braces (can be nested ):
Format: [leading String] {string 1 [{nested string 1…}] [, Character transfer 2…]} [Successor string]
For example, c {A {R, t, n}, B {R, t, n} s equals to cars cats cans cbrs cbts cbns
3. Other special characters:
<: Input redirection
>;: Output redirection (create if no file exists, and overwrite if any)
>;>;: Redirection of the output. (if no value is set, it is created. If yes, It is appended to the end of the file)
(: Starting with a sub-shell, the sub-shell inherits some environment variables of the parent shell.
): The Sub-shell ends.
{: Starting from the command block, it is executed by the current shell and all environment variables are retained.
}: The command block ends.
|: MPS queue
/: Reference a single character
': Strongly referenced string. special characters are not interpreted.
": Weak referenced string, interpreting all special characters
~ : Root directory
': Command replacement
;: Command separator (command Terminator), run multiple commands in one line
#: Line comment
$: Variable expression
&: Execute commands in the background
*: String wildcard
? : Single Character wildcard
Ii. Bash Variables
1. Custom Variables
A user-defined variable consists of letters, numbers, and underscores. the first character of the variable name cannot be a number, and the variable name is case sensitive.
Varname = Value Note bash cannot leave spaces on both sides of the equal sign
Shell language is a non-type interpreted language. Assigning a value to a variable actually defines the variable and can assign different types of values. There are two ways to reference a variable: $ varname and $ {varname}. We recommend that you use the second method to prevent ambiguity of the variable in the string. The value of the referenced undefined variable is null.
Assign a value to a variable using quotation marks. Note the difference between ',', and '. ''is equivalent to $ ()
To make the variable usable in other processes, You need to export the variable: Export varname
2. Environment Variables
You can use the set command to assign values to the variables or view the environment variable values. Use the unset command to clear the variable values. Use the Export command to export the variables so that other processes can access the environment variables.
3. location variable
Bitwise variables correspond to command line parameters. $0 indicates the Script Name, $1 indicates the first parameter, and so on. variables must be referenced with more than 9 parameters. Shell retains these variables and does not allow users to define them in other ways. The location variables passed to scripts or functions are local and read-only, other variables are global (you can declare them as local using the local keyword ).
4. other variables
$? : Save the return code of the previous command.
$-: Provides options when the shell is started or when the set command is used
$: Process ID of the Current Shell
$! : Process number of the previous sub-process
$ #: Number of parameters passed to the script or function, that is, the number of location variables minus 1, excluding the Script Name.
$ *: A single string consisting of parameters passed to the script or function, that is, the string starting from the first parameter after the Script Name is exceeded, each parameter is separated by $ ifs (generally, the internal domain separator $ IFS is 1 space ). Like "..."
$ @: List of parameters passed to the script or function. These parameters are expressed as multiple strings. It is like ""…. The difference between $ * and $ @ is convenient to use two methods to process command line parameters, but the parameter appearance is no different during printing.
For example: # vi posparm. Sh
Function cutparm
{Echo-E "inside cntparm: $ # parms: $ */N "}
Cntparm "$ *"
Cntparm "$ @"
#./Posparm. Sh abc bca cab
Inside cntparm: 1 parms: abc bca cab
Inside cntparm: 3 parms: abc bca cab
Iii. Bash Operators
1. String operators (replacement operators)
$ {Var:-word} If var exists and is not empty, return its value; otherwise, return word
$ {Var: = word} If var exists and is not empty, return its value. Otherwise, assign word to VaR and return its value.
$ {Var: + word} If var exists and is not empty, word is returned; otherwise, null is returned.
$ {Var :? Message} If var exists and is not empty, its value is returned,
Otherwise, "bash2: $ var: $ message" is displayed, and the current command or script is exited.
$ {Var: Offset []} returns a length substring of VaR starting from the offset position,
If no length exists, it is the end of the VaR string by default.
2. pattern matching Operator
Starting from the VaR header, $ {var # pattern} deletes the shortest mode string that matches pattern and returns the remaining string.
$ {Var # pattern}: Starting from the VaR header, delete the longest pattern string that matches pattern and return the remaining string. basename path =$ {path ##*/}
$ {Var % pattern}: starting from the end of VaR, delete the shortest mode string that matches pattern and return the remaining string. dirname path =$ {PATH % /*}
Starting from the end of VaR, $ {var % pattern} deletes the longest pattern string that matches pattern and returns the remaining string.
$ {Var/pattern/string} replaces the longest pattern string matched with pattern in Var with string.
4. Shell conditions and test commands
Bash can use [... ] Structure or test command to test Complex Conditions
Format: [expression] or test expression
Returns a code indicating whether the condition is true or false. If the return value is 0, the return value is true. Otherwise, the return value is false.
Note: spaces between the left and right brackets are required.
1. File test operators
-D file exists and is a directory
-E file exists
-F file: The file exists and is a common file.
-G file exists and is a SGID (set group ID) File
-R file has the read permission on the file.
-S file exists and is not empty
-U file exists and is a SUID (Set User ID) File
-W file: write permission on file
-X file has the execution permission on the file. If it is a directory, it has the search permission.
-O file has File
-G file: test whether it is a member of the file group.
-L file is a symbolic link.
File1-nt file2 file1 is newer than file2
File1-ot file2 file1 is older than file2
2. String Operators
Str1 = str2 str1 and str2 match
Str1! = Str2 str1 and str2 do not match
Str1 <str2 str1 is smaller than str2
Str1>; str2 str1 is greater than str2
-N str STR length is greater than 0 (not blank)
-Z str length is 0 (empty string)
3. Integer Operators
Var1-EQ var2 var1 equals var2
Var1-ne var2 var1 is not equal to var2
Var1-ge var2 var1 greater than or equal to var2
Var1-GT var2 var1 greater than var2
Var1-Le var2 var1 less than or equal to var2
Var1-lt var2 var1 less than var2
4. logical operators
! Expr
Expr1 & expr2 evaluate the logic and of expr1 and expr2. When expr1 is false, expr2 is not executed.
Expr1 | expr2 evaluates the logic of expr1 and expr2, and does not execute expr2 when expr1 is true
Note: The logic of another logical operator and expr1-A expr2 logic or expr1-O expr2
V. Shell Flow Control
1. Condition Statement: If
If condition ifs =:
Then for dir in $ path
Statement do
[Elif condition if [-O dir]; then
Statement] echo-e "/tyou own $ dir"
[Else
Statement] echo-e "/tyou don't own $ dir"
Fi
2. deterministic loop: For done
For value in list for docfile in/etc/*/usr/etc /*
Do do
Statements using $ value CP $ docfile extends your docfile.txt
Done done
Note: For var ;... Equivalent to for VAR in "$ @";...
3. Uncertainty loop: While and
While condition until Condition
Do do
Statement
Done done

Count = 1 COUNT = 1
While [-n "$ *"] Until [-z "$ *"]
Do do
Echo "parameter $ count" Echo "parameter $ count"
Shift
Count = 'expr $ count + 1' COUNT = 'expr $ count + 1'
Done done
The condition is true. The execution cycle body condition is false.
Note: Definitions and algorithms of Integer Variables
Declare-I idx defines integer variables using $ () without definition
Idx = 1
While [$ idx! = 150]
Do
CP somefile. $ idx
Idx = $ idx + 1 integer algorithm idx = $ ($ idx + 1 ))
Done
Another algorithm, Echo $ (100/3), puts the addition, subtraction, multiplication, division expressions into $ ().
4. Select structure: Case and select
The case expression in expression is compared with the mode in sequence, and the first matching mode is executed.
Mode 1); enables the program control flow to jump to esac for execution, equivalent to break
Statement; allow the expression to match with a pattern containing wildcards
Mode 2)
Statement ;;
......
[*)
Statement]
Esac

Select value [in list] menu automatically generated by list
If do does not have a list, it is the location variable by default.
Statements using $ Value
Done
For example: IFS =: Set the domain separator:
PS3 = "choice>;" changes the default select prompt
Clear
Select dir in $ path
Do
If [$ dir]; then
CNT =$ (LS-Al $ dir | WC-l)
Echo "$ CNT Files in $ dir"
Else
Echo "no such choice !"
Fi
Echo-e "/npress enter to continue, CTRL-C to quit"
Read: Press enter to continue the program. Press Ctrl + C to exit.
Clear
Done
5. Command shift
Pass the command line parameters stored in the location variable to the left in sequence.
Shift n the command line parameter transmits n strings to the left
Vi. Shell functions
Definition: function fname ()
{{
Commands commands
}}
Call: fname [parm1 parm2 parm3...]
Description: functions are defined before use. The two definitions have the same functions.
The function name and the called function parameter become the location variable of the function.
The variables in the function should be declared as local variables using local.
VII. Input and Output
Limited space. All input and output operators and functions are not discussed here.
1. I/O redirection
<: Input redirection
>;: Output redirection (create if no file exists, and overwrite if any)
>;>;: Redirection of the output. (if no value is set, it is created. If yes, It is appended to the end of the file)
<: Enter redirection (here document)
Format: Command <label
Input...
Label
Note: Make the input of a command a shell script (input ...), Until the end of the label
For example: CAT <$ home/. profile>; out
Echo "add to file end !" >;>; $ Home/. Profile
FTP: User = Anonymous
Pass = YC@163.com
FTP-I-n <End-I: non-interactive mode-N: Disable Automatic Logon
Open ftp.163.com
User $ user $ pass
CD/pub
Close
End end mark input end
2. String I/O operations
String output: Echo
Command Option:-E: Start escape sequence-N: Cancel output and wrap
Escape Sequence:/a: alt/Ctrl + g (Bell)/B: backspace/Ctrl + H
/C: line feed after canceling output/F: formfeed/Ctrl + J
/R: return/Ctrl + M/V: vertical Tab
/N: octal ASCII character //: Single/character/T: Tab
String input: Read
It can be used for user interaction input or for processing a line in a text file at a time.
Command Option:-A: Read the value into the array. The array subscript starts from 0.
-E: Use the GNU Readline library to read data and allow bash editing.
-P: print the prompt before reading
For example, ifs =:
Read-p "Start read from file. filename is:/C" filename
While read name pass uid gid gecos home shell <FILENAME
Do
Echo-E "Name: $ name/npass: $ pass/N"
Done
Note: If the number of fields in a row is greater than the number of variables in the variable table, all subsequent fields are appended to the final variable.
8. Command Line Processing
Command Line Processing Command: getopts
There are two parameters. The first is an option list string consisting of letters and colons, and the second is a variable name.
The option list string is composed of option letters starting with a colon. If an option requires a parameter, the option letter is followed by a colon.
Getopts splits the first parameter and extracts the option and assigns it to the second parameter variable.
If an option has parameters, read the parameters to the built-in variable optarg.
The built-in variable optind stores the value of the command line parameter (location parameter) to be processed.
After the Option List is processed, getopts returns 1; otherwise, 0 is returned.
For example, while getopts ": xy: Z:" OPT
Do
Case $ opt in
X) xopt = '-x set ';;
Y) yopt = "-y set and called with $ optarg ";;
Z) zopt = "-Z set and called with $ optarg ";;
/?) Echo 'usage: getopts. Sh [-x] [-y Arg] [-Z Arg] File... '
Exit 1
Esac
Done
Shift ($ opting-1) removes the processed command line parameters
Echo $ {xopt:-'Did not use-x '}
Echo $ {yopt:-'Did not use-y '}
Echo $ {zopt:-'Did not use-Z '}

Echo "remaining command-line arguments are :"
For f in "$ @"
Do
Echo-E "/T $ F/N"
Done
9. Process and Job Control
Signal Processing Command: Trap
Format: Trap Command sig1 sig2...
Trap can recognize more than 30 types of signals, such as interrupt (CTRL + C) and suspension (CTRL + Z). You can use kill-L to view the signal list.
When the script receives signals such as sig1 and sig2, trap executes the command. After the command is complete, the script is re-executed.
Signals can be identified by names or numbers.
Job control commands: BG, FG
BG: displays background processes, that is, processes that are suspended by Ctrl + Z or executed by 'COMMAND & '.
FG: transfers the background process to the foreground for execution.
Kill-9% N: Kill the nth background process
Appendix:
1. Bash-supported command line parameters
-A outputs all variables
-C "string" reads the command from string
-E: use non-interactive mode.
-F prohibit the generation of shell file names
-H Definition
-I Interactive Mode
-K is the command execution setting option.
-N: reads the command but does not execute it.
-R-restricted mode
-S command read from standard input
-T execute a command and then exit Shell
-When U is replaced, an error occurs when unspecified variables are used.
-V: Display Shell input lines
-X trace mode: displays the executed commands.
Many modes can be used in combination. You can use set to set or cancel shell options to change the shell environment. Enable option "-" And disable option "+". If the options set in shell are displayed, run: $ echo $-
2. The shell environment variables in profile are as follows:
The search path used when cdpath executes the CD command
Home user's home directory
The domain delimiter inside ifs, which is generally a space character, Tab character, or line feed.
Mail specifies the path of a specific file (Mailbox), which is used by the Unix Mail System
Path: Search Path of the command (the path of config. sys in DOS)
PS1 main command prompt, default is "$"
PS2 from the command prompt, the default is "> ;"
Term terminal type
========================================================== ==========================================================
From: http://unix-cd.com/unixcd12/article_view.asp? Id = 4440

To Length

% X = "ABCD"
# Method 1
% Expr length $ x
4
# Method 2
% Echo $ {# x}
4
# Method 3
% Expr "$ X ":".*"
4
# Expr help
# String: Regexp anchored pattern match of Regexp in string
Search for substrings

% Expr index $ X "B"
2
% Expr index $ X ""
1
% Expr index $ X "B"
2
% Expr index $ X "C"
3
% Expr index $ X "D"
4
Obtain the substring

# Method 1
# Expr <string> startpos Length
% Expr substr "$ X" 1 3
ABC
% Expr substr "$ X" 1 5
ABCD
% Expr substr "$ X" 2 5
BCD
# Method 2
# $ {X: pos: lenght}
% Echo $ {X: 1}
BCD
% Echo $ {X: 2}
CD
% Echo $ {X: 0}
ABCD
% Echo $ {X: 0: 2}
AB
% Pos = 1
% Len = 2
% Echo $ {X: $ pos: $ Len}
BC
Match Regular Expression

# Print matching length
% Expr match $ X "."
1
% Expr match $ X "ABC"
3
% Expr match $ X "BC"
0
The beginning and end of a string

% X = aabbaarealwwvvww
% Echo "$ {x % W * w }"
Aabbaarealwwvv
% Echo "$ {x % W * w }"
Aabbaareal
% Echo "$ {X # A * }"
Lwwvvww
% Echo "$ {X # A * }"
Bbaarealwwvvww
Where, # indicates the header, because # on the keyboard is on the left of $.
Here, % indicates % because % on the keyboard is on the right of $.
A single parameter indicates the minimum matching, and a double parameter indicates the maximum matching.
That is to say, when there are multiple matching schemes, select the maximum length or the minimum length of the matching.

String replacement

% X = abcdabcd
% Echo $ {x/a/B} # Replace only one
Bbcdabcd
% Echo $ {x // a/B} # Replace all
Bbcdbbcd
Regexp cannot be used, *? .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.