Created
July 16, 2011 07:56
-
-
Save pazdera/1086117 to your computer and use it in GitHub Desktop.
Example of using getopt(1) utility in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems missing
shift
afteresac
on line 53.