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 6. Neat Tricks with Shell Scripting

In the last chapter, you learned about shell and environment variables. You also learned about how to export environment variables, read-only variables, command-line arguments, and create/handle arrays.

In this chapter, we will cover following topics:

  • Interactive Shell scripts and reading from the keyboard
  • Using the here operator (<<) and here string (<<<)
  • File handling
  • Enabling debugging
  • Syntax checking
  • Shell tracing

Interactive Shell scripts – reading user input

The read command is a shell built-in command for reading data from a file or keyboard.

The read command receives the input from the keyboard or a file until it receives a newline character. Then, it converts the newline character into a null character:

  1. Read a value and store it in the variable, shown as follows:
    read variable
    echo $variable
    

    This will receive text from the keyboard. The received text will be stored in the variable.

  2. Command read with prompt. Whenever we need to display the prompt with certain text, we use the –p option. The option -p displays the text that is placed after –p on the screen:
    #!/bin/bash
    # following line will print "Enter value: " and then read data
    # The received text will be stored in variable value
    read -p "Enter value :  " value

    Output:

    Enter value : abcd
    
  3. If the variable name is not supplied next to the read command, then the received data or text will be stored in a special built-in variable called REPLY. Let's write a simple script read_01.sh, shown as follows:
    #!/bin/bash
    echo "Where do you stay ?"
    read     # we have not supplied any option or variable
    echo "You stay in $REPLY"

    Save the file, give the permission to execute, and run the script as follows:

    $ chmod u+x read_01.sh
    $ ./read_01.sh
    

    Output:

    "Where do you stay?"
    Mumbai
    "You stay at Mumbai"
    
  4. We will write the script read_02.sh. This script prompts the user to enter their first and last name to greet the user with their full name:
    #!/bin/bash
    echo "Enter first Name"
    read FIRSTNAME
    echo "Enter Last Name"
    read LASTNAME
    NAME="$FIRSTNAME $LASTNAME"
    echo "Name is $NAME"
  5. For reading text and storing in multiple variables, the syntax is as follows:
    $ read value1 value2 value3
    

    Let's write Shell script read_03.sh, shown as follows:

    #!/bin/bash
    echo "What is your name?"
    read fname mname lname
    echo "Your first name is : $fname"
    echo "Your middle name is : $mname"
    echo "Your last name is : $lname"

    Save the file, give the permission to execute, and run the script as follows:

    What is your name?
    Ganesh Sanjiv Naik
    "Your first name is : Ganesh"
    "Your middle name is : Sanjiv"
    "Your last name is : Naik"
  6. Let's learn about reading a list of words and storing them in an array:
    #!/bin/bash
    echo -n "Name few cities? "
    read -a cities
    echo "Name of city is ${cities[2]}."

    Save the file, give the permission to execute, and run the script as follows:

    Name few cities? Delhi London Washington Tokyo
    Name of city is Washington.

    In this case, the list of cities is stored in the array of cities. The elements in the array are here:

    cities[0] = Delhi
    cities[1] = London
    cities[2] = Washington
    cities[3] = Tokyo
    

    The index of the array starts with 0, and in this case, it ends at 3. In this case, four elements are added in the cities[] array.

  7. If we want the user to press the Enter key, then we can use the read command along with one unused variable, shown as follows:
    Echo "Please press enter to proceed further "
    read temp
    echo "Now backup operation will be started ! "
    

Summarizing the read command with options

The following table summarizes various read command-related options that you learned in the previous sections:

Format

Meaning

read

This command will read text from a keyboard and store the received text in a built-in variable REPLY.

read value

This reads a text from a keyboard or standard input and stores it into the variable value.

read first last

This will read the first word in variable first and the remaining text of the line in variable last. The first word is separated by white space from the remaining words in the line.

read –e

This is used in interactive shells for command-line editing. If vi editor is used, then vi commands can be used.

read –a array_name

This will store a list of words received in to an array.

read –r line

The text with backslash can be received here.

read –p prompt

This will print the prompt and wait for the user input. The received text will be stored in the variable REPLY.