(21) Trap

    The trap command is used to specify the
actions to take on receipt of signals. A common use is to tidy up a
script when it is interrupted.
    The trap command is passed the action to take, followed by the signal name (or names) to trap on:

        trap command signal
that the scripts are normally interpreted from top to bottom, so you
must specify the trap command before the part of the script you wish to

    To see the signal numbers and associated names, you can just type trap -l at a command prompt.
To reset a trap condition to the default, simply specify the command as
-. To ignore a signal, set the command to the empty string ‘’.

    Signal                                Description
    HUP (1)         Hang up; usually sent when a terminal goes offline, or a user logs out
    INT (2)         Interrupt; usually sent by pressing Ctrl+C
    QUIT (3)       Quit; usually sent by pressing Ctrl+/
    ABRT (6)       Abort; usually sent on some serious execution error
    ALRM (14)      Alarm; usually used for handling timeouts
    TERM (15)     Terminate; usually sent by the system when it’s shutting down

(22) unset
 The unset command removes variables or functions from the environment.
It can’t do this to read-only variables defined by the shell itself,
such as IFS.

(23) find
    The full syntax for the find command is as follows:
        find [path] [options] [tests] [actions]

    Option                    Meaning
    -depth                    Search the contents of a directory before looking at the directory itself.
    -follow                    Follow symbolic links.
    -maxdepths N          Search at most N levels of the directory when searching.
    -mount (or -xdev)     Don’t search directories on other file systems.

Test                         Meaning
-atime N        The file was last accessed N days ago.    
-mtime N        The file was last modified N days ago.        
-name pattern    The name of the file, excluding any path, matches the pattern pro-
                         vided. To ensure that the pattern is passed to find, and not evaluated
                         by the shell immediately, the pattern must always be in quotes.
-newer otherfile    The file is newer than the file otherfile.
-type C             The file is of type C, where C can be of a particular type; the most
                     common are “d” for a directory and “f” for a regular file. For other
                     types consult the manual pages.
-user username    The file is owned by the user with the given name.

Operator,     Short Form Operator,     Long Form Meaning
!                    -not                            Invert the test.
-a                   -and                        Both tests must be true.
-o                   -or                            Either test must be true.

Action                        Meaning
-exec command    Execute a command. This is one of the most common actions. See the
                          explanation following this table for how parameters may be passed to
                          the command. This action must be terminated with a /; character pair.
-ok command        Like -exec, except that it prompts for user confirmation of each file on
                           which it will carry out the command before executing the command.
                          This action must be terminated with a /; character pair.
-print                     Print out the name of the file.
-ls                        Use the command ls -dils on the current file.

(24) grep
    The grep command takes options, a pattern to match, and files to search in:
            grep [options] PATTERN [FILES]

Option     Meaning
-c        Rather than print matching lines, print a count of the number of linesthat match.      
-E        Turn on extended expressions.      
-h        Suppress the normal prefixing of each output line with the name of the file it was found in.       
-i         Ignore case.      
-l         List the names of the files with matching lines; don’t output the actual matched line.      
-v        Invert the matching pattern to select nonmatching lines, rather than matching lines.

(25) Regular Expressions
    The most frequently used are shown in the following table:

   Character                    Meaning
   ^                           Anchor to the beginning of a line
   $                           Anchor to the end of a line
   .                            Any single character
   [ ]                        The square braces contain a range of characters, any one of which
                                may be matched, such as a range of characters like a–e or an inverted
                                range by preceding the range with a ^ symbol.
If you want to use any of these characters as “normal” characters, precede them with a /.

There are also some useful special match patterns that can be used in square braces, as described in the following table:
    Match Pattern                        Meaning
    [:alnum:]                        Alphanumeric characters
    [:alpha:]                         Letters
    [:ascii:]                           ASCII characters
    [:blank:]                          Space or tab
    [:cntrl:]                          ASCII control characters
    [:digit:]                           Digits
    [:graph:]                         Noncontrol, nonspace characters
    [:lower:]                          Lowercase letters
    [:print:]                          Printable characters
    [:punct:]                        Punctuation characters
    [:space:]                        Whitespace characters, including vertical tab
    [:upper:]                         Uppercase letters
    [:xdigit:]                         Hexadecimal digits

In addition, if the -E
for extended matching is also specified, other characters that control
the completion of matching may follow the regular expression (see the
following table). With grep it is also necessary to precede these
characters with a /.

    $ grep "e$" words2.txt        # this finds lines that end in the letter e.
    $ grep a[[:blank:]] words2.txt        # find words that end with the letter a.  [[:blank:]]  tests for a space or a tab
    $ grep -E [a-z]/{10/} words2.txt        #
use the extended grep mode to search for lowercase words that are
exactly 10 characterslong. Do this by specifying a range of           
                                                          # characters
to match a to z, and a repetition of 10 matches

(26) Command Execution
    Execute a command and put the output of the command into a variable. You can do this by using the $(command) syntax.The result of the $(command) is simply the output from the command. If you want to get the result into a variable, you can just assign it in the usual way:
         echo $whoisthere

(27) Arithmetic Expansion
    The expr
command can enable simple arithmetic commands to be processed, but this
is quite slow to execute because a new shell is invoked to process the expr command.
    A newer and better alternative is $((...)) expansion. By enclosing the expression you wish to evaluate in $((...)), you can perform simple arithmetic much more efficiently.


while [ "$x" -ne 10 ]; do
    echo $x

exit 0

(28) Parameter Expansion

substitutions are often useful when you’re working with strings. The
last four, which remove parts of strings, are especially useful for
processing filenames and paths, as the following example shows.


unset foo
echo ${foo:-bar}

echo ${foo:-bar}

echo ${foo#*/}
echo ${foo##*/}

echo ${bar%local*}
echo ${bar%%local*}

exit 0

The output is:


(29) Here Document
special way of passing input to a command from a shell script is to use
a here document. This document allows a command to execute as though it
were reading from a file or the keyboard, whereas in fact it’s getting
input from the script.

A here document starts with the leader <<, followed by a special sequence of characters that is repeated at the end of the document. <<
is the shell’s label redirector, which in this case forces the command
input to be the here document. This special sequence acts as a marker
to tell the shell where the here document ends. The marker sequence
must not appear in the lines to be passed to the command, so it’s best
to make them memorable and fairly unusual.


cat <<!FUNKY!
this is a here


4.7 Debugging Shell
 Since scripts are interpreted, there’s no compilation overhead in
modifying and retrying a script. The main way to trace more complicated
errors is to set various shell options. To do this, you can either use
command-line options after invoking the shell or use the set command.
The following table summarizes the options:

4.8 Dialog Utility

# Ask some questions and collect the answer
dialog --title "Questionnaire" --msgbox "Welcome to my simple survey" 9 18

dialog --title "Confirm" --yesno "Are you willing to take part?" 9 18
if [ "$?" != 0 ]; then
  dialog --infobox "Thank you anyway" 5 20
  sleep 2
  dialog --clear
  exit 0

dialog --title "Questionnaire" --inputbox "Please enter your name" 9 30 2>_1.txt
Q_NAME=$(cat _1.txt)

dialog --menu "$Q_NAME, what music do you like best?" 15 30 4 1 "Classical" 2 "Jazz" 3 "Country" 4 "Other" 2>_1.txt
Q_MUSIC=$(cat _1.txt)

if [ "$Q_MUSIC" = "1" ]; then
  dialog --title "Likes Classical" --msgbox "Good choice!" 12 25
  dialog --title "Doesn’t like Classical" --msgbox "Shame" 12 25

sleep 2
dialog --clear
exit 0

    You start with a simple welcome screen, before asking the user if he will take part using the simple --yesno option of dialog. You use the $? variable to check the reply. If he agreed, you then get his name, store it in a variable Q_NAME, and ask what sort of music he likes, using the --menu option of dialog. By storing the numerical output in the variable Q_MUSIC, you can see what he answered, and give an appropriate response.