Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux_wiki:template_bash_script [2016/06/23 22:33] billdozor [Template: Bash Script] |
linux_wiki:template_bash_script [2016/06/23 22:38] billdozor [Parent Script] |
||
---|---|---|---|
Line 12: | Line 12: | ||
---- | ---- | ||
- | ====== | + | ====== |
+ | |||
+ | This script has arguments and a usage/help function ready to go. | ||
+ | |||
+ | <code 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. | This is the script to execute that will provide logging and launch the child script. | ||
Line 41: | Line 128: | ||
## Execute Script ## | ## Execute Script ## | ||
- | / | + | ${base_path}${worker} 2>&1 | tee -a ${log_file} |
# Close log file | # Close log file | ||
Line 52: | Line 139: | ||
---- | ---- | ||
- | ====== Child Script | + | ===== Child Script ===== |
This script does all the work and is launched via its parent script. | This script does all the work and is launched via its parent script. |