Passing parameter with single quote to shell script

Hello All,

I am trying below

+++++++++

#/bin/bash
set -x
Host=$1
Port=$2
User=$3
Pass=$4
Warning=$5
Critical=$6
SCRIPT_LOC=/opt/agent/
Parameters="'""hostname=$Host&""port=$Port&""username=$User&""password=$Pass&""jvm=UsedMemory,$Warning,$Critical""'"
echo $Parameters
$SCRIPT_LOC/agent.sh "$Parameters"

+++++++++++++++++

the agent.sh expects the arguments to be within single quotes as below

agent.sh 'hostname=xxx&port=xxx&username=xxx&password=xxx&jvm=UsedMemory,xx,xx'

in my above script echo $parameters does give the output as

'hostname=xxx&port=xxx&username=xxx&password=xxx&jvm=UsedMemory,xx,xx'

but $SCRIPT_LOC/agent.sh "$Parameters" gets executed as

agent.sh ''\''hostname=xxx&port=xxx&username=xxx&password=xxx&jvm=UsedMemory,xx,xx'\'''

and it fails.

Any pointers to fix this would help me.

Thanks
Sundari

Please use code tags as it is difficult for us to read.
How do you want the parameters to be passed?

Hi

#/bin/bash
set -x
Host=$1
Port=$2
User=$3
Pass=$4
Warning=$5
Critical=$6
SCRIPT_LOC=/opt/agent/
Parameters="'""hostname=$Host&""port=$Port&""username=$User&""password=$Pass&""jvm=UsedMemory,$Warni  ng,$Critical""'"
echo $Parameters
$SCRIPT_LOC/agent.sh "$Parameters"

the agent.sh expects the arguments to be within single quotes as below

agent.sh 'hostname=xxx&port=xxx&username=xxx&password=xxx&jvm=UsedMemory,xx,xx'
agent.sh "'hostname=${Host}&port=${Port}&username=${User}&password=${Pass}&jvm=UsedMemory,${Warning},${Critical}'"

Hi Srini

i updated my script(a.sh) as below

Host=$1
Port=$2
User=$3
Pass=$4
Warning=$5
Critical=$6
SCRIPT_LOC=/opt/agent/
$SCRIPT_LOC/agent.sh "'hostname=${Host}&port=${Port}&username=${User}&password=${Pass}&jvm=UsedMemory,${Warning},${Critical}'"

Ran a.sh as

./a.sh abc.xyz.com 7003 usera passwd 90 95

and got below error

/opt/agent/agent.sh ''\''hostname=abc.xyz.com&port=7003&username=usera&password=passwd&jvm=UsedMemory,90,95'\'''
java.io.IOException: Couldn't connect to the specified host : null|

Where as when i directly call agent.sh as

./agent.sh 'hostname=abc.xyz.com&port=7003&username=usera&password=passwd&jvm=UsedMemory,90,95'

it gives me the expected output.

try this

agent.sh "hostname=${Host}&port=${Port}&username=${User}&password=${Pass}&jvm=UsedMemory,${Warning},${Critical}"

Hi Srini

The agent.sh has a requirement that the parameters passed should be within single quotes.

the error you got shows that it is adding single quotes again around the arguments

cool thanks that worked :slight_smile:

which one worked for you?

this worked

agent.sh "hostname=${Host}&port=${Port}&username=${User}&password=${Pass}&jvm=UsedMemory,${Warning},${Critical}"