Glam Prestige Journal

Bright entertainment trends with youth appeal.

I have simple script. Script can be run with specific arguments. For example:

./my-script.sh --last-name=Smith --first-name=John

There 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 $command

You have one if statement for each variable.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy