Find the answer to your Linux question:
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 ...
  1. #1
    Just 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!

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Suppose we have install as:
    Code:
    #!/bin/sh
    
    echo "Done1"
    echo "Done2"
    echo "Done3"
    
    exit 0
    then we take each line from the output and do something with it -- here just an echo:
    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
    To produce:
    Code:
    % ./s1
    
    GNU bash 2.05b.0(1)-release
    
     do something with line :Done1:
     do something with line :Done2:
     do something with line :Done3:
    See http://www.tldp.org/LDP/abs/html/index.html for help in bash scripting ... cheers, drl
    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 )

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    drl's solution mostly works, but it's missing one key part. Before the for loop, you should set
    Code:
    IFS='\012'
    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.

    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:
    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
    Now you simply execute a program, and pipe it into this script. For instance:
    Code:
    ./install | ./input_to_param
    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.

    To use it is pretty simple:
    Code:
    ./install | xargs -I{} status "{}"
    Here, xargs will use '{}' to denote each line, and will call "status {}" on each line of input.

    See how this massively simplifies the problem?
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    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.
    Code:
    ./install | xargs -I{} status "{}"
    I copied this line, and substituted echo for 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, drl
    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 )

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    O.o

    I'm using xargs 4.3.7.
    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
    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.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...