Skip to content

Instantly share code, notes, and snippets.

@lobre
Created July 5, 2019 12:16
Show Gist options
  • Save lobre/6619f61d5d0e23ff4ae0473300dfc954 to your computer and use it in GitHub Desktop.
Save lobre/6619f61d5d0e23ff4ae0473300dfc954 to your computer and use it in GitHub Desktop.
Arguments parsing in bash example
#!/bin/bash
#
# This script will extract, modify and repack a bootable iso image.
#
# Features:
# - Add kickstart file
# - Add post install kickstart scripts
# - Play scripts in a chroot of the filesystem
#
# Dependencies:
# - mkisofs
# Initialize our own variables:
output="./output.iso"
workdir="~/.cache/isobuilder"
kickstart=""
postscripts=""
scripts=""
function usage {
echo "Usage: $(basename $0) [OPTION]... isofile.iso"
echo " -o output iso file path (default: $output)"
echo " -w working directory used internally "
echo " and cleaned when terminated (default: $workdir)"
echo " -k kickstart file path"
echo " -p post install script to add (can be used multiple times)"
echo " -s script to play in chroot (can be used multiple times)"
echo " -h display help"
}
# The variable OPTIND holds the number of options parsed by the last call to getopts
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "o:w:k:p:s:h" opt; do
case "$opt" in
h|\?)
usage
exit 0
;;
:)
echo "Invalid option: $OPTARG requires an argument"
exit 0
;;
o) output=$OPTARG
;;
w) workdir=$OPTARG
;;
k) kickstart=$OPTARG
;;
p) postscripts+=("$OPTARG")
;;
s) scripts+=("$OPTARG")
;;
esac
done
shift $((OPTIND-1))
# Ignore -- if existing
[ "${1:-}" = "--" ] && shift
# Check positional mandatory arguments
iso=$1
if [ -z "$iso" ]
then
usage
exit 0
fi
echo "iso: $iso"
echo "output: $output"
echo "workdir: $workdir"
echo "kickstart: $kickstart"
# List scripts
for val in "${scripts[@]}"; do
echo "script: $val"
done
# List post scripts
for val in "${postscripts[@]}"; do
echo "postscript: $val"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment