Find the answer to your Linux question:
Results 1 to 2 of 2
not sure if this is the best place for this or "miscellaneous". anyway... i have a script, blah.sh. within, it calls bleh.sh. bleh.sh has a read <variable> point. blah.sh (also) ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Location
    Minneapolis, MN
    Posts
    15

    passing multiple outputs / echos to script that calls other script(s)


    not sure if this is the best place for this or "miscellaneous". anyway...

    i have a script, blah.sh. within, it calls bleh.sh. bleh.sh has a read <variable> point. blah.sh (also) has a (separate) read <variable> point. what i want to be able to do is send the first read variable and the second read variable to the blah.sh script (for example, y and y)

    bleh.sh:
    Code:
    #!/bin/sh
    
    echo "enter bleh: "
    read bleh
    
    if [ $bleh = "y" -o $bleh = "Y" ]
    then
      echo "GOOD: $bleh"
    else
      echo "BAD: no."
    fi
    blah.sh:
    Code:
    #!/bin/sh
    
    ./bleh.sh
    
    echo "enter blah: "
    read blah
    
    if [ $blah = "y" -o $blah = "Y" ]
    then
      echo "GOOD: $blah"
    else
      echo "BAD: no."
    fi
    
    echo "blah blah blah"
    when running blah.sh manually, the steps go fine. it calls bleh, you enter y, it echos, it goes on to the rest of blah, you enter y, it goes on....

    if you take out bleh.sh and do something like
    echo y | ./blah.sh

    it's also fine

    but if bleh.sh is there (or if there are two read points in the file), then i can't pass two "y"s to the script to have one go to one and the second go to the next

    echo y | (echo y | ./blah.sh) or anything like that, doesn't work. in fact, although nothing gets sent to the second read point, the second if -o block returns "blah.sh: line x: [: too many arguments"

    i don't know if version matters in this case, but it's rhel 4

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    So indeed. The problem here is that read breaks on newlines. You need to somehow pass it 'y' on two different lines.

    There are two general approaches to this problem.

    If you are always going to be passing the same input, that's what the "yes" command was invented for. Run "man yes" for details.

    If you need to pass different lines, you have two choices:

    1) Put all of your input into a file and call the script using that file for input ("./blah.sh < file")
    2) Use a here document (explained at [1])

    [1] Here Documents

Posting Permissions

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