Snippets
Created by
Víctor Goñi Sanz
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #!/bin/bash
# Check spell http://www.shellcheck.net/# online, or download, to test!
############################################################ Function declaration
### OPTIONS
# This function, display usage info
f_display_usage()
{
echo -e "\n--------------------------------------------------"
echo "| This function teach you how this script works"
echo -e "| Usage:\n| $0" # -e option allow use \n
echo "| ${optionHelp} = Print usage"
echo "| ${optionSay} = Print echo with message"
echo "| ${optionPrintOptions} = Print all options"
echo "| ${optionAuthor} = Print author's name"
echo -e "--------------------------------------------------"
}
f_say()
{
echo -e "\n--------------------------------------------------"
echo " I want to say: ${1}"
echo -e "--------------------------------------------------"
}
f_author()
{
echo -e "\n--------------------------------------------------"
echo " The author is: vgoni"
echo -e "--------------------------------------------------"
}
f_options()
{
echo -e "\n--------------------------------------------------"
echo " Printing arguments..."
echo " The number of arguments provided is: ${Nargs}"
for ((i=0; i < ${Nargs}; i++))
{
echo " The argument $((i+1)): ${args[$i]}"
}
echo " No more arguments!"
echo -e "--------------------------------------------------"
}
### Other functions
# Check if are provided correct arguments
f_evaluate_options()
{
echo " Evaluation options, number of arguments: ${Nargs}"
eval set -- ${options}
echo " Options: ${options}"
}
############################################################ End Function declaration
# Main program
clear # clear terminal
# First of all, store arguments in a variable.
# Bash have variable when call an executable, $0 = CommandName, $1..$N = Arguments
# We store arguments in an array. If a function is called, you cannot access to them if not are global variables
# because they are overwrited by function arguments.
args=(${@}) # Arguments
Nargs=(${#}) # Number of arguments
# We define options, using getopt
# -o Each single character stands for an option
# -l Long name for each option
# : after an option (short or long) tells that option is required argument
# :: tells is a optional argument
# For extension take a look here: http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
options=$(getopt -o hs:ao -l help,say:,author,options -- "${@}")
if [[ $? -ne 0 ]]
then
echo " Error: Detected invalid option"
fi
# -o hs:ao = h & a option, no arguments; s option with a required option;
# -l help,say:,author = same.
# Declarate variables with options to clarify
optionHelp="-h|--help"
optionSay="-s|--say"
optionAuthor="-a|--author"
optionPrintOptions="-o|--options"
# We define some global variables
commandName=$(basename "$0") # Extract name from first variable
commandPath=$(pwd) # Get the path with command pwd
echo "------- My second script begins execution..."
echo "--------------------------------------------"
echo " Executing command: ${commandName}"
echo " At path: ${commandPath}"
# Call a function to evaluate arguments as options
f_evaluate_options
# Check all arguments
# We are going to check the all the options shifting them.
# Using shift, the positional parameters are shifted to the left by this number, N.
# Using case syntaxis, more info: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html
while [ "$1" != "" ]
do
echo "Processing option: ${1}"
case "$1" in
-h|--help) f_display_usage ;; # Call function
-s|--say) f_say $2 ; shift ;; # Call function with argument, and discard argument
-a|--author) f_author ;; # Call a function to print author
-o|--options) f_options ;; # Call a function to print options
*) echo "Unknown option: $1" ; f_display_usage ; break ;;
esac
shift; # Advance
done;
echo "------- My second script end now!"
echo "---------------------------------"
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.