I have a boubt passing different arguments at a time for any one option in below code.
I would also like to check which option has been selected (any one of i, r, u ) so that whether or not matching argument passed can be verified.
for i and r - install and re-install - I want to pass directory path
and for u -upgrade - I want to pass list of patches to be upgraded.
Is there a way I can catch i, r, u option value in a variable and match them with the argument pass for that option.
while getopts :iru: OPTION
do
case "$OPTION" in
i) install="$path install_function" ;;
r) reinstall="$path Reinstall_Function" ;;
u) patch_upgrade=$OPTARG upgrade_function ;;
:) echo "$0: $OPTARG option missing argument!"
exit 2 ;;
?) echo "$0: $OPTARG is an invalid option!"
echo "$0 -ab -c optarg patch_list"
exit 2 ;;
esac
done
shift $OPTIND-1
Your help is highly appreciated to get list of all parameters ($@) passing as an argument to option U in below code - patch_upgrade. Here I am passing list of patches as an argument ( patch count may vary ) for option -u but not sure, what I am missing , I am not able to pass all argument $OPTARG accepts first one only. Where as I wanted a complete list to get l on OPTARG. Please help me correcting below case statement.
install=0
reinstall=""
patch_upgrade=""
# if install and re-install dont need argument but upgrade need
while getopts :iru: OPTION
do
case "$OPTION" in
i ) install="yes" ;;
r ) reinstall="on" ;;
u ) patch_upgrade=$OPTARG ;; # patch_list
: ) echo "$0: $OPTARG option missing argument!"
exit 2 ;;
? ) echo "$0: $OPTARG is an invalid option!"
echo "$0 -ir -u optarg patch_list"
exit 2 ;;
esac
done
shift $((OPTIND-1)); OPTIND=1
if [ $install = "yes" ] ; then
echo " call install function"
fi
if [ $reinstall = "on" ] ; then
echo " call Re-install function"
fi
if [ $patch_upgrade = $OPTARG ] ; then
echo " call patch_Upgrade function "
echo " list of patches is $OPTARG"
echo " array of patch list is $@"
fi
Seems like every post you have made here has required a moderator to come along and update after you to add Code Tags, they are a lot more patient than me.
Have you considered using a quoted string for list of patches eg:
DOH!! How I can be so stupid not tried "" or '' ..both are working . but Isn't there a way without puting " " or '' we can treat all passing argument as a string that optarg shud hold.
Any way thank you Chubler , for your quick respond and pointing my mistake. $$ to you .