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

Implementing simple menus with select

With the Bash shell, it is possible to create simple menus with the help of the select built-in command.

The syntax of select is as follows:

            PS3=prompting-text
            select VARIABLE in item1 item2 item3
            do
              commands
            done

The advantage of a menu with select is that we can have an endless loop with it. We can have a condition in which we exit the loop.

In the following script select_01.sh, we show the menu with five options such as a, bc, def, ghi, and jkl. The script will execute the command inside do and done:

#!/bin/bash
select var1 in a  bc   def    ghi   jkl
do
echo "Present value of var1 is $var1
done

Let's test the program:

$ chmod +x select_01.sh
$ ./select_01.sh

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

1) a
2) bc
3) def
4) ghi
5) jkl
#? 2
"Present value of var1 is bc
#? 4
"Present value of var1 is ghi
#? 5
"Present value of var1 is jkl
#?
Press     ^C    to quit

We can implement the case command inside the do and done part of the select menu. The syntax will be as follows:

           PS3=prompting text
           select   VARIABLE in  item1  item2 item3
           do
             case VARIABLE in
             value1 ) command1 ; ;
             value2 ) command2 ; ;
           esac
           done

In the following script select_02.sh, we used the case command inside do and done. This gives us many convenient features. Due to select, we get endless such as continuous loop. In case the if option entered is quit, then it exits the continuous loop:

#!/bin/bash
PS3="please  select any one : "
select var in a b quit
do
case $var in
  a) echo option is a ;;
  b) echo option is b ;;
  quit) exit ;;
  *) echo option is default ;;
esac
done

Let's test the program:

$ chmod +x select_02.sh
$ ./select_02.sh

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

1) a
2) b
3) quit
please  select any one : 1
option is a
please  select any one : 2
option is b
please  select any one : 3

In the following script select_03.sh, we used a case with numerical options 1, 2, 3, 4, and an option for an invalid choice:

#!/bin/bash
PS3="Please enter one of the option"
select var in 1 2 3 4
do
case $var in
    1) echo "One is selected";;
    2) echo "Two is selected;;
    3) echo "Two is selected;;
    4) echo "Two is selected;;
    *) echo "not a proper option";;
esac
done

Let's test the program:

$ chmod +x select_03.sh
$ ./select_03.sh

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

1) 1
2) 2
3) 3
4) 4
Please enter one of the option : 1
"One is selected"
Please enter one of the option : 2
"Two is selected
Please enter one of the option : 3
"Two is selected
Please enter one of the option : 4
"Two is selected
Please enter one of the option : 8
"not a proper option"
Please enter one of the option :

In the case statement, we can put many choices to select the same command. Here is an example of the script select_04.sh as follows:

#!/bin/bash
PS3="Please select one of the above:"
select COMPONENT in comp1 comp2 comp3 all none
do
case $COMPONENT in
comp1|comp2|comp3) echo "comp1 or comp2 or comp3 selected" ;;
all) echo "selected all"
;;
none) break ;;
*) echo "ERROR: Invalid selection, $REPLY." ;;
esac
done

Let's test the program:

$ chmod +x select_04.sh
$ ./select_04.sh

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

1) comp1
2) comp2
3) comp3
4) all
5) none
Please select one of the above:

The script select_05.sh is used to inform the user about the calorie information in fruits as follows:

#!/bin/bash
PS3="Enter the number for your fruit choice: "

select fruit in apple orange banana peach pear "Quit Menu"
do
  case $fruit in
    apple)
      echo "An apple has 80 calories."
      ;;

    orange)
      echo "An orange has 65 calories."
      ;;

    banana)
      echo "A banana has 100 calories."
      ;;

    peach)
      echo "A peach has 38 calories."
      ;;

    pear)
      echo "A pear has 100 calories."
      ;;

    "Quit Menu")
      break
      ;;

    *)
      echo "You did not enter a correct choice."
      ;;
  esac
done

Let's test the program:

$ chmod +x select_05.sh
$ ./select_05.sh

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

1) apple      3) banana      5) pear
2) orange     4) peach      6) Quit Menu
Enter the number for your fruit choice: 1
An apple has 80 calories.
Enter the number for your fruit choice: 2
An orange has 65 calories.
Enter the number for your fruit choice: 3
A banana has 100 calories.
Enter the number for your fruit choice: 4
A peach has 38 calories.
Enter the number for your fruit choice: 5
A pear has 100 calories.
Enter the number for your fruit choice: 6