Advertisement
textandmetal

Bash script with flags example

May 16th, 2020
2,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.18 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. ############################################
  4. # Define variables
  5. # Script Information
  6. script="Hello World Example"
  7. time="$(date)"                                                                                # The current time and date
  8. TITLE="Hello World Example"                                                                   # This is the title of your
  9. AUTHOR="textandmetal"                                                                         # Who wrote this script
  10. TAGLINE="A super simple bash tool"                                                            # Tagline for your script in man page
  11. manpagename=$(printf "$TITLE" | tr '[:lower:]' '[:upper:]')                                   # This transforms the $title variable to uppercase for man page
  12.  
  13. man_page(){
  14. # This is the help page that is triggered by the -h flag
  15. # Give the user important information and include common
  16. # examples of usage
  17.  
  18. printf "
  19. $manpagename(1)                      User Commands                      MANPAGE(1)
  20.  
  21. NAME
  22.       $TITLE - $TAGLINE
  23.  
  24. SYNOPSIS
  25.       $TITLE [OPTION]...
  26. DESCRIPTION
  27.       A simple bash script to demonstrate flags and some basic
  28.       options for it.
  29.      
  30.       -h
  31.              Show this man page
  32.  
  33.       -l
  34.              View the licence for this script
  35.  
  36.       -d
  37.              Debug your script, the script will start but has
  38.              the -set x option enabled
  39. "
  40. }
  41.  
  42. view_licence(){
  43. # The licence for this script, it takes the $AUTHOR and $TILE variable
  44. printf "
  45. $TITLE
  46.  
  47. MIT License
  48.  
  49. Copyright (c) 2019 $AUTHOR
  50.  
  51. Permission is hereby granted, free of charge, to any person obtaining a copy
  52. of this software and associated documentation files (the "'"Software"'"), to deal
  53. in the Software without restriction, including without limitation the rights
  54. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  55. copies of the Software, and to permit persons to whom the Software is
  56. furnished to do so, subject to the following conditions:
  57.  
  58. The above copyright notice and this permission notice shall be included in all
  59. copies or substantial portions of the Software.
  60.  
  61. THE SOFTWARE IS PROVIDED "'"AS IS"'", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  62. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  63. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  64. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  65. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  66. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  67. SOFTWARE.
  68. "
  69. }
  70. txtred=$(tput setaf 1)
  71. txtgrn=$(tput setaf 2)
  72. txtylw=$(tput setaf 3)
  73. txtblu=$(tput setaf 4)
  74. txtpur=$(tput setaf 5)
  75. txtcyn=$(tput setaf 6)
  76. bold=$(tput bold)
  77. txtrst=$(tput sgr0)
  78.  
  79. ##############################################################################
  80. ############################### debug_program ################################
  81. ##############################################################################
  82. # This launches the script in a simple debugging mode
  83. debug_program(){
  84. set -x
  85. start_program
  86. set +x
  87. }
  88. ##############################################################################
  89. ##############################################################################
  90.  
  91. ##############################################################################
  92. ############################### start_program ################################
  93. ##############################################################################
  94. # This is the main part of the script that launches on execution without flags
  95. start_program(){
  96. echo "Hello World"
  97. }
  98. ##############################################################################
  99. ##############################################################################
  100.  
  101. # getopts for options
  102. while getopts dhl  option
  103. do
  104. case "${option}"
  105. in
  106. l) VIEWLICENCE="view_licence";;
  107. h) MANPAGE="man_page";;
  108. d) DEBUG="debug_program" ;;
  109. esac
  110. done
  111.  
  112. # Main Logic
  113. while true
  114. do
  115. #clear
  116. if [ $OPTIND -eq 1 ]; then start_program; fi
  117. shift $((OPTIND-1))
  118. #printf "$# non-option arguments"
  119. $MANPAGE
  120. $VIEWLICENCE
  121. $DEBUG
  122. exit
  123. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement