Table of Contents for
Learning Linux Shell Scripting

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Learning Linux Shell Scripting by Ganesh Sanjiv Naik Published by Packt Publishing, 2015
  1. Cover
  2. Table of Contents
  3. Learning Linux Shell Scripting
  4. Learning Linux Shell Scripting
  5. Credits
  6. About the Author
  7. Acknowledgments
  8. About the Reviewers
  9. www.PacktPub.com
  10. Preface
  11. What you need for this book
  12. Who this book is for
  13. Conventions
  14. Reader feedback
  15. Customer support
  16. 1. Getting Started and Working with Shell Scripting
  17. Tasks done by shell
  18. Working in shell
  19. Learning basic Linux commands
  20. Our first script – Hello World
  21. Compiler and interpreter – difference in process
  22. When not to use scripts
  23. Various directories
  24. Working more effectively with shell – basic commands
  25. Working with permissions
  26. Summary
  27. 2. Drilling Deep into Process Management, Job Control, and Automation
  28. Monitoring processes using ps
  29. Process management
  30. Process monitoring tools – top, iostat, and vmstat
  31. Understanding "at"
  32. Understanding "crontab"
  33. Summary
  34. 3. Using Text Processing and Filters in Your Scripts
  35. IO redirection
  36. Pattern matching with the vi editor
  37. Pattern searching using grep
  38. Summary
  39. 4. Working with Commands
  40. Command substitution
  41. Command separators
  42. Logical operators
  43. Pipes
  44. Summary
  45. 5. Exploring Expressions and Variables
  46. Working with environment variables
  47. Working with read-only variables
  48. Working with command line arguments (special variables, set and shift, getopt)
  49. Understanding getopts
  50. Understanding default parameters
  51. Working with arrays
  52. Summary
  53. 6. Neat Tricks with Shell Scripting
  54. The here document and the << operator
  55. The here string and the <<< operator
  56. File handling
  57. Debugging
  58. Summary
  59. 7. Performing Arithmetic Operations in Shell Scripts
  60. Using the let command for arithmetic
  61. Using the expr command for arithmetic
  62. Binary, octal, and hex arithmetic operations
  63. A floating-point arithmetic
  64. Summary
  65. 8. Automating Decision Making in Scripts
  66. Understanding the test command
  67. Conditional constructs – if else
  68. Switching case
  69. Implementing simple menus with select
  70. Looping with the for command
  71. Exiting from the current loop iteration with the continue command
  72. Exiting from a loop with a break
  73. Working with the do while loop
  74. Using until
  75. Piping the output of a loop to a Linux command
  76. Running loops in the background
  77. The IFS and loops
  78. Summary
  79. 9. Working with Functions
  80. Passing arguments or parameters to functions
  81. Sharing the data by many functions
  82. Declaring local variables in functions
  83. Returning information from functions
  84. Running functions in the background
  85. Creating a library of functions
  86. Summary
  87. 10. Using Advanced Functionality in Scripts
  88. Using the trap command
  89. Ignoring signals
  90. Using traps in function
  91. Running scripts or processes even if the user logs out
  92. Creating dialog boxes with the dialog utility
  93. Summary
  94. 11. System Startup and Customizing a Linux System
  95. User initialization scripts
  96. Summary
  97. 12. Pattern Matching and Regular Expressions with sed and awk
  98. sed – noninteractive stream editor
  99. Using awk
  100. Summary
  101. Index

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!