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

Working with arrays

An array is a list of variables. For example, we can create an array FRUIT, which will contain many fruit names. The array does not have a limit on how many variables it may contain. It can contain any type of data. The first element in an array will have the index value as 0:

student@ubuntu:~$ FRUITS=(Mango Banana Apple)
student@ubuntu:~$ echo ${FRUITS[*]}
Mango Banana Apple
student@ubuntu:~$ echo $FRUITS[*]
Mango[*]
student@ubuntu:~$ echo ${FRUITS[2]}
Apple
student@ubuntu:~$ FRUITS[3]=Orange
student@ubuntu:~$ echo ${FRUITS[*]}
Mango Banana Apple Orange

Creating an array and initializing it

You will learn about creating an array in the Bash shell.

If the array name is FRUIT, then we can create an array as follows:

FRUIT[index]=value

Index is the integer value. It should be 0 or any positive integer value.

We can also create an array as follows:

$ declare -a array_name
$ declare -a arrayname=(value1 value2 value3)

Example:

$ declare -a fruit=('Mango' 'Banana' 'Apple' 'Orange' 'Papaya')
$ declare -a array_name=(word1 word2 word3 ...)
$ declare -a fruit=( Pears Apple Mango Banana Papaya )
$ echo ${fruit[0]}
Pears
$ echo ${fruit[1]}
Apple
$ echo "All the fruits are ${fruit[*]}"
    All the fruits are Pears Apple Mango Banana Papaya
$ echo "The number of elements in the array are ${#fruit[*]}"
    The number of elements in the array are 3
$ unset fruit  or unset ${fruit[*]}

Accessing array values

Once we have initialized an array, we can access it as follows:

${array_name[index]}

We will create the script array_01.sh as follows:

#!/bin/bash

FRUIT[0]="Pears"
FRUIT[1]="Apple"
FRUIT[2]="Mango"
FRUIT[3]="Banana"
FRUIT[4]="Papaya"
echo "First Index: ${FRUIT[0]}"
echo "Second Index: ${FRUIT[1]}"

output:

$ chmod +x array_01.sh
$./array_01.sh
First Index: Pears
Second Index: Apple

To display all the items from the array, use the following commands:

${FRUIT[*]}
${FRUIT[@]}

Create an array_02.sh script as follows:

#!/bin/bash
FRUIT[0]="Pears"
FRUIT[1]="Apple"
FRUIT[2]="Mango"
FRUIT[3]="Banana"
FRUIT[4]="Papaya"
echo "Method One : ${FRUIT[*]}"
echo "Method Two : ${FRUIT[@]}"

Output:

$ chmod +x array_02.sh
$./ array_02.sh
Method One : Pears Apple Mango Banana Papaya
Method Two : Pears Apple Mango Banana Papaya

Let's see few more examples:

$ city[4]=Tokyo

The fourth member of the array, city, is being assigned Tokyo. Since it is the only element in the array, array the size will be 1.

$ echo ${city[*]}
Tokyo

The size of the array city is 1, since any other member of the array is not yet initialized.

${city[*]} will display the only element of the array city.

$ echo ${city[0]}

city[0] has no value, and neither does city[1] and city[2].

$ echo ${city[4]}
Tokyo

city[4] has the name of city as Tokyo.

$ countries=(USA  [3]=UK  [2]=Spain)

The array countries are being assigned USA at index 0, UK at index 3, and Spain at index 2. We can observe here that it does not matter in which sequence we are initializing the members of the array. They need not be in same sequence.

$ echo ${countries[*]}
USA Spain UK
$ echo ${countries[0]}
USA

The first element of the countries array is printed.

$ echo ${countries[1]}

There is nothing stored in countries [1].

$ echo ${countries[2]}
Spain

The third element of the countries array, countries [2], was assigned Spain.

$ echo ${countries[3]}
UK

The fourth element of the countries array, countries [3], was assigned UK.