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 command line arguments (special variables, set and shift, getopt)

Command line arguments are required for the following reasons:

  • They inform the utility or command as to which file or group of files to process (reading/writing of files)
  • Command line arguments tell the command/utility which option to use

Check the following command line:

student@ubuntu:~$  my_program   arg1  arg2  arg3

If my_command is a bash Shell script, then we can access every command line positional parameters inside the script as follows:

$0 would contain "my_program"    # Command
$1 would contain "arg1"      # First parameter
$2 would contain "arg2"      # Second parameter
$3 would contain "arg3"      # Third parameter

The following is the summary of positional parameters:

$0

Shell script name or command

$1–$9

Positional parameters 1–9

${10}

Positional parameter 10

$#

Total number of parameters

$*

Evaluates to all the positional parameters

$@

Same as $*, except when double quoted

"$*"

Displays all parameters as "$1 $2 $3", and so on

"$@"

Displays all parameters as "$1" "$2" "$3", and so on

Let's create a script param.sh as follows:

#!/bin/bash
echo "Total number of parameters are = $#"
echo "Script name = $0"
echo "First Parameter is $1"
echo "Second Parameter is $2"
echo "Third Parameter is $3"
echo "Fourth Parameter is $4"
echo "Fifth Parameter is $5"
echo "All parameters are = $*"

Then as usual, give execute permission to script and then execute it:

./parameter.sh London Washington Delhi Dhaka Paris

Output:

Total number of parameters are = 5
Command is = ./parameter.sh
First Parameter is London
Second Parameter is Washington
Third Parameter is Delhi
Fourth Parameter is Dhaka
Fifth Parameter is Paris
All parameters are = London Washington Delhi Dhaka Paris

Understanding set

Many times we may not pass arguments on the command line, but we may need to set parameters internally inside the script.

We can declare parameters by the set command as follows:

$ set USA Canada UK France
$ echo $1
USA
$ echo $2
Canada
$ echo $3
UK
$ echo $4
France

We can use this inside the set_01.sh script as follows:

#!/bin/bash
set USA Canada UK France
echo $1
echo $2
echo $3
echo $4

Run the script as:

$ ./set.sh

Output:

USA
Canada
UK
France

Table declare Options

Option

Meaning

–a

An array will be created

–f

Displays function names and definitions

–F

Displays only the function names

–i

Makes variables integer types

–r

Makes variables read-only

–x

Exports variables

We give commands as follows:

set One Two Three Four Five
echo $0      # This will show command
echo $1       # This will show first parameter
echo $2
echo $*    # This will list all parameters
echo $#    # This will list total number of parameters
echo ${10} ${11}  # Use this syntax for parameters for 10th and
         # 11th parameters

Let us write script set_02.sh as follows:

#!/bin/bash
echo The date is $(date)
set $(date)
echo The month is $2
exit 0

Output:

Understanding set

In the script $(date), the command will execute and the output of that command will be used as $1, $2, $3 and so on. We have used $2 to extract the month from the output.

Let's write script set_03.sh as follows:

#!/bin/bash

echo "Executing script $0"
echo $1 $2 $3

set eins zwei drei
echo "One two three in German are:"
echo "$1"
echo  "$2"
echo  "$3"

textline="name phone address birthdate salary"
set $textline
echo  "$*"
echo 'At this time $1 = ' $1 'and $4 = ' $4

Output:

Understanding set

In this script, the following output shows:

  1. Initially when the set is not called, then $1, $2, $3 do not contain any information.
  2. Then, we set $1 to $3 as GERMAN numerals in words.
  3. Then, set $1 to $5 as name, phone, address, birthdate, and salary, respectively.

Understanding shift

Using shift, we can change the parameter to which $1 and $2 are pointing to the next variable.

Create a script shift_01.sh as follows:

#!/bin/bash
echo "All Arguments Passed are as follow : "
echo $*
echo "Shift By one Position :"
shift
echo "Value of Positional Parameter $ 1 after shift :"
echo $1
echo "Shift by Two Positions :"
shift 2
echo "Value of Positional Parameter $ 1 After two Shifts :"
echo $1

Execute the command as follows:

$ chmod +x shift_01.sh
$ ./shift_01.sh One Two Three Four

Output:

student@ubuntu$ ./shift_01.sh One Two Three Four

All arguments passed are as follows:

One Two Three Four

Shift by one position.

Here, the value of the positional parameter $1 after shift is:

Two

Shift by two positions.

The value of the positional parameter $1 after two shifts:

Four

We observed that initially $1 was One. After shift, $1 will be pointing to Two. Once shift is done, the value in position 1 is always destroyed and is inaccessible.

Create a shift_02.sh script as follows:

#!/bin/bash

echo '$#: ' $#
echo '$@: ' $@
echo '$*: ' $*
echo
echo '$1 $2 $9 $10 are: ' $1 $2 $9 $10
echo

shift
echo '$#: ' $#
echo '$@: ' $@
echo '$*: ' $*
echo
echo '$1 $2 $9 are: ' $1 $2 $9

shift 2
echo '$#: ' $#
echo '$@: ' $@
echo '$*: ' $*
echo
echo '$1 $2 $9 are: ' $1 $2 $9

echo '${10}: ' ${10}
Understanding shift

In this script execution, the following output shows:

  1. Initially, $1 to $13 were numerical values 1 to 13, respectively.
  2. When we called the command shift, then $1 shifted to number 2 and accordingly all $numbers are shifted.
  3. When we called the command shift 2, then $1 shifted to number 4 and accordingly all $numbers are shifted.

Resetting positional parameters

In certain situations, we may need to reset original positional parameters.

Let's try the following:

set Alan John Dennis

This will reset the positional parameters.

Now $1 is Alan, $2 is John, and $3 is Dennis.

Inside the scripts, we can save positional parameters in a variable as follows:

oldargs=$*

Then, we can set new positional parameters.

Later on, we can bring back our original positional parameters as follows:

set $oldargs