Difference between revisions of "Bash script template"
From Teknologisk videncenter
m (Created page with "Below is a script template with options. <source lang=bash> #!/bin/bash # # FILNAME - SHORT DESCRIPTION # # (c) 2012 - DONALD DUCK <dd@disney.org> - all rights reserved # # THIS ...") |
m |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | |||
+ | <source lang=bash> | ||
+ | #!/usr/bin/env bash | ||
+ | set -o errexit # Exit on error | ||
+ | set -o nounset # Exit if variable is unset | ||
+ | set -o pipefail # Exit if one side of a pipe fails | ||
+ | |||
+ | firstargument="${1:-somedefaultvalue}" | ||
+ | |||
+ | echo "$firstargument" | ||
+ | </source> | ||
Below is a script template with options. | Below is a script template with options. | ||
+ | |||
<source lang=bash> | <source lang=bash> | ||
#!/bin/bash | #!/bin/bash | ||
Line 24: | Line 36: | ||
N=1 # Default set | N=1 # Default set | ||
UNIT="" | UNIT="" | ||
+ | TEXT="Distance between A and B" | ||
usage() { | usage() { | ||
Line 31: | Line 44: | ||
echo " -r r flag" | echo " -r r flag" | ||
echo " -c c flag with value" | echo " -c c flag with value" | ||
− | echo " -n | + | echo " -n n flag - unset" |
echo " -t t flag with text" | echo " -t t flag with text" | ||
echo " -h help" | echo " -h help" | ||
echo "" | echo "" | ||
echo " Description line1" | echo " Description line1" | ||
− | echo " Description | + | echo " Description line2" |
exit 1 | exit 1 | ||
} | } | ||
Line 42: | Line 55: | ||
# collect options and override default | # collect options and override default | ||
− | while getopts 'c: | + | while getopts ':c:t:mkrnh' CMDLINEARG |
do | do | ||
case "${CMDLINEARG}" in | case "${CMDLINEARG}" in | ||
c) | c) | ||
COUNT=${OPTARG} | COUNT=${OPTARG} | ||
+ | ;; | ||
+ | t) | ||
+ | TEXT=${OPTARG} | ||
;; | ;; | ||
m|k) | m|k) | ||
Line 59: | Line 75: | ||
N=${OPTARG} | N=${OPTARG} | ||
;; | ;; | ||
− | h | + | h) |
usage | usage | ||
;; | ;; | ||
+ | *) echo -e "ERROR: illegal option -${OPTARG}" | ||
+ | ;; | ||
esac | esac | ||
done | done | ||
echo -e "Settings" | echo -e "Settings" | ||
echo -e "COUNT = $COUNT" | echo -e "COUNT = $COUNT" | ||
+ | echo -e "TEXT = $TEXT" | ||
echo -e "UNIT = $UNIT" | echo -e "UNIT = $UNIT" | ||
echo -e "R = $R" | echo -e "R = $R" | ||
echo -e "N = $N" | echo -e "N = $N" | ||
echo -e "Done..." | echo -e "Done..." | ||
+ | </source> | ||
+ | Running the script - example | ||
+ | <source lang=bash> | ||
+ | heth@mars:~/bin$ ./bash_template -c 78 -t "Distance between sun and mars" -r -m | ||
+ | Settings | ||
+ | COUNT = 78 | ||
+ | TEXT = Distance between sun and mars | ||
+ | UNIT = m | ||
+ | R = r | ||
+ | N = 1 | ||
+ | Done... | ||
</source> | </source> | ||
+ | =Links= | ||
+ | *[http://wiki.bash-hackers.org/howto/getopts_tutorial getopts tutorial] | ||
[[Category:bash]][[category:Linux]] | [[Category:bash]][[category:Linux]] |
Latest revision as of 09:22, 23 February 2024
#!/usr/bin/env bash
set -o errexit # Exit on error
set -o nounset # Exit if variable is unset
set -o pipefail # Exit if one side of a pipe fails
firstargument="${1:-somedefaultvalue}"
echo "$firstargument"
Below is a script template with options.
#!/bin/bash
#
# FILNAME - SHORT DESCRIPTION
#
# (c) 2012 - DONALD DUCK <dd@disney.org> - all rights reserved
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# default settings
COUNT=0
unset R # Default unset
N=1 # Default set
UNIT=""
TEXT="Distance between A and B"
usage() {
echo "usage: $0 [-mkrh] [-c count -n] [-t text]"
echo " -m m flag fx. report result in miles"
echo " -k k flag fx. report result in km"
echo " -r r flag"
echo " -c c flag with value"
echo " -n n flag - unset"
echo " -t t flag with text"
echo " -h help"
echo ""
echo " Description line1"
echo " Description line2"
exit 1
}
# collect options and override default
while getopts ':c:t:mkrnh' CMDLINEARG
do
case "${CMDLINEARG}" in
c)
COUNT=${OPTARG}
;;
t)
TEXT=${OPTARG}
;;
m|k)
[ ! -z "${UNIT}" ] && echo '-m and -k are mutually exclusive. Select miles or km.'\
&& usage
UNIT="${CMDLINEARG}"
;;
r)
R=${CMDLINEARG}
;;
n)
N=${OPTARG}
;;
h)
usage
;;
*) echo -e "ERROR: illegal option -${OPTARG}"
;;
esac
done
echo -e "Settings"
echo -e "COUNT = $COUNT"
echo -e "TEXT = $TEXT"
echo -e "UNIT = $UNIT"
echo -e "R = $R"
echo -e "N = $N"
echo -e "Done..."
Running the script - example
heth@mars:~/bin$ ./bash_template -c 78 -t "Distance between sun and mars" -r -m
Settings
COUNT = 78
TEXT = Distance between sun and mars
UNIT = m
R = r
N = 1
Done...