Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-11-2006, 04:55 PM   #1
Senior Member
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
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!

Last edited by abefroman; 05-11-2006 at 04:59 PM.
Old 05-11-2006, 06:11 PM   #2
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with shell script using a variable from a unix command. abefroman Programming 1 05-10-2006 03:14 PM
Perl Script: Variable Names composed of Variables wwnexc Programming 4 02-06-2006 04:47 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 10:45 PM
runing perl unix script in windows 2000 anirudh Programming 3 04-15-2005 11:07 AM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 02:45 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration