3 基本的Shell命令
3.1 Redirecting Output
管道重定向:>
对文件追加:>>
For ">", by default, if the file already exists, then it will be overwritten. If you want to change the default behavior, you can use the command set -o noclobber (or set -C), which sets the noclobber option to prevent a file from being overwritten using redirection. You can cancel this option using set +o noclobber.
For ">>", outputs are appended to the end of the file.
File descriptor 0 is the standard input to a program, file descriptor 1 is the standard output, and file descriptor 2 is the standard error output. It’s unusual to want to redirect any other than the standard ones: 0, 1, and 2.
To redirect the standard error output, preface the > operator with the number of the file descriptor you wish to redirect. Because the standard error is on file descriptor 2, use the 2> operator. This is often useful to discard error information and prevent it from appearing on the screen.
The command
$ kill -HUP 1234 >killout.txt 2>killerr.txt
will put the output and error information into separate files.
If you prefer to capture both sets of output into a single file, you can use the >& operator to combine the two outputs. Therefore,
$ kill -1 1234 >killouterr.txt 2>&1
will put both the output and error outputs into the same file. Notice the order of the operators. This reads as “redirect standard output to the file killouterr.txt, and then direct standard error to the same place as the standard output.” If you get the order wrong, the redirect won’t work as you expect.
3.2 Redirecting Input
<
3.3 Pipes
Processes connected by pipes can run simultaneously and are automatically rescheduled as data flows between them.
3.4 file command
The best way to check if a file is a script or not is to use the file command — for example, file first or file /bin/bash.
3.5 read command
3.7 touch filename
Check whether it exists and create it if it doesn’t.
4 Shell Scripts
If your script is invoked with parameters, some additional variables are created. If no parameters are passed, the environment variable $# still exists but has a value of 0.
do
statements
done
Note:
for file in $(ls f*.sh); do
In this example, the parameter list for the for command is provided by the output of the command enclosed in the $() sequence.
(3)
while condition do
statements
done
(4)
until condition
do
statements
done
Note: In general, if a loop should always execute at least once, use a while loop; if it may not need to execute at all, use an until loop.
(5)
case variable in
pattern [ | pattern] ...) statements;;
pattern [ | pattern] ...) statements;;
...
esac
Note:
(6) Lists
If you want to use multiple statements in a place where only one is allowed, such as in an AND or OR list, you can do so by enclosing them in braces {} to make a statement block.
(8) Shell Function
function_name () {
statements
}
3.1 Redirecting Output
管道重定向:>
对文件追加:>>
For ">", by default, if the file already exists, then it will be overwritten. If you want to change the default behavior, you can use the command set -o noclobber (or set -C), which sets the noclobber option to prevent a file from being overwritten using redirection. You can cancel this option using set +o noclobber.
For ">>", outputs are appended to the end of the file.
File descriptor 0 is the standard input to a program, file descriptor 1 is the standard output, and file descriptor 2 is the standard error output. It’s unusual to want to redirect any other than the standard ones: 0, 1, and 2.
To redirect the standard error output, preface the > operator with the number of the file descriptor you wish to redirect. Because the standard error is on file descriptor 2, use the 2> operator. This is often useful to discard error information and prevent it from appearing on the screen.
The command
$ kill -HUP 1234 >killout.txt 2>killerr.txt
will put the output and error information into separate files.
If you prefer to capture both sets of output into a single file, you can use the >& operator to combine the two outputs. Therefore,
$ kill -1 1234 >killouterr.txt 2>&1
will put both the output and error outputs into the same file. Notice the order of the operators. This reads as “redirect standard output to the file killouterr.txt, and then direct standard error to the same place as the standard output.” If you get the order wrong, the redirect won’t work as you expect.
3.2 Redirecting Input
<
3.3 Pipes
Processes connected by pipes can run simultaneously and are automatically rescheduled as data flows between them.
3.4 file command
The best way to check if a file is a script or not is to use the file command — for example, file first or file /bin/bash.
3.5 read command
You can assign user input to a variable by using the
read command.The read normally completes when the user presses Enter.
3.6
which
Check which command is used, e.g.
which test.
3.7 touch filename
Check whether it exists and create it if it doesn’t.
4 Shell Scripts
4.1 Quoting
If you enclose a $ variable expression in double quotes, then it’s replaced with its value when the line is executed. If you enclose it in single quotes, then no substitution takes place. You can also remove the special meaning of the $ symbol by prefacing it with a /.
Usually, strings are enclosed in double quotes, which protects variables from being separated by whitespace but allows $ expansion to take place.
4.2 Environment Variables
If you want to check out how the program works in a different environment by running the
env <command>, try looking at the env manual pages.
4.3 Parameter Variables
If your script is invoked with parameters, some additional variables are created. If no parameters are passed, the environment variable $# still exists but has a value of 0.
As you can see, within double quotes, $@ expands the positional parameters as separate fields, regardless of the IFS value. In general, if you want access to the parameters, $@ is the sensible choice.
4.4 A Problem With Variables
For a script
if [ $timeofday = “yes” ]
, if just press "Enter",rather than answer anything.When the variable timeofday was tested, it consisted of a blank string. Therefore, the if clause looks like
if [ = “yes” ]
which isn't a valid condition. To avoid this, you must use quotes around the variable:
if [ “$timeofday” = “yes” ]
An empty variable then gives the valid test:
if [ “” = “yes” ]
Remember that adding the quotes tells the shell to consider everything between them as a single string. This is one way of getting spaces to be stored in a variable.
4.5 The test or [ Command
For a script
if [ $timeofday = “yes” ]
, if just press "Enter",rather than answer anything.When the variable timeofday was tested, it consisted of a blank string. Therefore, the if clause looks like
if [ = “yes” ]
which isn't a valid condition. To avoid this, you must use quotes around the variable:
if [ “$timeofday” = “yes” ]
An empty variable then gives the valid test:
if [ “” = “yes” ]
Remember that adding the quotes tells the shell to consider everything between them as a single string. This is one way of getting spaces to be stored in a variable.
4.5 The test or [ Command
The
[ and
test commands are synonymous, except that when the
[ command is used, a trailing
] is also used. The
test or
[ can be used to check to see whether a file exists.
e.g.
test -f <filename> or
[ -f <filename> ]
(
Note: You must put spaces between the [ braces and the condition being checked.)
Conditions that you can use
test command are:
4.6 Control Structures
(1)
if condition
then
statements
elif condition ; then
else
statements
fi
(2)
for
variable
in
values
if condition
then
statements
elif condition ; then
else
statements
fi
(2)
do
statements
done
Note:
for file in $(ls f*.sh); do
In this example, the parameter list for the for command is provided by the output of the command enclosed in the $() sequence.
(3)
while condition do
statements
done
(4)
until condition
do
statements
done
Note: In general, if a loop should always execute at least once, use a while loop; if it may not need to execute at all, use an until loop.
(5)
case variable in
pattern [ | pattern] ...) statements;;
pattern [ | pattern] ...) statements;;
...
esac
Note:
- Notice that each pattern line is terminated with double semicolons (;;). You can put multiple statements between each pattern and the next, so a double semicolon is needed to mark where one statement ends and the next pattern begins.
- Be careful with the case construct if you are using wildcards such as ‘*‘ in the pattern. The problem is that the first matching pattern will be taken, even if a later pattern matches more exactly.
(6) Lists
- AND List: The AND list construct enables you to execute a series of commands, executing the next command only if all the previous commands have succeeded. The syntax is
statement1 && statement2 && statement3 && ...
- OR List: The OR list construct enables us to execute a series of commands until one succeeds, and then not execute any more. The syntax is as follows:
statement1 || statement2 || statement3 || ...
If you want to use multiple statements in a place where only one is allowed, such as in an AND or OR list, you can do so by enclosing them in braces {} to make a statement block.
(8) Shell Function
function_name () {
statements
}