Skip to content

Instantly share code, notes, and snippets.

@pazdera
Created July 16, 2011 07:56
Show Gist options
  • Select an option

  • Save pazdera/1086117 to your computer and use it in GitHub Desktop.

Select an option

Save pazdera/1086117 to your computer and use it in GitHub Desktop.
Example of using getopt(1) utility in bash
#!/bin/bash
# Example of using getopt(1) utility to parse script arguments
# Copyright (C) 2011 Radek Pazdera
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
ARGS=`getopt -o "123:" -l "one,two,three:" -n "getopt.sh" -- "$@"`
#Bad arguments
if [ $? -ne 0 ];
then
exit 1
fi
eval set -- "$ARGS"
while true;
do
case "$1" in
-1|--one)
echo "Uno"
shift;;
-2|--two)
echo "Dos"
shift;;
-3|--three)
echo "Tres"
# We need to take the argument
if [ -n "$2" ];
then
echo "Argument: $2"
fi
shift 2;;
--)
shift
break;;
esac
done
@nguyenlamlll

Copy link
Copy Markdown

Seems missing shift after esac on line 53.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment