Solved

bash: expanding a variable with embedded quotes

Posted on 2014-04-09
2
558 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now