Visit Our UNIX and Linux User Community


How to send keyboard inputs toa UNIX command executed from a shell script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to send keyboard inputs toa UNIX command executed from a shell script?
# 1  
Old 11-16-2013
How to send keyboard inputs toa UNIX command executed from a shell script?

I have a unix command that prompts for 'y'. How do I run this from my shell script?
# 2  
Old 11-16-2013
Try
Code:
read v; echo "$v"

vs.
Code:
read v << EOF
y
EOF
echo "$v"

# 3  
Old 11-16-2013
Hi Sree10...

With your vague request I assume that your command is a native executable for the platform you are on.

As we cannot possibly know this command then a man page might show a flag to quench the Y input request and force an automatic continuation.

At this point I ask that you give us more information:-

1) OS.
2) Shell in use.
3) Terminal type, optional.
4) Machine, platform.
5) The special command.
6) And finally your attempts <- assuming it is possible in the first palce.

Help us, and we will attempt solutions.
# 4  
Old 11-16-2013
I am trying to run this on a Linux host. I have a customized shell script which when run prompts for some inputs. When i run this script manually from the command line, I can specify the input as y from the keyboard.
Code:
>  
/bin/test 
If this is correct, enter 'y':

But, when how do I invoke the same shell script from another shell script and pass y as an input.
# 5  
Old 11-16-2013
Hi Sree10...

Once again as we have no idea about your shell scripts nor commands then I have
created a very dirty example using a shell script generated on the fly awaiting a
physical "y" to be entered and an automatic "n" later, done longhand...
You will have to work out any subtleties, loops etc for yourself...
Code:
#!/bin/bash --posix
# An example ONLY...
> /tmp/Y
echo "Test for a remote Y on another shell script..."
echo -n "Enter y to continue:- "
read y
echo -n "$y" > /tmp/Y
echo -e "read wait_for_yes < /tmp/Y" > /tmp/test.sh
echo -e 'if [ "$wait_for_yes" == "y" ]' >> /tmp/test.sh
echo -e "then" >> /tmp/test.sh
echo -e "        echo 'Y was detected...'" >> /tmp/test.sh
echo -e "fi" >> /tmp/test.sh
echo -e 'if [ "$wait_for_yes" == "n" ]' >> /tmp/test.sh
echo -e "then" >> /tmp/test.sh
echo -e "        echo 'Auto N was detected...'" >> /tmp/test.sh
echo -e "fi" >> /tmp/test.sh
chmod 755 /tmp/test.sh
echo -e "Launch this command test.sh...\n"
/tmp/test.sh
echo -e "\nReturn back to the calling shell...\n"
echo "Check using N for the remote entry but not inputted manually..."
echo -e "Launch this command test.sh...\n"
echo -n "n" > /tmp/Y
/tmp/test.sh
echo -e "\nReturn back to the calling shell..."
rm /tmp/Y

Results on OSX 10.7.5, default bash terminal...
Code:
Last login: Sat Nov 16 23:41:57 on ttys000
AMIGA:barrywalker~> chmod 755 auto-yes.sh
AMIGA:barrywalker~> ./auto-yes.sh
Test for a remote Y on another shell script...
Enter y to continue:- y
Launch this command test.sh...

Y was detected...

Return back to the calling shell...

Check using N for the remote entry but not inputted manually...
Launch this command test.sh...

Auto N was detected...

Return back to the calling shell...
AMIGA:barrywalker~> _

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #230
Difficulty: Easy
The Advanced Research Projects Agency Network (ARPANET) was an early packet-switching network and the first network to implement the TCP/IP protocol suite.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to check a command executed sucessfully or not

Hi All, I am trying to write a shell script to check if a command executed successfully or not in rhel 7 and finding the installed tomcat version. I am using below script. var4=$(find / -name "catalina.jar" ! -size 0 |egrep -v... (6 Replies)
Discussion started by: sravani25
6 Replies

2. UNIX for Beginners Questions & Answers

UNIX Shell Script to Remove MongoDB Document-Based on Many inputs

Here I am in a position to write a Unix Shell script(.sh) to remove MongoDB collection documents. I know how to remove based on a condition like below and it works for me. eval 'db.Collection.remove({TimeStamp:{$lte: "'$var'"}}) But I need to change the remove statement based on a new parameter... (1 Reply)
Discussion started by: senthilmadhanT
1 Replies

3. Shell Programming and Scripting

awk command not getting executed in shell script

I am able to execute awk command from shell prompt. but the same command is not getting executed when written and run in a bash script the command from bash cmd prompt. awk '/world/{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile file: # hello world my... (4 Replies)
Discussion started by: ashima jain
4 Replies

4. Shell Programming and Scripting

Unable to pass send command in shell script

I am trying to automate a testcase . I am installing some software and it waits for user input after displaying "Do you want to continue ? " I am trying to do this in shell scripting. #!/bin/bash #!/usr/bin/expect -f /usr/bin/expect << EOF spawn apt-get install openjdk-7-jdk expect "*Do you... (1 Reply)
Discussion started by: Abdul Navaz
1 Replies

5. Programming

Some error with number of keyboard inputs occured with this code for reversing a string..

i used a two-way linked list "node" for the code:: #include<stdio.h> #include<malloc.h> void insert(); void reverse(); struct node { char c; struct node *next; struct node *back; }*start=NULL; int main() { int n,i; (4 Replies)
Discussion started by: mscoder
4 Replies

6. Shell Programming and Scripting

How to send email once a day at certain time in unix shell script

hi, i have to send an email once a day at ceratin time say 22. i have tried with date commad, but not working.:( HOUROFTHEDAY=`date +'%H'` if ; then mailx -s "Info" emailid@org.com < $ProcessStatisticsFile fi Please help me... (5 Replies)
Discussion started by: sreelu
5 Replies

7. UNIX for Dummies Questions & Answers

Help how replace stardard keyboard inputs by arguments at run time of a script

Hello Everybody, Please help. I was trying to automate the use of a third-party given shell script. The script is written to be used at run-time to collect a few variables to be provided by the user through key board, in the fashion as below: ./runcommand please provide a file name to... (6 Replies)
Discussion started by: Dingrong
6 Replies

8. Shell Programming and Scripting

perl - why is the shell script executed before the print command?

i'm writing some simple scripts to help me learn perl. why does the print command get called after the shell script is executed? the purpose of the shell script is to simply echo to the screen "script run". which is does, but before the print command, you can clearly see the shell script is... (3 Replies)
Discussion started by: mjays
3 Replies

9. UNIX for Advanced & Expert Users

Unix shell Script To Send SMS

hi , can anyone help with a script to send SMS to some mobile numbers on some conditions. (1 Reply)
Discussion started by: sdcoms
1 Replies

10. Shell Programming and Scripting

Unix shell script couldn't be executed. Pls help!

I have wrriten a script to call sql script to do some work in database. However, the script couldn't be executed. The only information was: ksh: ./updt_attrib.ksh cannot execute. Please help me to identify where the problem is. I post script here for your reference. Thanks a lot. #!/bin/ksh ... (8 Replies)
Discussion started by: duke0001
8 Replies

Featured Tech Videos