When writing a shell script, we need to use a variety of special symbols, through these special symbols can make us write code more concise and efficient, here to the summary:
1, {} curly braces:
Usage One: Wildcard extension
Eg:ls My_{finger,toe}s
This command corresponds 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.
Use Two: can be used for the construction of statement blocks , between statements separated by carriage returns. If you want to use multiple statements in places where a single statement is used (for example, in the and or list), you can construct a statement block 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 commands in the above braces are enough to be a block of statements)
Usage three: Parameter extension
${name:-default} uses a default (typically null) value to replace the empty or unassigned variable name;
${name:=default} uses the specified value instead of the 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 the name
${name%word} Deletes the smallest part that matches word from the tail of name and returns the remainder
${name%%word} deletes the longest part that matches word, starting at the end of name, and then returns the remainder
${name#word} Deletes the smallest part that matches word from the header of name and returns the remainder
${name# #word} Deletes the longest part that matches word from the header of name and then returns 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, [] bracket:
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
Use two: for conditional judgment symbols:
the [] symbol is understood to point to a soft link to the test command, so its usage can be fully referenced to test and replaced with the test position.
Eg:if ["$?"!= 0] is equivalent to if test "$?"!= 0
Then echo "Executes error"
3. ' Command ' counter quotes: '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 extension f*.sh the name of the file that gives all matching schemas.
4, ' string ' single quotes and ' string ' double quotes
Double quotes: If you want to add a space to a defined variable, you must use either single or double quotes.
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 heyyou variable)
5, $# its role is to tell you the total number of reference variables is how much
$$ 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 $ $ $ ...;
$@ is basically similar to $* (see ordinal 7), but it is somewhat different when the array is assigned a value;
$? Exit code for the previous command;
$-Displays the current options used by the shell;
$! The process ID number of the last background run.
6. $ ((...)) Syntax: evaluation of expressions 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 the parameters passed in by the script, noting that the number is enclosed in curly braces when you want to represent a two-digit parameter.
$@ lists all parameters, separated by spaces
$*: Lists all parameters, separated by the first character of the environment variable IFS
8. List of commands:
and list statement1 && statement2 && statement3 && ...: Execute the last command only if all previous commands have been executed successfully
or List statement1 | | Statement2 | | Statement3 | | ...: Allow execution of a series of commands until one of the commands 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 from the example above is:
Hello
In Else
The list of and or lists is used in logical judgments, and here's one of the most common examples:
[Condition] && command for true | | Command for false:
Executes commandfor true when the condition is true, executing command for false when the condition is false
9,: Colon: Built-in empty instructions, the return value of 0
eg: $:
$ echo $?
$0
While: (the statement structure can implement an infinite loop)
10,; Semicolon: In the shell, the symbol that acts as a "continuous instruction" function is "semicolon."
EG:CD ~/backup; mkdir startup; CP ~/.* startup/.
11, # Well No.: the symbol is followed by the annotation text, will not be executed;
* matches any character in the filename, including a string;
? Matches any single character in the file name.
~ Represents the user's home directory
12, \ Inverted slash: placed in front of the instruction, there is a cancellation of aliases (alias) role, before the special symbol, then the role of the special symbol disappeared; at the very end of the instruction, the instruction connects to the next line (makes the carriage return invalid, only acts as a newline)
13,! Exclamation point: usually it represents the function of the logic, such as conditional detection, with!= to represent "not equal"
14, * * Secondary operation: two asterisks in the operation of the "second side" means
Eg:let "Sus=2**3″
echo "SUS = $sus"
$ sus = 8-