Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:08:40+00:00 2026-06-15T11:08:40+00:00

I’m having a dilemma where I’m trying to pass a string through popen in

  • 0

I’m having a dilemma where I’m trying to pass a string through popen in C, but have it maintain the double quotes in the string. The string reads as follows:

ssh %s@%s grep -c \"%s\" %s%s

I need to run this command so it greps from a log on a remote system and returns the count for it. I’m passing different arguments to through the string, so it generates the username and password as well as the search string and target. However, the search string contains multiple words separated by whitespace characters, so I need the double quotes intact so it parses out the search string correctly. However, so far popen has been stripping the double quotes from the string so the search doesn’t complete. I’m not sure of another way to do the remote ssh command, or of keeping the double quotes in the statement. Any ideas?

Thanks!

*EDIT: Here is the full sprintf statement I’m using to generate the string as well as the popen command.

sprintf(command, "ssh %s@%s grep -c \"%s\" %s%s", user, server, search, path, file);
fd = popen(command, "r");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:08:41+00:00Added an answer on June 15, 2026 at 11:08 am

    popen forks a child which execs a shell with 2 arguments: -c and the command string you passed to popen. So any characters you pass that are meaningful to the shell need to be escaped so the shell does the right thing with them. In your case, you need the shell to KEEP the " characters so that the remote shell will get them, which is easiest to do by wrapping ' quotes around them:

    sprintf(command, "ssh %s@%s grep -c '\"%s\"' %s%s", ...
    

    However, this will only work if your search string does not contain any ' or " characters — if it does, you need to do some more complex escaping. You could instead use backslash escapes:

    sprintf(command, "ssh %s@%s grep -c \\\"%s\\\" %s%s", ...
    

    but THIS will fail if your search string has quotes or multiple consecutive spaces or other whitespace like tabs int it. To deal with all cases you need first insert backslashes before all relevant charaters in the search string, plus ' are a pain to deal with:

    // allocate 4*strlen(search)+1 space as escaped_search
    for (p1 = search, p2 = escaped_search; *p1;) {
        if (*p1 == '\'') *p2++ '\'';
        if (strchr(" \t\r\n\"\\'{}()<>;&|`$", *p1))
            *p2++ = '\';
        if (*p1 == '\'') *p2++ '\'';
        *p2++ = *p1++; }
    *p2 = '\0';
    sprintf(command, "ssh %s@%s grep -c '%s' %s%s", user, server, escaped_search, ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.