LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with perl script using a variable from a unix command. (https://www.linuxquestions.org/questions/programming-9/problem-with-perl-script-using-a-variable-from-a-unix-command-443902/)

abefroman 05-11-2006 04:55 PM

Problem with perl script using a variable from a unix command.
 
It is not putting the number only in $swap.

How can I get it to store the 4th number only from free -m |grep Swap?

$swap=`free -m |grep Swap | awk {'print $4'}`;
if($swap < 100){
print "swap low";
}

It is putting the results form `free -m |grep Swap` as the variable and ignoring the | awk {'print $4'}` part

Thanks in advance!

taylor_venable 05-11-2006 06:11 PM

The backtick ` interpolates variables like $4, which is undefined. This means that your command was piping to "awk { print; }" which of course just prints the line. You need to escape the dollar sign in the awk command with a backslash. Running Perl with warnings enabled (using the -w option or writing "use warnings;" in the script) would have caught this. You should probably also put your single quotes outside the curly braces in the awk invocation, since the curly braces are themselves part of the awk script.


All times are GMT -5. The time now is 12:27 PM.