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

  • SEARCH
  • Home
  • 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 use a for loop to print every date between 2 dates.
I want to know how long a command takes to run. At the moment
How can I spawn a process after a delay in a shell script? I
I have a bash shell script that loops through all child directories (but not
I have this Shell script and I've managed to muck it up and I
So I have .csh script generate.csh I want to call another .csh script from
I have a ridiculous question due to a ridiculous problem. Normally if I want
I would like to automate the following svn command. Note this command produces the
EDIT : I don't know in advance at which column my digits are going
On both, my Cygwin and my Linux box (Debian) I'm experiencing same issue: I'm

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.