Results 1 to 5 of 5
Hi All,
Im strugging to write a script to take lines of output of one script and pass each line as a parameter to another script, i.e.
Run install script
...
- 07-30-2007 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 1
Script output redirection problem
Hi All,
Im strugging to write a script to take lines of output of one script and pass each line as a parameter to another script, i.e.
Run install script
For every line output by install script
Run status script with install script output line as the input
For example, if install generates the lines
"Done1"
"Done2"
"Done3"
I want to call
status "Done1"
status "Done2"
status "Done3"
Ive been trying stuff with pipes but to no avail.
Any help appreciated - thanks!
- 07-30-2007 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Suppose we have install as:
then we take each line from the output and do something with it -- here just an echo:Code:#!/bin/sh echo "Done1" echo "Done2" echo "Done3" exit 0
To produce:Code:#!/bin/sh # @(#) s1 Demonstrate processing output line by line. set -o nounset echo echo "GNU bash $BASH_VERSION" >&2 echo for i in $( ./install ) do echo " do something with line :$i:" done exit 0
See http://www.tldp.org/LDP/abs/html/index.html for help in bash scripting ... cheers, drlCode:% ./s1 GNU bash 2.05b.0(1)-release do something with line :Done1: do something with line :Done2: do something with line :Done3:
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 07-30-2007 #3
drl's solution mostly works, but it's missing one key part. Before the for loop, you should set
The reason for this is that normally, the input to the for loop would get split on any whitespace. You only want it to split on newlines (octal 012, decimal 10). $IFS determines what the input splits on.Code:IFS='\012'
Now, drl's script works, but you would need to somehow tell the script the name of the program as a parameter or hardcode it in. That is one solution. A solution I prefer is:
Now you simply execute a program, and pipe it into this script. For instance:Code:#!/bin/bash # @(#) input_to_param Demonstrates how to read STDIN and turn each line into a paramter to another program # Usage: PROGRAM | input_to_param while read line; do status "$line" done
As a final solution, I will share with you one of my favorite programs. It is called xargs. xargs is designed for situations where you want to do the same thing with multiple lines of input.Code:./install | ./input_to_param
To use it is pretty simple:
Here, xargs will use '{}' to denote each line, and will call "status {}" on each line of input.Code:./install | xargs -I{} status "{}"
See how this massively simplifies the problem?DISTRO=Arch
Registered Linux User #388732
- 07-30-2007 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi, Cabhan.
Yes, it's always good to get more than one point of view, and alternative methods of solving problems. Thanks for posting.
I copied this line, and substituted echo for status.Code:./install | xargs -I{} status "{}"
What is your version of xargs? Mine is "GNU xargs version 4.1.20", it prints {} literally, and it doesn't like -I{} ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 07-30-2007 #5
O.o
I'm using xargs 4.3.7.
Check your man page, but that is weird. I guess our versions are too far apart. Check for an option that lets you set replace-str.Code:alex@danu ~ $ xargs --version GNU xargs version 4.3.7 Built using GNU gnulib version 2007-05-26 alex@danu ~ $ echo 'a b c' | xargs -I{} echo {} a b c
As an interesting note, in this case, the "-I{}" is unnecessary, as by default, xargs simply appends each piece of input to the command. However, "-d \n" might be necessary, as by default xargs splits on whitespace, while we only want it to split on newlines. But since my "-d" was being weird (xargs was splitting on newlines anyway), I decided to throw it in just in case.DISTRO=Arch
Registered Linux User #388732


Reply With Quote