LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-11-2006, 03:55 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,257

Rep: Reputation: 53
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 03:59 PM.
 
Old 05-11-2006, 05:11 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

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


Reply


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 02:14 PM
Perl Script: Variable Names composed of Variables wwnexc Programming 4 02-06-2006 03:47 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
runing perl unix script in windows 2000 anirudh Programming 3 04-15-2005 10:07 AM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM


All times are GMT -5. The time now is 09:19 AM.

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