LinuxQuestions.org
Visit Jeremy's Blog.
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 11-05-2007, 03:16 PM   #1
johnfman
LQ Newbie
 
Registered: Sep 2007
Posts: 13

Rep: Reputation: 0
cant make 'read' work with grepped output


I cant gegt the read command to read items grepped from df command output. Here's my command line:

/bin/df -k /dev/hda2 | grep 'opt' | read filesys total used avail pct directory

Only one line is reurned from the grep, with several numbers. But nothing exists in the variable names following the read command. My IFS is set to default.

What's up with this?

jaf
 
Old 11-05-2007, 05:19 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Try it like this:

Code:
/bin/df -k /dev/hda2 | grep 'opt' | while read filesys total used avail pct directory; do
  echo "filesys=$filesys total=$total etc."
done
 
Old 11-05-2007, 05:56 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
use awk, it combines grep and while loop together

Code:
/bin/df -k /dev/sda2 | awk 'NR>1{print "filesys:"$1" total: "$3}'
 
Old 11-06-2007, 08:36 AM   #4
johnfman
LQ Newbie
 
Registered: Sep 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Question Good answers, but need a bit more

Both of those solutions work, echoing to the screen the numbers from the df output. My problem is, however, that I need one of those numbers (the pct number) in a shell variable so I can test it with a conditional statement (I'm checking Oracle filesystems for near-fullness). My script comes from a Solaris box but does not work as is in bash.

Thanks!

John
 
Old 11-06-2007, 11:14 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
The echoing is just an example of what to do with the variable. In my example, the values are taken from the variables and echoed to the screen. You never specified what you wanted to do with the variables and so an echo is the simplest use as a demonstration.
 
Old 11-07-2007, 09:30 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
you are probably using that rubbish bash shell

try the one true shell!

/bin/ksh

Code:
:primalA$ /bin/df -k . |grep appl | read filesys total used avail pct directory
:primalA$ echo $total $used $avail
9437184 5397744 4039440
Incidentally did you know you can do similar stuff with set?

Code:
:primalA$ set -- $(/bin/df -k . |grep appl )                                            
:primalA$ echo $5 $4 $3 $2 $1
58% 4039664 5397520 9437184 dyfp0001d:/vol/app01/qt_prima_APP00928_pa/l
 
Old 11-12-2007, 08:05 AM   #7
johnfman
LQ Newbie
 
Registered: Sep 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Matthewg42 -

You are right I did not state the whole problem in my first post, but I could not even get an echo to work. Your solution works inside the 'while' loop but the variables lose their definition after the 'end'. Wonder what will happen if I put the whole 'df-then-test' section inside the 'while'.

billy -

this script came from ksh - if I had it here there wouldnt be an issue. No, I didn't know that 'set' works in that way. That is VERY cool!

Thanks!

jaf
 
  


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
NRPE: Unable to read output theking2 Linux - Software 4 08-04-2010 04:59 PM
USB HardDrive mounts read-only. Is there anyway to make it read-write? Fritz_Katz Linux - Hardware 7 09-13-2007 06:50 PM
Example share in smb.conf doesn't work - read/write vs read only kleptophobiac Linux - Networking 0 09-01-2004 07:14 PM
how do I make a read-only file NOT read-only? robster Linux - General 1 02-17-2004 09:11 PM
Read the output from a pipe with bash ? fluppi Linux - Software 3 01-13-2004 12:59 PM


All times are GMT -5. The time now is 07:43 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