-1
echo "yes" | my_program < input_file

I'm getting Ambiguous input redirect error in csh. How to solve this?

  • 2
    | redirects the input. < also redirects the input. Pick one. – Stephen Rauch Nov 14 '17 at 6:34
  • Using both | and < works in bash. – DarkHeart Nov 14 '17 at 6:37
  • 3
    How do you want to solve it? Which is the redirection that you actually want to do? We are not telepathic. You have to tell us what input you actually want to send to the program. – JdeBP Nov 14 '17 at 6:45
  • I have added a sample expect script to my answer, I can refine it somewhat to your needs if you could specify what the question is that you get asked by my_program and which you want to send "yes" as reply to... – thecarpy Nov 15 '17 at 22:03
1

According to a comment left by op, my_program sometimes expects a response to a yes|no question.

This means the op wants to use expect, use the right tools for the task; from the man page of expect:

Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

Now, I do not know what the prompt says that the op's program puts out, but, assuming it starts with Are you sure the following should work:

#!/usr/bin/expect

spawn  bash -c "my_program < /tmp/input"

expect {
     "Are you sure" {
     send "yes"
     }
     eof
}

You simply create a shell script with the above and execute it instead of the command you want to execute.

More generic expect script, wants four arguments: "program", "input file", "question", "your reply" (where the latter two are optional, if "question" does not come, no "reply" will be sent and the script will just quit successfully):

#!/usr/bin/expect
set arg1 [lindex $argv 0]
set arg2 [lindex $argv 1]
set arg3 [lindex $argv 2]
set arg4 [lindex $argv 3]

spawn  bash -c "$arg1 < $arg2"

expect {
     "$arg3" {
     send "$arg4"
     }
     eof
}

use as follows (assuming you copied the above into myexp.sh):

./myexp.sh "my_program" "/tmp/input" "yes"

In general, if you want multiple sources of input you use a here document to sort them.

my_program <<EOF
`cat input_file`
yes
EOF

Another option for multiple input:

echo 'yes' >> input_file

or using a temp file:

echo 'yes' > /tmp/myfile$$.txt
cat input_file >> /tmp/myfile$$.txt
my_program < /tmp/myfile$$.txt

It all really depends on what you want to achieve.

EDIT (From the comments):

( echo 'yes'; cat input_file;) | my_program 

works as well ... I find heredocs easier to read, YMMV.

  • You mean (echo 'yes'; cat input_file) | my_program, right ? – thecarpy Nov 14 '17 at 9:07
  • csh reports {: Command not found. on my box ... upvoted your initial comment as it put me on the way to another option ;-) – thecarpy Nov 14 '17 at 9:12
  • Apologies I missed the csh tag on the question – roaima Nov 14 '17 at 11:19
0

The problem is that you're piping the stdout of echo to the stdin of my_program...

echo "yes" | my_program 

... while at the same time you are also feeding the content of input_file to the stdin of my_program:

my_program < input_file

A process (in this case my_program) can't be fed on stdin from two different sources, so you need to choose which one must provide the input.

  • my_program sometimes gives out a yes/no prompt. That is why I'm doing echo "yes" so that it takes whenever the prompt comes up. How do I achieve this? – aviator Nov 14 '17 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.