Solved

bash: expanding a variable with embedded quotes

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
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.:
Suggested Courses

635 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