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 ?
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.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 ofpopen(), then build that value into the second command: