linux_wiki:template_bash_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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:
 ---- ----
  
-====== Parent Script ======+====== 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: Script description here 
 +# Last Modified: 2016-06-21 
 +# Recent Changes: -Change 1 
 +#                 -Change 2 
 + 
 +##### Customize These Variables ##### 
 +# Customization 
 +var1="myvar" 
 + 
 +##### End of Customize Variables ##### 
 + 
 +#===================================== 
 +# Functions; Main starts after 
 +#===================================== 
 +function show_usage 
 +
 +  echo -e "\n==== Script Name Usage ====" 
 +  echo -e "\nDescripton: Script description HERE.." 
 +  echo -e "\n--Usage--" 
 +  echo -e "-h                    => Display usage." 
 +  echo -e "-d argument here      => Another function." 
 +  echo -e "\n--Other Requirements--" 
 +  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 "hd:" opt; do 
 +  case "${opt}" in 
 +    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 ">> ERROR! A directory must be provided as an argument." 
 +  show_usage 
 +  exit 1 
 +fi 
 + 
 +#=================== 
 +# Main starts here 
 +#=================== 
 + 
 +# Do some stuff 
 +echo -e ">> Doing some stuff at: ${dir}" 
 +echo "Hello" >> ${dir}/myfile.txt 
 + 
 +echo -e "\n============================" 
 +echo -e "=- Script Name Completed. -=" 
 +echo -e "============================" 
 +</code> 
 + 
 +---- 
 + 
 +====== 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 ##
-/usr/bin/time ${base_path}${worker} 2>&1 | tee -a ${log_file}+${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.
  • linux_wiki/template_bash_script.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)