Solved

bash: expanding a variable with embedded quotes

Posted on 2014-04-09
2
560 Views
Last Modified: 2014-04-09
Hi,

I've been wrestling with this for too long, and need help.

I have the following bash shell function to issue a cURL command:

mycurlfunc () {
	CMD=GET
	HDRS=
	BODY=
	if [ "$1" = PUT ]; then
		CMD=PUT 
		HDRS='-H '"'"'Content-Type: application/json'"'"
		BODY='-d '"'"$2"'"
	fi
	curl --compressed -k -v -X $CMD $HDRS $BODY [<url>]
}

Open in new window

Here's what I want to happen.  I want to invoke mycurlfunc with (something like) the following:

mycurlfunc PUT '{"token": "foo"}'

Open in new window

such that $1 = 'PUT' and $2 = '{"token": "foo"}'.  I want the HDRS and BODY variables to be expanded/inserted into the curl command so it is issued as follows:

curl --compressed -k -v -X PUT -H 'Content-Type: application/json' -d '{"token": "foo"}' [<url>]

Open in new window

When I create HDRS and BODY at the command line they look like the strings I want:

$ set -vx
$ HDRS='-H '"'"'Content-Type: application/json'"'"
$ echo $HDRS
echo $HDRS
+ echo -H ''\''Content-Type:' 'application/json'\'''
-H 'Content-Type: application/json'
$ BODY='-d '"'"'{"token": "foo"}'"'"
$ echo $BODY
echo $BODY
+ echo -d ''\''{"token":' '"foo"}'\'''
-d '{"token": "foo"}'

Open in new window

However, when I put them into the curl command, the literal variable values, not "evaluated" values, appear; i.e., the single quotes themselves are being escaped and the string tokens in the variables are being quoted:

curl --compressed -k -v -X PUT -H ''\''Content-Type:' 'application/json'\''' -d ''\''{"token":' '"foo"}'\''' [<url>]

Open in new window

My question is simple: How can I set HDRS and BODY up to expand the way I want?

Max points since I'm trying to get this done quickly - I think I'm reasonably close, but just can't find the right combination.  Thanks for any help!
0
Comment
Question by:TruthHunter
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39989330
Try in line 10:

eval curl --compressed -k -v -X $CMD $HDRS $BODY [<url>]
0
 

Author Comment

by:TruthHunter
ID: 39989441
Aha!  That was it!  I'm usually so "eval-averse" that it hadn't crossed my mind to try that.  Once you mentioned it, it was obvious.

So thanks very much, and enjoy the points!  :^)
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
NTP Server in VMware 5 225
Oracle Finace 3 77
How to run git alias with shell other than sh 17 103
Powershell Script need assistance to make some changes 4 81
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question