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 3224168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:11:29+00:00 2026-05-17T16:11:29+00:00

I want the output of the shell command ( echo free | grep Mem|

  • 0

I want the output of the shell command (echo free | grep Mem| awk '{print $2}') collected in a variable so that I can use it in a C program. So I have the code here.

 system("TOTAL=$(echo `free | grep Mem| awk '{print $2}'`)");
 popen("grep -v procs $1 | grep -v free | awk '{USED=TOTAL-$4-$5-$6;print USED}'", "r");

Can I use the variable TOTAL in the same program inside a popen() call as shown above ?

  • 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-05-17T16:11:30+00:00Added an answer on May 17, 2026 at 4:11 pm

    You will need to create a string which contains the complete script that you want run, or you will need to create a script which can be run simply, and then arrange to read the output of that script with popen(). Either is possible; which is easier depends on the level of your scripting skills vs your C programming skills.

    char command[4096];
    
    strcpy(command, "TOTAL=$(free | grep Mem| awk '{print $2}')\n");
    strcat(command, "grep -v procs $1 | grep -v free |\n");
    strcat(command, "awk '{USED=TOTAL-$4-$5-$6;print USED}' TOTAL=$TOTAL\n");
    
    FILE *in = popen(command, "r");
    
    ...read the results, etc...
    

    The string operations simplify the first shell script, and then pass the value of TOTAL calculated to awk.

    The other way to do it would be to read the output from free | grep Mem | awk '{print $2}' – the value that is TOTAL – from one use of popen(), then build that value into the second command:

    char command[4096];
    
    strcpy(command, "free | grep Mem| awk '{print $2}'");
    char total[20];
    FILE *in1 = popen(command, "r");
    ...read TOTAL into total...
    
    strcpy(command, "grep -v procs $1 | grep -v free |\n");
    strcat(command, "awk '{USED=TOTAL-$4-$5-$6;print USED}' TOTAL=");
    strcat(command, total);
    
    FILE *in2 = popen(command, "r");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign the output of a shell command into a make variable.
I want to run a mysql command and set the output of that to
I am writing a shell script on Ubuntu and use the sed command to
I have a case to want to execute the following shell command in Python
I have a shell variable: all_apk_file=a 1 2.apk x.apk y m.apk I want to
I want to echo the PATH variable, with the goal to get the same
I want to use a for loop to print every date between 2 dates.
Possible Duplicate: Deflate command line tool Yes, I know I can use PHP itself
I want to do immediate expansion of a shell command within a Makefile, but
I can call system(echo, ''), and this won't be interpreted by a shell. I'll

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.