Command line argument not getting reflected in shell scipt

49 Views Asked by At

I am trying to substitute a variable value from command line for a csh shell script

cat test.sh

#!/bin/csh

set verbose
set echo

/usr/bin/wget  -O created.json  --post-data="{ "version": "4.3", "merchant": { "bin": "000001" }, "order": { "customerProfileOrderOverideInd": "NO", "customerProfileFromOrderInd": "A", "orderDefaultAmount": "1050" }, "paymentInstrument": { "customerAccountType": "CC", "card": { "ccAccountNum": $1, "ccExp": "202406" } }, "profile": { "customerName": "Glgli", "customerAddress1": "EGL", "customerCity": "Aurora", "customerState": "CO", "customerZIP": "80011", "customerEmail": "[email protected]", "customerPhone": "81942636", "customerCountryCode": "US" } }" --header="OrbitalConnectionUsername: XXXXXXX"  --header="OrbitalConnectionPassword: XXXXXXXX" --header="MerchantID: XXXXXX" --header="content-type: application/json" --no-cookies --no-check-certificate "https://some website"

I am executing the shell script as below

./test.sh <some card number>

But it is not reflecting the value and the profile is not getting created. If I hardcode the value of "ccAccountNum" the profile gets created successfully. I even tried with "-method=post" , "--body-data" and double quotes with $1. Both didn't worked. Please let me know what mistake I am doing.

PS:Because of some constraints I cannot use --post-file.

1

There are 1 best solutions below

0
Martin Tournoij On

You can't nest " quotes; with this:

--post-data="{ "version": "4.3",

That first " will start quoting, that second will stop quoting, that this will start quoting, etc. You can test this with echo:

> echo --post-data="{ "version": "4.3" }"
--post-data={ version: 4.3 }

Which isn't what you want.

Instead, quote the literal JSON parts with ', then stop quoting, insert your $1 as $1:q to insert it as literal, and start quoting with ' again.

This sounds confusing, and it is, but that's shell scripting for you.

Abbreviated, this is what it looks like:

wget -O created.json \
    --post-data='{"card":{"ccAccountNum": "'$1:q'", "ccExp": "202406"}}' \
    --no-cookies --no-check-certificate \
    "https://some website"