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

IO redirection

You will learn the very useful concept of I/O redirection in this section.

File descriptors

All I/O, including files, pipes, and sockets, are handled by the kernel via a mechanism called the file descriptor. A file descriptor is a small unsigned integer, an index into a file-descriptor table maintained by the kernel and used by the kernel to reference open files and I/O streams. Each process inherits its own file-descriptor table from its parent. The first three file descriptors are 0, 1, and 2. File descriptor 0 is standard input (stdin), 1 is standard output (stdout), and 2 is standard error (stderr). When you open a file, the next available descriptor is 3, and it will be assigned to the new file.

Redirection

When a file descriptor is assigned to something other than a terminal, it is called I/O redirection. The shell performs redirection of output to a file by closing the standard output file descriptor 1 (the terminal) and then assigning that descriptor to the file. When redirecting standard input, the shell closes file descriptor 0 (the terminal) and assigns that descriptor to a file. The Bash shells handle errors by assigning a file to the file descriptor 2.

The following command will take input from the sample.txt file:

$ wc  <  sample.txt

The preceding command will take content from the sample.text file. The wc command will print the number of lines, words, and characters in the sample.txt file.

$ echo "Hello world" > log.txt

This command will redirect output to be saved in the log.txt file.

$ echo "Welcome to Shell Scripting" >> log.txt

This command will append the Hello World text in the log.txt file.

The single > will overwrite or replace the existing text in log file. And double >> will append the text in the log file.

Let's see a few more examples:

$ tr '[A-Z]' '[a-z]' < sample.txt

The preceding tr command will read text from the sample.txt file. The tr command will convert all uppercase letters to lower case letters and will print converted text on screen:

$ ls > log.txt
$ cat log.txt

The output of command will be as follows:

dir_1
sample.txt
extra.file

In this example command, ls is sending directory content to file log.txt. Whenever we want to store the result of the command in the file, we can use the preceding example.

$ date >> log.txt
$ cat log.txt

Output:

dir_1
dir_2
file_1
file_2
file_3
Sun Sept 17 12:57:22 PDT 2004

In the preceding example, we are redirecting and appending the result of the date command to the log.txt file.

$ gcc hello.c 2> error_file

The gcc is a C language compiler program. If an error is encountered during compilation, then it will be redirected to error_file. The > character is used for a success result and 2> is used for error results redirection. We can use error_file for debugging purposes:

$ find . –name "*.sh" > success_file 2> /dev/null

In the preceding example, we are redirecting output or success results to success_file and errors to /dev/null. /dev/null is used to destroy the data, which we do not want to be shown on screen.

$ find . –name "*.sh" &> log.txt

The preceding command will redirect both output and error to log.txt.

$ find . –name "*.sh"  > log.tx 2>&1

The preceding command will redirect result to log.txt and send errors to where the output is going, such as log.txt.

$ echo "File needs an argument" 1>&2

The preceding command will send a standard output to the standard error. This will merge the output with the standard error.

The summary of all I/O redirection commands will be as follows:

< sample.txt

The command will take input from sample.txt

> sample.txt

The success result will be stored in sample.txt

>> sample.txt

The successive outputs will be appended to sample.txt

2> sample.txt

The error results will be stored in sample.txt

2>> sample.txt

The successive error output will be appended to sample.txt

&> sample.txt

This will store success and errors, such as in sample.txt

>& sample.txt

This will store success and errors, such as in sample.txt (same as above)

2>&1

This will redirect an error to where output is going

1>&2

This will redirects output to where error is going

>|

This overrides no clobber when redirecting the output

<> filename

This uses the file as both standard input and output if a device file (from /dev)

cat xyz > success_file 2> error_file

This stores success and failure in different files

The following is the summary of various metacharacters:

Char

Meaning

Example

Possible output

*

Match with zero or multiple number of any character

$ ls –l *.c file*

Sample.c, hello.c, file1, file_2, filebc

?

Match any single character

$ ls –l file?

filea, fileb, file1

[..]

Match with any single character with in the bracket

$ ls –l file[abc]

filea, fileb,filec

;

Command separator

$cat filea; date

Displays the content of filea and displays the current date and time

|

Pipe two commands

$ cat filea | wc -l

Prints the number of lines of filea

()

Group commands, used when the output of the command group has to be redirected

$ (echo "***x.c***";cat x.c)>out

Redirects the content of x.c with a heading ***x.c*** to the file out

Run the following command:

$ touch filea fileb filec fileab filebc filead filebd filead
$ touch file{1,2,3}

Try the following command out:

$ ls  s*
$ ls  file
$ ls  file[abc]
$ ls  file[abc][cd]
$ ls  file[^bc]
$ touch file file1 file2 file3 … file20
$ ls ?????
file1
file2
file3
$ ls file*
file file1 file10 file2 file3
$ ls file[0-9]
file1 file2 file3
$ ls file[0-9]*
file1 file10 file2 file3
$ ls file[!1-2]
file3

Brace expansion

Curly braces allow you to specify a set of characters from which the shell automatically forms all possible combinations. To make this work, the characters to be combined with the given string must be specified as a comma separated list with no spaces:

$ touch file{1,2,3}
$ ls
Brace expansion
$ mkdir directory{1,2,3}{a,b,c}
$ ls
Brace expansion
$ touch file{a..z}
$ ls
Brace expansion

The following is the summary of various io-redirection and logical operators:

Brace expansion

For example:

$ ls || echo "Command un-successful"
$ ls a abcd || echo "Command un-successful"

These commands will print Command un-successful if the ls command is unsuccessful.