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

Pattern searching using grep

The g/RE/p stands for globally search for the regular expression (RE) and print out the line.

Return status – success 0, pattern not found 1, file not found 2.

$  ps -ef | grep root

The preceding command will show all processes running currently whose user ID is "root".

$  ll /proc | grep "cpuinfo"

The preceding command will show the file with the name cpuinfo from the /proc directory.

$  grep –lir "text" *   // only file names  //   
$ grep –ir  "text" dir_name   // show lines of files //

We will try the following commands on the love.txt file:

Metacharacter

Function

Example

Description

^

Beginning-of-line anchor

'^mango'

Will display all lines beginning with mango

$

End-of-line anchor

'mango'$'

Will display all lines ending with mango

.

Matches single character

'm..o'

Will display lines containing m, followed by two characters, followed by an o

*

Matches zero or more characters preceding the asterisk

'*mango'

Will display lines with zero or more spaces, followed by the pattern mango

[ ]

Matches single character in the set

'[Mm]ango'

Will display lines containing Mango or mango

[^]

Matches single character not in the set

'[^A–M]ango'

Will display lines not containing a character in the range A through M, followed by ango

\<

Beginning-of-word anchor

'\<mango'

Will display lines containing a word that begins with mango

\>

End-of-word anchor

'mango\>'

Will display lines containing a word that ends with mango

We will create a new file sample.txt, as follows:

Apple  Fruit    5  4.5
Potato  Vegetable  4  .5
Onion  Vegetable  .3  8
Guava  Fruit    5  1.5
Almonds  Nuts    1  16
Tomato  Vegetable  3  6
Cashew  Nuts    2  12
Mango    Fruit    6  6
Watermelon  Fruit    5  1

We will try the following commands on the sample.txt file:

Sr. no.

Command

Description

1

grep Fruit sample.txt

This will show all lines with pattern Fruit.

2

grep Fruit G*

This searches pattern Fruit in all files starting with G.

3

grep '^M' sample.txt

This searches all lines starting with M.

4

grep '6$' sample.txt

This searches lines ending with 6.

5

grep '1\..' sample.txt

This displays lines containing 1 and any character after it.

6

grep '\.6' sample.txt

This shows lines containing .6.

7

grep '^[AT]' sample.txt

This searches lines starting with A or T.

8

grep '[^0-9]' sample.txt

This contains at least one alphabet.

9

grep '[A-Z][A-Z] [A-Z]' sample.txt

This searches the upper case, upper case space, and upper case word.

10

grep '[a-z]\{8\}' sample.txt

This displays all lines in which there are at least eight consecutive lowercase letters.

11

grep '\<Fruit' sample.txt

This displays all lines containing a word starting with Fruit. The \< is the beginning-of-word anchor.

12

grep '\<Fruit\>' sample.txt

This displays the line if it contains the word Fruit.

The \< is the beginning-of-word anchor and the \> is the end-of-word anchor.

13

grep '\<[A-Z].*o\>' sample.txt

This displays all lines containing a word starting with an uppercase letter, followed by any number of characters and a word ending in o.

14

grep -n '^south' sample.txt

This displays line numbers also.

15

grep –i 'pat' sample.txt

This displays case insensitive search.

16

grep -v 'Onion' sample.txt > temp

mv temp sample.txt

This deletes the line containing pattern.

17

grep –l 'Nuts' *

This lists files containing pattern.

18

grep –c 'Nuts' sample.txt

This prints the number of lines where pattern is present.

19

grep –w 'Nuts' sample.txt

This counts where the whole world pattern is present, not a part of the word.