Total Hit Counter

Scripting Methods & trimming output


*  Return value of the command

Each command returns its exit status (variable: "$?") as the return value.
Table 1.21. Command exit codes
command exit statusnumeric return valuelogical return value
successzero, 0TRUE
errornon-zero, -1FALSE

For example, try the following.
$ [ 1 = 1 ] ; echo $?
0
$ [ 1 = 2 ] ; echo $?
1
[Note]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

Let's try to remember following shell command idioms typed in one line as a part of shell command.
Table 1.22. Shell command idioms
command idiomdescription
command &background execution of command in the subshell
command1 | command2pipe the standard output of command1 to the standard input of command2 (concurrent execution)
command1 2>&1 | command2pipe both standard output and standard error of command1 to the standard input of command2 (concurrent execution)
command1 ; command2execute command1 and command2 sequentially
command1 && command2execute command1; if successful, execute command2 sequentially (return success if both command1 and command2 are successful)
command1 || command2execute command1; if not successful, execute command2 sequentially (return success if command1 or command2 are successful)
command > fooredirect standard output of command to a file foo (overwrite)
command 2> fooredirect standard error of command to a file foo (overwrite)
command >> fooredirect standard output of command to a file foo (append)
command 2>> fooredirect standard error of command to a file foo (append)
command > foo 2>&1redirect both standard output and standard error of command to a file "foo"
command < fooredirect standard input of command to a file foo
command << delimiterredirect standard input of command to the following lines until "delimiter" is met (here document)
command <<- delimiterredirect 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: jobsfgbg, 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 | pager
Although 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
Hello
Here, "n<&-" and "n>&-" mean to close the file descriptor "n".
The file descriptor 0-2 are predefined.
Table 1.23. Predefined file descriptors
devicedescriptionfile descriptor
stdinstandard input0
stdoutstandard output1
stderrstandard error2

Command alias

You can set an alias for the frequently used command.
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/file
Here 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: