LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-13-2010, 05:57 PM   #1
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
reading command output by line in Bourne script?


I have a command which generates the following output:
Code:
$ sudo vnconfig -l
vnd0: not in use
vnd1: not in use
vnd2: not in use
vnd3: not in use
$
I would like to find the first vnd device which is not in use.
  • The following is a nice first try...
    Code:
    $ echo $(sudo vnconfig -l) | grep 'not in use'
    vnd0: not in use vnd1: not in use vnd2: not in use vnd3: not in use
    ...but all lines are concatenated together.
  • The following seems reasonable, but the results are far worse:
    Code:
    $ cat test.sh
    #!/bin/sh
    
    for LINE in $(vnconfig -l) ; do
        echo $LINE
    done
    $ sudo sh test.sh
    vnd0:
    not
    in
    use
    ...
    $
  • Reading the output line-by-line would be ideal, but so far my foo is weak:
    Code:
    $ cat test.sh
    #!/bin/sh
    
    while read LINE ; do
        echo $LINE
    done < $(vnconfig -l)
    $ sudo sh test.sh
    test.sh[5]: cannot open vnd0: not in use
    vnd1: not in use
    vnd2: not in use
    vnd3: not in use: No such file or directory
    $
Is there a way to read the command's output line-by-line without resorting to redirecting output into a temporary file?

Thanks.
 
Old 02-13-2010, 07:10 PM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
There is a possibility. But this requires to switch from sh to bash:
Code:
#!/bin/bash or /usr/bin/bash or /usr/local/bin/bash

(while read LINE ; do
    echo $LINE
done) < <(vnconfig -l)
 
Old 02-13-2010, 07:11 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Code:
device="$(vncconfig -l | grep "not in use" | head -n 1)"
device="${device%%:*}"
 
Old 02-13-2010, 09:49 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@OP, you can do it with just one awk statement

Code:
vncconfig | awk -F":" '/not in use/{print $1;exit}'
 
Old 02-13-2010, 10:47 PM   #5
BenCollver
Rogue Class
 
Registered: Sep 2006
Location: OR, USA
Distribution: Slackware64-15.0
Posts: 376
Blog Entries: 2

Rep: Reputation: 172Reputation: 172
Quote:
Code:
while read LINE ; do
    echo $LINE
done < $(vnconfig -l)

That was close. Here is one that will work.

Code:
vnconfig -l | while read LINE
do
    echo $LINE
done
 
Old 02-13-2010, 10:51 PM   #6
BenCollver
Rogue Class
 
Registered: Sep 2006
Location: OR, USA
Distribution: Slackware64-15.0
Posts: 376
Blog Entries: 2

Rep: Reputation: 172Reputation: 172
For your purposes, a pipe would be better than a while loop.


vnconfig -l | grep 'not in use' | head -1 | cut -d : -f 1


"grep" finds lines matching 'not in use'
"head -1" prints the first line
"cut -d : -f 1" prints the name
 
Old 02-14-2010, 02:48 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Pseudo code:
$(sudo vnconfig -l) | sed 's/not in use/not in use\n'/ | grep not in use?

End
 
  


Reply



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
PS command output reading ilnli Linux - General 3 10-15-2012 02:31 PM
Reading screen output line by line basildon Linux - Newbie 1 09-23-2008 01:33 PM
processing each line of output in Bourne shell? ocicat Programming 11 08-01-2007 01:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration