LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2008, 09:53 PM   #1
serconsult
LQ Newbie
 
Registered: Jul 2008
Posts: 12

Rep: Reputation: 0
Lightbulb passing a string to system() with embedded quotes


I am trying to construct a string in C to pass to system() which needs the parameters quoted.

At execution the command needs to look like this:

$sudo -u server /u/server/exe/do_agent "active_camp" "myuserid"

I created a series of strcpy() commands to assemble the string since "active_camp" and "myuserid" are variables

wcamp = "active_camp"
agentid = "myuserid"

{
strcpy(runstr, "sudo -u server /u/server/exe/do_agent '");
strcat(runstr, wcamp);
strcat(runstr, "' '");
strcat(runstr, agentid);
}

system(runstr);

I get $sudo -u server /u/server/exe/do_agent active_camp myuserid

I need some ideas here
 
Old 08-02-2008, 09:56 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Why do you think you need to pass double quotes?
 
Old 08-02-2008, 10:10 PM   #3
serconsult
LQ Newbie
 
Registered: Jul 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Sometimes inspiration waits until you finishing hitting the post button. I continued trying and came up with this revision which works.

strcpy(runstr, "sudo -u server /u/server/exe/do_agent \"");
strcat(runstr, wcamp);
strcat(runstr, "\" \"");
strcat(runstr, agentid);
strcat(runstr, "\" ");

I am sure there is a more elegant way to do this though.
 
Old 08-02-2008, 10:14 PM   #4
serconsult
LQ Newbie
 
Registered: Jul 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Red face

Quote:
Originally Posted by Mr. C. View Post
Why do you think you need to pass double quotes?
I don't know -- If you look at my first code example, I was trying to pass a single quote within the double quotes (not easy to see), but they were dropping off as well.
 
Old 08-02-2008, 10:20 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
You only need to protect shell meta-characters, and your examples have none.

Regardless, consider what is happening. The system() call, invokes a shell. That shell strips away the first layer of quotes. So you have to protect any quotes that you want to pass that shell.

You don't need the strcats - just backslash your double quote to escape it in C:

char *foo ="This is \"double quote\" protected\n";
 
Old 08-04-2008, 11:14 AM   #6
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Quote:
Originally Posted by serconsult View Post
Sometimes inspiration waits until you finishing hitting the post button. I continued trying and came up with this revision which works.

strcpy(runstr, "sudo -u server /u/server/exe/do_agent \"");
strcat(runstr, wcamp);
strcat(runstr, "\" \"");
strcat(runstr, agentid);
strcat(runstr, "\" ");

I am sure there is a more elegant way to do this though.
Code:
sprintf(runstr, "sudo -u server  /u/server/exe/do_agent  \"%s\" \"%s\"", wcamp, agentid);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to put a string (w/ dbl & sgl quotes) into a pipe without it being changed ? lumix Linux - Newbie 8 05-15-2008 10:03 AM
passing parameters in string in C shifter Programming 8 04-11-2008 09:01 AM
Replace a string including simple quotes. philipina Programming 4 03-18-2004 08:01 AM
passing a string to a function jkobrien Programming 8 11-05-2003 01:41 PM
passing quotes in shell script Tenor Trombone Programming 1 04-18-2001 09:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration