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

Looping with the for command

For iterative operations, the bash shell uses three types of loops: for, while, and until. Using the for looping command, we can execute a set of commands for a finite number of times for every item in a list. In the for loop command, the user-defined variable is specified. After the in command, the keyword list of values can be specified. The user-defined variable will get the value from that list and all statements between do and done get executed until it reaches the end of the list.

The purpose of the for loop is to process a list of elements. It has the following syntax:

for variable in element1 element2 element3
do
commands
done

The simple script with the for loop could be as follows:

for command in clear date cal
do
  sleep 1
  $command
Done

In the preceding script, the commands clear, date, and cal will be called one after another. The sleep command will be called before every command for a second.

If we need to loop continuously or infinitely, then the following is the syntax:

for ((;;))
do
    command
done

Let's write a simple script for_01.sh. In this script, we will print the var variable 10 times:

#!/bin/bash
for var in {1..10}
do
  echo $var
done

Let's test the program:

$ chmod +x sor_01.sh
$ ./for_01.sh

The following will be the output after executing the preceding commands:

1
2
3
4
5
6
7
8
9
10

The following script for_02.sh uses the C programming style syntax:

#!/bin/bash
max=10
for  ((i=1; i<=max;  i++))
do
echo -n "$i     "    # one case with echo without –n option
done

Let's test the program:

$ chmod +x for_02.sh
$ ./for_02.sh

The following will be the output after executing the preceding commands:

$ ./for_02.sh      # OUTPUT with –n option
1     2     3     4     5     6     7     8     9     10
$ ./for_02.sh      # OUTPUT without –n option
1
2
3
4
5
6
7
8
9
10

In the next script for_03.sh, we will be processing a list of numbers, which are listed next to the in keyword:

#!/bin/bash
for var in 11 12 13 14 15 16 17 18 19 20
do
  echo $var
done

Let's test the program:

$ chmod +x for_03.sh
$ ./for_03.sh

The following will be the output after executing the preceding commands:

$ ./for_03.sh
11
12
13
14
15
16
17
18
19
20

In the following script for_04.sh, we create users11 to user20 along with their home directory:

#!/bin/bash
for var in user{11..20}
do
  useradd –m $var
  passwd -d $var
done

Let's test the program:

$ chmod +x for_04.sh.sh
$ sudo ./for_04.sh

The following will be the output after executing the preceding commands:

user11 to user20 will be created with their home folders in the /home/ folder. You need to be a root user or administrator to run this script.

In the for_05.sh script, we will be passing command-line parameters. All the command-line parameters will be available as the $* inside script:

#!/bin/sh
for var in $*
do
  echo "command line contains: $var"
done

Let's test the program:

$ chmod +x for_05.sh
$ ./for_05.sh 1 2 3 4 5 6

The following will be the output after executing the preceding commands:

command line contains: 1
command line contains: 2
command line contains: 3
command line contains: 4
command line contains: 5
command line contains: 6

In the next script for_06.sh, we are passing a list of words such as name of fruits. Inside the script, we are printing the information of variable:

#!/bin/bash
# create fruits.txt => Apple Mango Grapes Pears Banana Orange Pineapple
for var in `cat fruits.txt`
do
    echo "var contains: $var"
done

Let's test the program:

$ chmod +x for_06.sh
$ ./for_06.sh

The following will be the output after executing the preceding commands:

var contains: Apple
var contains: Mango
var contains: Grapes
var contains: Pears
var contains: Banana
var contains: Orange
var contains: Pineapple

Using the for_07.sh script, we generate a list of files with the ls shell command. This will be the list of filenames. In the for loop, the following list of files will be printed:

#!/bin/bash
echo -n "Commands in bin directory are : $var"
for var in $(ls /bin/*)
do
        echo -n -e "$var \t"
done

Let's test the program:

$ chmod +x for_07.sh
$ ./for_07

The following will be the output after executing the preceding commands:

This will print the content of /bin/ directory.

For taking a backup of files, we can write the for_08.sh script as follows:

#!/bin/bash
for filename in *.c
do
  echo "Copying $filename to $filename.bak"
  cp $filename $filename.bak 
done

Let's test the program:

$ chmod +x for_08.sh
$ touch 1.c 2.c
$ ./for_08.sh

The following will be the output after executing the preceding commands:

"Copying 1.c to 1.c.bak"
"Copying 2.c to 2.c.bak"