Differences
This shows you the differences between two versions of the page.
linux_wiki:template_bash_script [2016/06/23 22:36] billdozor |
linux_wiki:template_bash_script [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Template: Bash Script ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | Templates for creating two types of bash scripts: | ||
- | - A single script with arguments | ||
- | - A bash script that provides logging and launches a child script. | ||
- | |||
- | **Checklist** | ||
- | * Distro(s): Any | ||
- | |||
- | ---- | ||
- | |||
- | ====== Single Script with Arguments ====== | ||
- | |||
- | This script has arguments and a usage/help function ready to go. | ||
- | |||
- | <code bash> | ||
- | #!/bin/bash | ||
- | # Name: script-name-here.sh | ||
- | # Description: | ||
- | # Last Modified: 2016-06-21 | ||
- | # Recent Changes: -Change 1 | ||
- | # | ||
- | |||
- | ##### Customize These Variables ##### | ||
- | # Customization | ||
- | var1=" | ||
- | |||
- | ##### End of Customize Variables ##### | ||
- | |||
- | # | ||
- | # Functions; Main starts after | ||
- | # | ||
- | function show_usage | ||
- | { | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e "-d argument here => Another function." | ||
- | echo -e " | ||
- | echo -e "-> Run as either root or regular user with sudo privileges." | ||
- | echo -e | ||
- | } | ||
- | |||
- | # | ||
- | # Get Script Arguments | ||
- | # | ||
- | # Reset POSIX variable in case it has been used previously in this shell | ||
- | OPTIND=1 | ||
- | |||
- | while getopts " | ||
- | case " | ||
- | h) # -h (help) argument | ||
- | show_usage | ||
- | exit 0 | ||
- | ;; | ||
- | d) # -d argument-description | ||
- | dir=${OPTARG} | ||
- | ;; | ||
- | *) # invalid argument | ||
- | show_usage | ||
- | exit 0 | ||
- | ;; | ||
- | esac | ||
- | done | ||
- | |||
- | # | ||
- | # Pre-checks: Make sure we have good options set | ||
- | # | ||
- | |||
- | # Ensure argument is provided | ||
- | if [[ -z ${dir} ]]; then | ||
- | echo -e ">> | ||
- | show_usage | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # | ||
- | # Main starts here | ||
- | # | ||
- | |||
- | # Do some stuff | ||
- | echo -e ">> | ||
- | echo " | ||
- | |||
- | echo -e " | ||
- | echo -e "=- Script Name Completed. -=" | ||
- | echo -e " | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== Script with Logging and Child ====== | ||
- | |||
- | This type of script has a parent script that provides the logging and control. It launches a child or worker script. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Parent Script ===== | ||
- | |||
- | This is the script to execute that will provide logging and launch the child script. | ||
- | |||
- | <code bash script_skel.sh> | ||
- | ############################################################################################### | ||
- | #!/bin/bash | ||
- | # Name: script_skel.sh | ||
- | # Description: | ||
- | ############################################################################################### | ||
- | |||
- | ##### Customize These Variables ##### | ||
- | # Log file | ||
- | log_file=" | ||
- | |||
- | # Name of worker script to execute | ||
- | worker=" | ||
- | #### End Customize Variables #### | ||
- | |||
- | # Store this script' | ||
- | script_name=" | ||
- | base_path=$(echo ${0} | sed " | ||
- | |||
- | # Clear log and timestamp the beginning | ||
- | echo -e ">> | ||
- | cat /dev/null > ${log_file} | ||
- | echo -e "---- Log Started: $(date) ----\n" | ||
- | |||
- | ## Execute Script ## | ||
- | / | ||
- | |||
- | # Close log file | ||
- | echo -e " | ||
- | |||
- | # Reminder of log file location | ||
- | echo -e " | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Child Script ===== | ||
- | |||
- | This script does all the work and is launched via its parent script. | ||
- | |||
- | <code bash script_worker_skel.sh> | ||
- | ############################################################################################### | ||
- | #!/bin/bash | ||
- | # Name: script_worker_skel.sh | ||
- | # Description: | ||
- | ############################################################################################### | ||
- | |||
- | ##### Customize These Variables ##### | ||
- | # | ||
- | ##### End of Customize Variables ##### | ||
- | |||
- | #### Functions Here: Main Starts After #### | ||
- | # function name{ | ||
- | # | ||
- | #} | ||
- | #### End of Functions #### | ||
- | |||
- | # | ||
- | # Main Starts Here | ||
- | # | ||
- | |||
- | # Pre-checks | ||
- | |||
- | # | ||
- | # Confirm running the script | ||
- | # | ||
- | echo -e " | ||
- | echo -e "#### | ||
- | echo -e " | ||
- | echo | ||
- | echo -e " | ||
- | echo -e " | ||
- | read run_script | ||
- | |||
- | if [[ ${run_script} != " | ||
- | echo -e " | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # Do some stuff | ||
- | |||
- | echo -e " | ||
- | echo -e "#### | ||
- | echo -e " | ||
- | </ | ||
- | |||
- | ---- | ||