Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created August 4, 2011 18:53
Show Gist options
  • Save rafaelrinaldi/1125931 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/1125931 to your computer and use it in GitHub Desktop.
Easy (maybe dumb also?) way to get short and long parameters of a Shell script.
# author: Rafael Rinaldi (rafaelrinaldi.com)
# since: Aug 3, 2011
# license: WTFPL
# A string with available options
options=$@
# Options converted to an array
arguments=($options)
# Loop index
index=0
# Variables I have
f=""
c="FFFFFF"
t=""
w="100%"
h="100%"
v="10.0.0"
sov="2.2"
for argument in $options
do
# Incrementing loop index
index=`expr $index + 1`
# Getting the parameters
case $argument in
-f|-file) f=${arguments[index]} ;;
-c|-color) c=${arguments[index]} ;;
-t|-title) t=${arguments[index]} ;;
-w|-width) w=${arguments[index]} ;;
-h|-height) h=${arguments[index]} ;;
-v|-version) v=${arguments[index]} ;;
-sov) sov=${arguments[index]} ;;
--help) echo "Script usage" ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment