LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash/ksh: Automatically send response to a program (https://www.linuxquestions.org/questions/programming-9/bash-ksh-automatically-send-response-to-a-program-518367/)

jimieee 01-11-2007 09:57 AM

bash/ksh: Automatically send response to a program
 
Hi,

I'm working on a small script that has a sanity check to make sure the user can't easily run the script without meaning to. It has a subroutine that throws up something like this:

Code:

./myscript.sh
Are you you wish to continue?
1) Yes
2) No

My question is that for development/testing purposes what's the best way to automatically answer yes (1) to this?

I'm currently using:

Code:

{
./myscript.sh
} < answer.txt

How can I take the file out of the equation? Ksh is my first choice of shell, but I'd also like to know how to do it in bash (the above works in both).

Thanks,

James

druuna 01-11-2007 10:03 AM

Hi,

Take a look at the yes command. It can, repeatedly, output any string you like (man yes for details).

Hope this is what you are looking for.

jimieee 01-11-2007 10:31 AM

Thanks druuna,

but it turns out that what I was after was just a simple pipe:

Code:

echo 1 | ./myscript.sh
I think it's time to take a break, I really should have spotted that. Sorry for wasting your time :)

jimieee 01-11-2007 11:43 AM

Actually, back on this point. Is it possible to output "1" only once and hand back stdin to the terminal?

For my solution above, myscript.sh will get a "1" for the first request for stdin and then die when it fails to get the next request for stdin. If I used the yes command, it will answer "1" as many times as it's requested, which isn't what I want either.

I seem to remember a solution to this, that looked something like a perl filehandle, for example:

Code:

EOF << echo 1
./myscript.sh
EOF

Obviously that doesn't work in bash, or ksh, but does anyone know what I'm getting at? Or, more importantly, the correct way to implement it?

colucix 01-11-2007 12:13 PM

But for the development/testing purposes, why cannot you simply comment out the "reading from input" lines in your script? Just a curiosity...

unSpawn 01-11-2007 12:33 PM

...or use a variable as in
[ BUGTESTING -ne 0 ] && { # run yes/no code block
doSomething
} # End BUGTESTING block
so when you're finished testing you just prep your script with grep -v BUGTESTING myscript.sh > realscriptname.sh

wjevans_7d1@yahoo.co 01-11-2007 05:45 PM

or, if you dont' want to change your shell script, write another shell script (call it spike, for example). Let this shell script output "1" to standard output. Then it simply calls "cat", which sends stuff from standard input to standard output. The script is extremely simple:

#!/bin/bash
echo 1
cat

Then you simply do this:

spike | yourscripthere

Hope this helps.


All times are GMT -5. The time now is 05:39 PM.