* Return value of the command
Each command returns its exit status (variable: "
$?
") as the return value.
Table 1.21. Command exit codes
command exit status | numeric return value | logical return value |
---|---|---|
success | zero, 0 | TRUE |
error | non-zero, -1 | FALSE |
For example, try the following.
$ [ 1 = 1 ] ; echo $? 0 $ [ 1 = 2 ] ; echo $? 1
Note | |
---|---|
Please note that, in the logical context for the shell, success is treated as the logical TRUE which has 0 (zero) as its value. This is somewhat non-intuitive and needs to be reminded here. |
Typical command sequences and shell redirection
Table 1.22. Shell command idioms
command idiom | description |
---|---|
command & | background execution of command in the subshell |
command1 | command2 | pipe the standard output of command1 to the standard input of command2 (concurrent execution) |
command1 2>&1 | command2 | pipe both standard output and standard error of command1 to the standard input of command2 (concurrent execution) |
command1 ; command2 | execute command1 and command2 sequentially |
command1 && command2 | execute command1 ; if successful, execute command2 sequentially (return success if both command1 and command2 are successful) |
command1 || command2 | execute command1 ; if not successful, execute command2 sequentially (return success if command1 or command2 are successful) |
command > foo | redirect standard output of command to a file foo (overwrite) |
command 2> foo | redirect standard error of command to a file foo (overwrite) |
command >> foo | redirect standard output of command to a file foo (append) |
command 2>> foo | redirect standard error of command to a file foo (append) |
command > foo 2>&1 | redirect both standard output and standard error of command to a file "foo " |
command < foo | redirect standard input of command to a file foo |
command << delimiter | redirect standard input of command to the following lines until "delimiter " is met (here document) |
command <<- delimiter | redirect standard input of command to the following lines until "delimiter " is met (here document, the leading tab characters are stripped from input lines) |
The Debian system is a multi-tasking system. Background jobs allow users to run multiple programs in a single shell. The management of the background process involves the shell builtins:
jobs
, fg
, bg
, and kill
. Please read sections of bash(1) under "SIGNALS", and "JOB CONTROL", and builtins(1).For example, try the following
$ </etc/motd pager
$ pager </etc/motd
$ pager /etc/motd
$ cat /etc/motd | pagerAlthough all 4 examples of shell redirections display the same thing, the last example runs an extra
cat
command and wastes resources with no reason.The shell allows you to open files using the
exec
builtin with an arbitrary file descriptor.$ echo Hello >foo $ exec 3<foo 4>bar # open files $ cat <&3 >&4 # redirect stdin to 3, stdout to 4 $ exec 3<&- 4>&- # close files $ cat bar HelloHere, "
n<&-
" and "n>&-
" mean to close the file descriptor "n
".The file descriptor 0-2 are predefined.
You can set an alias for the frequently used command.
For example, try the following
You can list any existing aliases by
For example, try the following
For example, try the following
$ alias la='ls -la'Now, "
la
" works as a short hand for "ls -la
" which lists all files in the long listing format.You can list any existing aliases by
alias
(see bash(1) under "SHELL BUILTIN COMMANDS").$ alias ... alias la='ls -la'You can identity exact path or identity of the command by
type
(see bash(1) under "SHELL BUILTIN COMMANDS").For example, try the following
$ type ls ls is hashed (/bin/ls) $ type la la is aliased to ls -la $ type echo echo is a shell builtin $ type file file is /usr/bin/fileHere
ls
was recently searched while "file
" was not, thus "ls
" is "hashed", i.e., the shell has an internal record for the quick access to the location of the "ls
" command.
No comments:
Post a Comment