In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the special symbols in the shell script". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what special symbols are in the shell script.
The special symbols commonly used in shell are listed as follows:
#;;. , /\ 'string' |! ${} $? $$*
\ "string\" * *?: ^ $# $@ `command` {} [] [[]] () (())
| | & {xx,yy,zz,...} ~ + ~-&\\ + -% = =! =
# pound sign (comments)
This is a symbol almost everywhere, except for the\ "first line\" mentioned earlier.
#! / bin/bash
The pound sign also often appears at the beginning of a line or after a complete instruction, which indicates that the symbol is followed by comment text and will not be executed.
# This line is comments.
Echo\ "a = $a\" # a = 0
Because of this feature, when you temporarily don't want to execute a line of instructions, just add # to the beginning of the line. This is often used in the writing process.
# echo\ "a = $a\" # a = 0
If it is used in an instruction, or enclosed in double quotation marks, or after a backslash, it becomes a general symbol and does not have the above special functions.
~ home directory of the account
It is a common symbol that represents the user's home directory: cd ~; you can also add the name of an account directly after the symbol: cd ~ user or as part of the path: ~ / bin
~ + the current working directory. This symbol represents the current working directory. It has the same function as the built-in instruction pwd.
# echo ~ + / var/log
~-the last working directory, and this symbol represents the last working directory.
# echo ~-/ etc/httpd/logs
; semicolon (Command separator)
In shell, the symbol that serves as the function of\ "sequential instruction\" is\ "semicolon\". For example, the following examples: cd ~ / backup; mkdir startup; cp ~ /. * startup/.
;; consecutive semicolon (Terminator)
Dedicated options in case to play the role of Terminator.
Case\ "$fop\" inhelp) echo\ "Usage: Command-help-version filename\";; version) echo\ "version 0.1\";; esac
. Comma (dot, which means "dot")
In shell, users should be aware that one dot represents the current directory and two dot represent the upper directory.
CDPATH=.:~:/home:/home/web:/var:/usr/local
In the uplink CDPATH setting, the dot after the equal sign represents the current directory.
If the file name starts with dot, the file is a special file and must be displayed with the-an option in the ls directive. In addition, in regularexpression, a dot represents a match to a character.
'string' single quotation marks (single quote)
Content enclosed in single quotation marks will be treated as a single string. The $symbol that represents a variable in quotation marks has no effect, that is, it is treated as a general symbol to prevent any variable substitution.
Heyyou=homeecho'$heyyou' # We get $heyyou
\ "string\" double quotes (double quote)
Content enclosed in double quotes will be treated as a single string. It prevents wildcard expansion, but allows variable expansion. This is different from the way single arguments are handled.
Heyyou=homeecho\ "$heyyou\" # We get home
`command` inverted quotation marks (backticks)
In the preceding single and double quotation marks, you enclose the string, but what if the string is a command line? The answer is no. To deal with this situation, we have to use inverted single quotation marks.
Fdv= `date +% F`echo\ "Today $fdv\"
Date +% F in inverted quotes is treated as an instruction, and the result of the execution is brought into the fdv variable.
, comma (comma, comma in punctuation)
This symbol is often used as a "partition" in operations. Such as the following example
#! / bin/bashlet\ "T1 = ((a = 5 + 3, b = 7-1, c = 15 / 3))\" echo\ "T1 = $T1, a = $a, b = $b\"
/ slash (forward slash)
When the path is represented, it represents the directory.
Cd / etc/rc.dcd.. /.. Cd /
Usually a single / represents the meaning of the root root; in four operations, the symbol for division.
Let\ "num1 = (a = 10 / 2, b = 25 / 5)\"
\\ backslash
The escape character in interactive mode has several functions; before the instruction, it has the effect of canceling aliases; before the special symbol, the function of the special symbol disappears; at the end of the instruction, the instruction is connected to the next line.
# type rmrm is aliased to `rm-iTunes #\\ rm. / * .log
In the above example, I added the escape character before the rm instruction to temporarily cancel the alias function and restore the rm instruction.
# bkdir=/home# echo\ "Backup dir,\\ $bkdir= $bkdir\" Backup dir,$bkdir = / home
The\ $bkdir,escape in the above example echo disables the function of the $variable, so it outputs $bkdir, while the second $bkdir outputs the variable's content / home.
| | pipeline (Pipeline) |
Pipeline is a basic and important concept of UNIX system. Connect the standard output of the previous instruction as the standard input of the next instruction.
Who | wc-l
Making good use of this concept is quite helpful to simplify script.
! Exclamation point (negate or reverse)
Usually it represents the role of anti-logic, such as in conditional detection, using! = to represent\ "not equal to\"
If [\ "$?\"! = 0] thenecho\ "Executes error\" exit 1fi
She plays the role of anti-logic in regular expressions.
Ls a [! 0-9]
In the above example, the representative shows that in addition to a0, A1. A9 other files of these files.
: colon
In bash, this is a built-in instruction:\ "do nothing\" but return a status value of 0.
:
Echo $? # response is 0
: > f.commodity $
The above line is equivalent to cat / dev/null > f. Quote $. Not only is the writing shorter, but also the execution efficiency is much better.
Sometimes, there are the following kinds of usages
: ${HOSTNAME?} ${USER?} ${MAIL?}
The purpose of this line is to check whether these environment variables are set, and those that are not set will display an error message as a standard error. Such checks can basically be handled using practices such as test or if, but they are not as concise and efficient as the previous example.
? Question mark (wild card)
The role played on the file name extension (Filename expansion) is to match any character, but does not contain a null character.
# ls a?a1
Make good use of her characteristics, you can do a more accurate file name matching.
* asterisk wild card
A fairly common symbol. On the file name extension (Filename expansion), she is used to represent any character, including null characters.
# ls Ayoga A1 access_log
In the operation, it stands for\ "multiplication\".
Let\ "fmult=2*3\"
In addition to the built-in instruction let, there is also an instruction expr about operations, where the asterisk also plays the role of multiplication. But you have to be careful when using it. He must be preceded by an escape character.
* * power operation
The two asterisks mean "to the power" in the operation.
Let\ "sus=2**3\" echo\ "sus= $sus\" # sus= 8
$money number (dollar sign)
The symbol representing the variable substitution (Variable Substitution).
Vrs=123echo\ "vrs= $vrs\" # vrs=123
In addition, it is defined as the end (end-of-line) of\ "line\" in Regular Expressions. This is commonly used in grep, sed, awk, and vim (vi).
Regular expression of ${} variable
Bash defines a number of uses for ${}. The following is a list of tables taken from online instructions
${parameter:-word} ${parameter:=word} ${parameter:?word} ${parameter:+word} ${parameterffset:length} ${! prefix*} ${# parameter} ${parameter#word} ${parameter##word} ${parameter%word} ${parameter%%word} ${parameter/pattern/string} ${parameter//pattern/string}
$*
$* references the execution reference variable of script, using the same algorithm as the general instruction, with the instruction itself being 0, followed by 1, and so on. Reference variables are represented as follows:
$0, $1, $2, $3, $4, $5, $6, $7, $8, $9, ${10}, ${11}.
For single digits, you can use numbers directly, but for more than two digits, you must use the {} symbol.
$* is the symbol that represents all referenced variables. When in use, double quotation marks should be added as appropriate.
Echo\ "$*\"
There is also a symbol that has the same effect as $*, but with slightly different utility and handling.
$@
A symbol that has the same effect as $*, but there is one difference between them.
The symbol $* treats all reference variables as a whole. However, the symbol $@ still retains the section concept of each reference variable.
$#
This is also a symbol related to reference variables, and its function is to tell you what the total number of reference variables is.
Echo\ "$#\"
$? Status value (status variable)
Generally speaking, the process of a UNIX (linux) system ends with a system call exit (). This return value is the status value. Passed back to the parent process to check the execution status of the child process.
If the general instruction program executes successfully, its return value is 0; the failure is 1.
Tar cvfz dfbackup.tar.gz / home/user > / dev/nullecho\ "$?" $$
Because the ID of a process is unique, there can be no repetitive PID at the same time. Sometimes, script needs to generate temporary files to store the necessary data. And this script may also be used by users at the same time. In this case, the fixed file name is unreliable in the way it is written. Only by generating a dynamic file name can it meet the needs. The symbol $$may meet this requirement. It represents the PID of the current shell.
Echo\ "$HOSTNAME, $USER, $MAIL\" > ftmp.$$
Using it as part of a file name can avoid overwriting the same file name at the same time.
Ps: basically, the system reclaims the executed PID and then allocates it again as needed. So even if the temporary file is written with a dynamic file name, script will cause other problems if it is not cleared after the execution of the script.
Instruction group (command group)
Enclose a series of consecutive instructions in parentheses, which is called an instruction group for shell. As in the following example: (cd ~; vcgh= `pwd`; echo $vcgh), instruction groups have a feature that shell executes by generating subshell. Therefore, the variables defined in it act only on the instruction group itself. Let's look at an example.
# cat ftmpMuy01 echo a#./ftmp-01incgfsh binapash bashawings FSH (a=incg; FTMP-e\ "\ n $a\\ n\")
In addition to the above instruction groups, parentheses are also used in the definition of array variables; they are also used in other situations where escape characters may need to be added to use, such as expressions.
(())
The function of this set of symbols is similar to that of let instructions. It is used in arithmetic operations and is a built-in function of bash. Therefore, the execution efficiency is much better than using let instructions.
#! / bin/bash ((a = 10)) echo-e\ "inital value, a = $a\\ n\" ((averse +)) echo\ "after astatine, a = $a\"
{} curly braces (Block of code)
Sometimes it appears in the script, with one or more instructions or variable settings ending with\ "semicolon\" in curly braces.
# cat ftmpMuoo02BINHANGUBING FSH {a=inbc; echo-e\ "\ n $a\\ n\"} echo $a#./ftmp-02inbcinbc
This usage is very similar to the instruction group described above, except that it is executed in the current shell and does not produce subshell.
Curly braces are also used in the function function. In a broad sense, simply using curly braces acts like a function without a specified name. Therefore, it is also a good thing to write script in this way. Especially for the redirection of output and input, this approach can simplify the complexity of script.
In addition, there is another use of curly braces, as follows
{xx,yy,zz,...}
This combination of curly braces is often used in the combination of strings. Let's look at an example.
Mkdir {userA,userB,userC}-{home,bin,data}
We get userA-home, userA-bin, userA-data, userB-home, userB-bin,userB-data, userC-home, userC-bin,userC-data, these directories. This group of symbols has a wide range of applicability. If it can be put to good use, the reward is simplicity and efficiency. Like the following example
Chown root / usr/ {ucb/ {ex,edit}, lib/ {ex?.?*,how_ex}}
If it were not for the support for this usage, we would have to write a few lines and repeat them several times.
[] brackets
It often appears in the process control and plays the role of including judgment. If [\ "$?\"! = 0] thenecho\ "Executes error\" exit1fi
This symbol plays a role like\ "range\" or\ "set\" in regular expressions.
Rm-r 200 [1234]
The above example represents the deletion of 2001, 2002, 2003, 2004 and other directories.
[[]]
This set of symbols works basically the same as the previous [] symbols, but she allows the direct use of symbols such as | and & & logic in it.
#! / bin/bashread akif [[$ak > 5 | | $ak
< 9 ]]thenecho $akfi || 逻辑符号 这个会时常看到,代表 or 逻辑的符号。 && 逻辑符号 这个也会常看到,代表 and 逻辑的符号。 & 后台工作 单一个& 符号,且放在完整指令列的最后端,即表示将该指令列放入后台中工作。 tar cvfz data.tar.gz data >/ dev/null&
\\ word boundary
In regular expressions, this set of symbols is defined as the meaning of\ "boundary\". For example, when we are looking for the word the, if we use the
Grep the FileA
You will find that words like there are also treated as matching words. Because the happens to be part of there. If we want to avoid this situation, we have to add the symbol\ "boundary\".
Grep'\ 'FileA
+ plus sign (plus)
In the expression, she used to mean\ "addition\".
Expr 1 + 2 + 3
In addition, in regular expressions, it is used to indicate the meaning of the leading character of\ "many\".
The symbol # grep'10\ + 9 'fileB109100910000910000931010009# must be preceded by an escape character.
-minus sign (dash)
In the expression, she uses it to mean "subtraction".
Expr 10-2
It is also an option symbol for system instructions.
Ls-expr 10-2
In the GNU directive, if the-symbol is used alone, without any additional file name, it means\ "standard input\". This is a common option for GNU instructions. For example, the following example
Tar xpvf-
The-symbol here represents both reading data from standard input.
However, it is more special in the cd instruction
Cd-
This means changing the working directory to the last working directory.
% division (Modulo)
In an expression, it is used to represent\ "division\".
Expr 10 2
In addition, it is also applied to the following regular expressions about variables
${parameter%word} ${parameter%%word}
One% represents the shortest word match and two represents the longest word match.
= equal sign (Equals)
A symbol often seen when setting variables.
Vara=123echo\ "vara= $vara\"
Or like the setting of PATH, or even applied to such purposes as operations or judgments.
= = equal sign (Equals)
It is often seen in conditional judgment, which stands for\ "equal to\".
If [$vara = = $varb]
... The following outline
! = not equal to
It is often seen in conditional judgment that it stands for "not equal to".
If [$vara! = $varb]
... The following outline
^
In regular expressions, this symbol represents the\ "beginning\" position of the line, and in [] it also means "not" as well as\ "!\" (exclamation mark).
Output / input redirection
>
< &>2 & 2 > & 2
A file descriptor (File Descriptor) that uses a number (usually 0-9) to represent a file.
Common file descriptors are as follows:
File descriptor names commonly used abbreviations default values
0 standard input stdin keyboard
1 standard output stdout screen
2 standard error output stderr screen
When we simply use it, it is equivalent to using 0.
< 或 1>(described in more detail below).
* cmd > file
Redirect the output of the cmd command to the file file. If file already exists, empty the original file and use the noclobber option of bash to prevent the original file from being overwritten.
* cmd > > file
Redirect the output of the cmd command to the file file, and if the file already exists, add the information to the original file.
* cmd
< file 使cmd命令从file读入 * cmd &n 把输出送到文件描述符n cmd m>& n redirects the information output to the file character m to the file descriptor n
Cmd > &-turn off standard output
The effect of cmd-1 is different from that of cmd 2 > & 1 > file.
Thank you for reading, these are the contents of "what are the special symbols in the shell script". After the study of this article, I believe you have a deeper understanding of what special symbols are in the shell script, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.