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 10. Using Advanced Functionality in Scripts

In the last chapter, you learned about using functions in Shell scripts and defining, displaying, and removing functions from the shell. You also learned about passing arguments to functions, sharing data between functions, declaring local variables in functions, returning results from functions, and running functions in the background. In the end, you learned about using source and . commands. You used these commands for using a library of functions.

In this chapter, you will learn the following topics:

  • Understanding signals and traps
  • Graphical menu development using the dialog utility

Understanding signals and traps

Two types of interrupts exist in the Linux operating system: the hardware interrupt and the software interrupt. Software interrupts are called signals or traps. Software interrupts are used for interprocess synchronizations.

Signals are used to notify about a certain event occurrence or to initiate a certain activity.

We use software signals many times, for example, if any command is not responding after it is typed, then you might have entered Ctrl + C. This sends a SIGINT signal to the process, and the process is terminated. In certain situations, we may want the program to perform a certain activity instead of terminating it using the Ctrl + C command. In such cases, we can use the trap command to ignore a signal or to associate our desired function with that signal.

In operating systems, software interrupts or signals are generated when the process attempts to divide a number by zero or due to power failure, system hang up, illegal instruction execution, or invalid memory access.

The action performed by a few signals is to terminate the process. We can configure the shell to do the following responses:

  • Catch the signal and execute user defined programs
  • Ignore the signal
  • Suspend the process (similar to Ctrl + Z)
  • Continue the process, which was suspended earlier

Enter either of following commands to get the full list of all signals:

$ kill -l
$ trap –l

Output:

Understanding signals and traps

If we want to know which keys are used for particular signals, then we enter the following command:

$ stty -a

The following is a list of a few of the standard signals that a process can use:

Number

Name

Description

Action

0

EXIT

The shell exits.

Termination

1

SIGHUP

The terminal has been disconnected.

Termination

2

SIGINT

The user presses Ctrl + C

Termination

3

SIGQUIT

The user presses Ctrl + \

Termination

4

SIGILL

This gives an illegal hardware instruction.

Program error

5

SIGTRAP

This is produced by debugger.

Program error

8

SIGFPE

This gives an arithmetic error, such as division by zero.

Program error

9

SIGKILL

This cannot be caught or ignored.

Termination

We can send either of the kill signals to a process with PID # 1234 as follows:

kill -9 1234
kill -KILL 1234
kill -SIGKILL 1234

As we can see, we can use a signal number or a signal name along with the process ID. By default, the kill command sends signal number 15 to process. Using the kill command, we can send the desired signal to any specific process.

We can stop a process using the Ctrl + Z signal as follows:

$ kill -19 pid

Ctrl + Z or SIGTSTP will stop the process.

We can run the stopped process by sending the SIGCONT signal.

$ kill -18 pid

The signal number of SIGCONT is 18.