This article mainly introduces some special symbols and its function in shell script, summary is very concise, easy to understand, the need for friends can refer to the next
In writing shell scripts, we need to use a variety of special symbols, through these special symbols can make the code we write more concise and efficient, here are summarized below:
1. {} curly braces:
Usage One: Wildcard extension
Eg:ls My_{finger,toe}s
This command is equivalent to a combination of the following commands:
LS my_fingers my_toes
Eg:mkdir {Usera,userb,userc}-{home,bin,data}
We will get Usera-home, Usera-bin, Usera-data, Userb-home, Userb-bin,userb-data,userc-home, Userc-bin, UserC-data, these several directories.
usage Two: can be used for the construction of statement blocks , between statements separated by carriage return. If you want to use multiple statements in certain places where a single statement is used, such as in an and or or list, you can construct a block of statements by enclosing them in curly braces {}.
eg
{
Grep-v "$cdcatnum" $strack _file > $temp _file
Cat $temp _file > $strack _file
Echo
Cat-n file1
} (Note: The four sentences in the curly braces above are enough to be a block of statements)
Usage three: Parameter extension
${name:-default} uses a default value (usually a null value) in place of the empty or unassigned variable name;
${name:=default} uses the specified value instead of an empty or unassigned variable name;
${name:?message} If the variable is empty or unassigned, the error message is displayed and the execution of the script is aborted and the exit code 1 is returned.
${#name} gives the length of name
${name%word} removes the smallest part that matches word from the end of name and then returns the remainder
${name%%word} removes the longest part that matches word from the end of name and then returns the remainder
${name#word} removes the smallest part that matches word from the head of name and then returns the remainder
${name# #word} Remove the longest part of word from the head of name and return the remainder
(Note, name is the variable name, Word is the string to match)
Usage three is useful when dealing with strings and unknown variables.
2, [] brackets:
Usage One: Wildcard extension:
Allow matching of any single character in square brackets
Eg:ls/[eh][to][cm]*
Equivalent to executing ls/etc/home (if there is a/eom directory, it is equivalent to executing ls/etc/home/eom)
Note: cannot be extended under the mkdir command
Usage two: Used for conditional judgment symbols:
The [] symbol can be interpreted as a soft link to the test command, so its usage can be fully referenced by test, and the test location will be replaced with [.
Eg:if ["$?"! = 0] equivalent to if test "$?"! = 0
Then echo "Executes error"
3, ' command ' anti-quote: 'command ' has the same meaning as $ (command), which returns the result of the current execution command
Eg: #!/bin/sh
For file in $ (ls f*.sh);d o
LPR $file
Done
Exit 0
This example implements the name of the file that extended f*.sh gives all matching patterns.
4, ' string ' single quote and ' string ' double quotation marks
Double quotation marks: If you want to add a space to a defined variable, you must use single or double quotation marks.
The difference between single and double quotes is that double quotes escape special characters and single quotes do not escape special characters
Eg: $ heyyou=home
$ Echo ' $heyyou '
$ $heyyou ($ not escaped)
Eg: $ heyyou=home
$ echo "$heyyou"
$ home (Obviously, $ escaped the value of the output of the heyyou variable)
5. $# its role is to tell you how much the total number of reference variables is
$$ its role is to tell you the process number of the shell script;
$* displays all the parameters passed by the script in a single string. Equivalent to $ $ $ ...;
[email protected] is basically similar to $* (see serial number 7), but it is somewhat different when assigning values to an array;
$? The exit code of the previous command;
$-shows the current options used by the shell;
$! The process ID number that was last run in the background.
6, $ ((...)) Syntax: Evaluating an expression within parentheses
eg
#!/bin/sh
X=0
hile ["$x"-ne];d o
Echo $x
x=$ (($x + 1))
Done
Exit 0
7. References to several special parameter variables in the shell
$, $, $3......${10}, ${11}, ${12} ... : represents each parameter passed in by the script, and note that the number is enclosed in curly braces when it is necessary to represent a parameter after a two-digit number.
[Email protected] List all parameters, each parameter is separated by a space
$*: Lists all parameters, separated by the first character of the environment variable IFS
8. List of commands:
and list statement1 && statement2 && statement3 && ...: Executes the latter command only if all previous commands are executed successfully
or List statement1 | | Statement2 | | Statement3 | | ...: Allows a series of commands to be executed until one command succeeds, and all subsequent commands are no longer executed
Eg:#!/bin/sh
Touch File_one
Rm-f File_two
If [-f File_one] && echo "Hello" && [-F File_two] && echo "There"
Then
echo "in If"
Else
echo "in Else"
Fi
Exit 0
The output of the above example is:
Hello
In Else
The list of and lists and or lists are used in logical judgments, and the following is the most common example:
[Condition] && command for true | | Command for false:
Executes commandfor true when the condition is true, and executes command for false when the condition is false
9,: Colon: Built-in null instruction, return value is 0
Eg: $:
$ echo $?
$0
While: (the statement structure can implement an infinite loop)
10,; Semicolon: In the shell, the symbol that functions as a "continuous instruction" is a semicolon.
EG:CD ~/backup; mkdir startup; CP ~/.* startup/.
11, #: Indicates that the symbol is followed by the annotation text, will not be executed;
* Match any character in the file name, including the string;
? Matches any single character in the file name.
~ Represents the user's home directory
12, \ Inverted slash: placed before the command, there is a cancellation of the role of aliases (alias), before the special symbol, the role of the special symbol disappears; placed at the very end of the instruction, indicating that the command is connected to the next line (which makes the carriage return invalid and only acts as a newline)
14,! Exclamation point: usually it represents the role of anti-logic, such as conditional detection, using! = to represent "not equal"
15, * * Sub-square operation: two asterisks represent the meaning of "the second party" in operation
Eg:let "Sus=2**3″
echo "SUS = $sus"
$ sus = 8-
Some of the special symbols and functions that are common in shell scripts