Chapter 4. Working with Commands

In the last chapter, you learned about using more, less, head, and tail commands, and text processing tools like diff, cut, paste, comm, and uniq. You learned what standard input, output, and standard error are. You also learned about metacharacters and pattern matching using vi and grep.

In this chapter, you will cover the following topics:

  • Analyzing shell interpretation of commands
  • Working with command substitution
  • Working with command separators
  • Working with pipes

Learning shell interpretation of commands

When we log in, the $ sign will be visible in the shell terminal (# prompt if logged in as root or administrator). The Bash shell runs scripts as interpreter. Whenever we type a command, the BASH shell will read them as series of words (tokens). Each word is separated by a space ( ), semi colon (;), or any other command delimiter. We terminate the command by pressing the Enter key. This will insert a new line character at the end of the command. The first word is taken as a command, then consecutive words are treated as options or parameters.

The shell processes the command line as follows:

  • If applicable, substitution of history commands
  • Converting command line into tokens and words
  • Updating history
  • Processing of quotes
  • Defining functions and substitution of alias
  • Setting up of pipes, redirection, and background
  • Substitution of variables (such as $name and $user) is performed
  • Command substitution (echo `cal` and echo `date`) is performed
  • Globing is performed (file name substitution, such as rm *)
  • Execution of the command

The sequence of execution of different types of commands will be as follows:

  • Aliases (l, ll, egrep, and similar)
  • Keywords (for, if, while, and similar)
  • Functions (user defined or shell defined functions)
  • Built-in commands (bg, fg, source, cd, and similar)
  • Executable external commands and scripts (command from the bin and sbin folder)

Whenever a command is given in a shell or terminal, the complete command will be tokenized, and then shell will check if the command is alias.

Aliases, keywords, functions, and built-in commands are executed in the current shell, therefore their execution is fast as compared to executable external commands or scripts. Executable external commands will have a corresponding binary file or Shell script file in the file system, which will be stored in any folder. The shell will search the binary file or script of a command by searching in the PATH environment variable. If we want to know what the type of command it is, such as if it is an alias or a function or internal command, it can be found out by the type built-in command, which is shown as follows:

$ type mkdir
mkdir is /bin/mkdir

$ type cd
cd is a shell builtin

$ type ll
ll is aliased to `ls -alF'

$ type hello
hello is a function
hello ()
{
       echo "Hello World !";
}


$ type for
for is a shell keyword

Checking and disabling shell internal commands

Bash has provision of a few built-in commands to change the sequence of command line processing. We can use these built-in commands to change default behavior of command-line processing.

  • The build-in command will disable aliases and functions for the command which will be following the command. The shell will search for the external command and the built-in command will search for the command passed as an argument, as follows:
    $ command ls
    

    This will make aliases and functions be ignored and the external ls command will execute.

  • The builtin command will work as follows:
    $ builtin BUILT-IN
    

    This will ignore aliases and functions from the shell environment and only built-in commands and external commands will be processed.

  • The break built-in command will work as follows:
    $ builtin –n break
    

    This will make the break built-in to be disabled and the external command break will be processed.

  • To display all shell built-in commands, give the command as follows:
    $ enable
    
  • The output on the screen will show the following as shell internal commands:

    .

    command

    eval

    history

    pwd

    test

    ..

    compgen

    exec

    jobs

    read

    times

    [

    complete

    exit

    kill

    readarray

    trap

    alias

    compopt

    export

    let

    readonly

    true

    bg

    continue

    false

    local

    return

    type

    bind

    declare

    fc

    logout

    set, unset

    typeset

    break

    dirs

    fg

    mapfile

    shift

    ulimit

    builtin

    disown

    getopts

    popd

    shopt

    umask

    caller

    echo

    hash

    printf

    source

    unalias

    cd

    enable

    help

    pushd

    suspend

    wait

  • The shell built-in command can be disabled by following:
    $ enable –n built-in-command
    

    For example: $ enable –n test

    In this case, in my shell, if we have to test an external command, then instead of the internal test command, the external test command will be executed.

The exit status

In Shell scripting, we need to check if the last command has successfully executed or not. For example, whether a file or directory is present or not. As per the result, our Shell script will continue processing.

For this purpose, the BASH shell has one status variable ?. The status of the last command execution is stored in ?. The range of numerical value stored in ? will be from 0 to 255. If successful in execution, then the value will be 0; otherwise, it will be non-zero, which is as follows:

$ ls
$ echo $?
0

Here, zero as the return value indicates success.

In the next case, we see:

$ ls /root
$ echo $?
2

Here, non-zero value indicates an error in the last command execution.

In the next case, we see:

$ find / -name hello.c
$ echo $?

The return value will indicate if the hello.c file is present or not!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset