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

Using the expr command for arithmetic

We can use the expr command for arithmetic operations. The expr command is an external command; the binary of the expr command is stored in the folder called /usr/bin/expr.

Perform an addition operation as follows:

$ expr 40 + 2
42

Perform a subtraction operation as follows:

$ expr 42 - 2
40

Perform a division operation as follows:

$ expr 40 / 10
4

Perform a modulus (getting remainder) operation as follows:

$ expr 42 % 10
2
$ expr 4 * 10
expr: syntax error

With the expr command, we cannot use * for multiplication. We need to use \* for multiplication:

$ expr "4 * 10"
4 * 10
$ expr 4 \* 10
40

We will write a simple script to add two numbers. Write the Shell script called arithmetic_01.sh as follows:

#!/bin/bash
x=5
y=2
z=`expr $x + $y`
echo $z
Test the script as follows:
$ chmod +x arithmetic_01.sh
$ ./arithmetic_01.sh

The output is here:

7

Let's write a script to perform all the basic arithmetic operations. Write the Shell script called arithmetic_02.sh as follows:

#!/bin/bash
var1=30
var2=20
echo `expr $var1 + $var2`  # Arithmetic Addition
echo `expr $var1 - $var2`  # Arithmetic Subtraction
echo `expr $var1 \* $var2` # Arithmetic Multiplication
echo `expr $var1 / $var2`  # Arithmetic Division
echo `expr $var1 % $var2`  # Arithmetic Modular Division
                           # (Remainder)

Let us test the script:

$ chmod +x arithmetic_02.sh
$ ./arithmetic_02.sh

The output is here:

50
10
600
1
10

Using an arithmetic expansion

We can use two different ways for evaluating arithmetic expressions:

$(( expression ))
$[ expression ]

Learn arithmetic operations using the preceding mentioned arithmetic expansion:

$ a=10
$ b=20
$ c=$((a + b))
$ echo $c

During arithmetic operations, we may need to find square or cube of any given number. These operations are called as exponent operations. We can perform exponent operations as follows:

$ a=5
$ b=3
$ expo=$[ $a ** $b ]# This is equivalent to ab
$ echo $expo
125

This is the result of the 53 operations.

Another way of arithmetic expansions is as follows:

$ B=10
$ A=$[B + 10]
$ echo $A
20
$ echo $[ 3 + 4 - 5 ]
2

$ echo $[ 3 + 4 * 5]
23

Arithmetic multiplication has more precedence over addition. Therefore, 4*5 was performed first, and the addition of 3+20 was performed later on:

$ echo $[(3 + 4) * 5]
35

$ echo $(( 3 + 4 ))
7

$ echo $(( 6 / 0 ))
bash: 6/0: division by 0 ( error token is "0")

We will use many of the preceding arithmetic techniques for doing the same addition operation and check the result.

Let's write an interactive script called arithmetic_03.sh as follows:

#!/bin/bash
echo "Enter  first value"
read number_1
echo "Enter  secondvalue"
read number_2
total=`expr $number_1 + $number_2`
echo $total
sum=$(($number_1 + $number_2))
echo "sum is "$sum
echo "Sum is $[ $number_1+$number_2 ]"

Let us test the script:

$ chmod +x arithmetic_03.sh
$ ./arithmetic_03.sh

Output:

Enter  first value
10
Enter  second  value
5
15
Sum is 15
Sum is 15

The preceding Shell script shows that even if we use any of the previous techniques, the result remains the same.

Let's write the shell called script arithmetic_04.sh as follows:

#!/bin/bash
# Interactive Shell Script Demonstrating Arithmetic Operators
echo "Enter First value"
read number_1
echo "Enter Second value"
read number_2
echo $(($number_1 + $number_2))
echo $(($number_1 / $number_2)) # Division of two numbers

Let's test the program as follows:

$ chmod +x arithmetic_04.sh
$ ./arithmetic_04.sh

Output:

Enter First value
10
Enter Second value
5
15
2

We will write one more script with a different technique. Let's write the Shell script arithmetic_05.sh as follows:

#!/bin/bash
# Script is For Demonstrating Arithmetic
var1=10
var2=20
echo $(($var1+$var2))  # Adding Two Values
echo $(($var1-$var2))  # Subtract Two Values
echo $(($var1*$var2))  # Multiply Two Values
echo $(($var1%$var2))  # Remainder

Let's test the program here:

$ chmod +x arithmetic_05.sh
$ ./arithmetic_05.sh

Output:

30
-10
200
10

We will write a script to add five numbers that are passed from a command line. Let's write the Shell script arithmetic_06.sh as follows:

#!/bin/bash
# Write a shell script which will receive 5 numbers from command line # and print their sum.
echo "Sum of Five Numbers is:" $(($1 + $2 + $3 + $4 + $5))

Let's test the program:

$ chmod +x arithmetic_06.sh
$ ./arithmetic_06.sh 10 20 30 40 50

Output:

Sum of Five Numbers is: 150

Let's write the Shell script arithmetic_07.sh as follows for finding cube, quotient, and remainder:

#!/bin/bash

x=99

(( cube = x * x * x ))
(( quotient = x / 5 ))
(( remainder = x % 5 ))

echo "The cube of $x is $cube."
echo "The quotient of $x divided by 5 is $quotient."
echo "The remainder of $x divided by 5 is $remainder."

# Note the use of parenthesis to controlling arithmetic operator
# precedence evaluation.
(( y = 2 * (quotient * 5 + remainder) ))
echo "Two times $x is $y."

Let's test the program:

$ chmod +x arithmetic_07.sh
$ ./arithmetic_07.sh

Output:

The cube of 99 is 970299.
The quotient of 99 divided by 5 is 19.
The remainder of 99 divided by 5 is 4.
Two times 99 is 198.