Шаблон bash скрипта #!/usr/bin/env bash # <script_name>: <description> # Author: <author> # Current maintainer (?) # Copyright/ License (?) # Project/Repository (?) # Warnings/Defects (?) # Argument support: no/yes (use -h/--help for usage info) # Other information #______________________________________________________________________________ ############################################################################### # Help text # usage: "# [?] For an explanation, see line <line number>" #------------------------------------------------------------------------------ # About using bash idioms # The bash idiom is used instead of the `if` operator, # because `if` operator for the case with 1-2 commands # in the then/else branch is too large and unwieldy. ############################################################################### # Error Handling #------------------------------------------------------------------------------ # A handler function for terminating signals and errors, # which should clean up after itself # Usage: error_handler # Global vars: no # Input parameters: no # Output values: no function error_handler { local exit_code="$?" echo "Error: exit code: $?" >&2 # some rm shit exit "$exit_code" } # End of error_handler function # Unofficial strict mode # -e -- Ends the script on the first error # -E -- Allows the trap command to work # -u -- When trying to access undefined var the script terminates # -o pipefail -- Terminates the script if any command in the pipeline fails set -eEuo pipefail # A signal trap that calls the error_handler function # ABRT - abort interrupt signal # HUP - terminal restart signal # INT - interrupt signal (Ctrl+C) # QUIT - exit signal (Ctrl+C) # TERM - termination signal (allows the process to terminate gracefully) # ERR - error signal trap error_handler ABRT HUP INT QUIT TERM ERR # [?] For an explanation, see line 13 # Checking for superuser privileges ? (( EUID != 0 )) && { echo "The script needs superuser rights." >&2; exit 1; } PROGRAM="${0##*/}" # bash version of the `basename` command ############################################################################### # Global variables/constants #------------------------------------------------------------------------------ EXAMPLE_GLOBAL_VAR='/path/2/shit' ############################################################################## # Functions #----------------------------------------------------------------------------- # <Function usage> # Global vars: no # Input parameters: no # Output values: no function Foo { : } # End of Foo function #----------------------------------------------------------------------------- # <Othuh function usage> # Global vars: no # Input parameters: no # Output values: no function Bar { : } # End of Bar function ############################################################################### # Main code #------------------------------------------------------------------------------ Соус: Книга “Идиомы Bash” → Глава 11. “Разработка своего руководства по стилю” → “Шаблон сценария” bash