LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2007, 09:57 AM   #1
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Rep: Reputation: 15
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
 
Old 01-11-2007, 10:03 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 01-11-2007, 10:31 AM   #3
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
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
 
Old 01-11-2007, 11:43 AM   #4
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
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?
 
Old 01-11-2007, 12:13 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
But for the development/testing purposes, why cannot you simply comment out the "reading from input" lines in your script? Just a curiosity...
 
Old 01-11-2007, 12:33 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...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
 
Old 01-11-2007, 05:45 PM   #7
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash more bloated than ksh? noir911 Linux - General 4 10-10-2006 04:58 PM
It is possible to change ksh script on a SUN into bash richikiki Programming 2 06-20-2006 06:08 AM
processing a ksh shell in bash environment ntoughe Programming 9 09-12-2005 05:09 AM
bash/ksh variables question tpe Programming 2 08-11-2005 09:39 AM
ksh commands in bash nkelle Linux - General 2 11-13-2003 11:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration