Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

bash: expanding a variable with embedded quotes

Posted on 2014-04-09
2
Medium Priority
?
574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

688 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