I have simple script. Script can be run with specific arguments. For example:
./my-script.sh --last-name=Smith --first-name=JohnThere is called curl and sended POST-request in script:
curl --data-urlencode last_name=$lastName --data-urlencode first_name=$firstName Where firstName and lastName are variables, that parsed from arguments.
Issue, that firstName or lastName (but not both) can be missed. If argument is missed, appropriate parameter do not sended to server.
But I do not know, how I can implement this logic in scripts.
Ofcouse, I can add if to check argument on null, but I can have much arguments in command line and in this case I will have tons of if...
1 Answer
Something like this: suppose
arg1=myname
arg2=Then in order to include only non-empty parameters, code:
command="curl "
if [ "$arg1" != "" ]; then command="$command --data-urlencode arg1=$arg1"
fi
if [ "$arg2" != "" ]; then command="$command --data-urlencode arg2=$arg2"
fi
echo $commandYou have one if statement for each variable.