LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Enter key in bashscript (https://www.linuxquestions.org/questions/linux-general-1/enter-key-in-bashscript-427743/)

happy78 03-23-2006 01:20 PM

Enter key in bashscript
 
Hello all,

Im writing this script (A) which launches another one (B), which asks for input. I am able to echo my answer to the input line of B, but within the script, it doesn't do anything because I don't know what is the equivalent of "Enter Key" in bash script.

In other words, I want my script (A) to execute "Enter Key" autotmatically after the echo.

any suggestion is greatly welcome. thank you all for your time and assistance

Happy

pixellany 03-23-2006 02:50 PM

This is--I hope--PART of the answer.

In real time, you type a command and hit enter---that is the signal to the shell to act on what you just typed.

In a program, script, whatever, you pass a word to a function and you get a similar result.

Maybe one way to think of it is that "enter" adds the CR character. When a text variable is created, the CR is included.

AGAIN--I am sure that my explanation is imprecise and impure....;)

alexander_bosakov 03-23-2006 04:42 PM

Can you be more specific about what are you trying to do? 'echo' command sends CR by default, unless explicitly told not to do so with the '-n' option.

happy78 03-23-2006 05:04 PM

Quote:

Originally Posted by alexander_bosakov
Can you be more specific about what are you trying to do? 'echo' command sends CR by default, unless explicitly told not to do so with the '-n' option.

basicly, i just do echo "doThis"
and I expect it to jump to the next line.

alexander_bosakov 03-24-2006 01:15 AM

Well, from your description, I suppose you are trying something like 'echo "blabla">tty_of_the_scriptB"'. Did I got it right? Anyway, if we are talking about 'bash', the 'read' operator does precisely that - reads an input, assigns it to a variable(s) and proceeds to the next line. And it can read not only from keyboard, but from any open file descriptor (see bash manpage). So if you want one script to control another, you can use a named pipe, so called FIFO (see man mkfifo) - both script open the FIFO file - one writes to it, other reads. Or you can create 2 FIFOs for bidirectional communication.
Example:

# scriptA (invoking B)
...
mkfifo fifoAtoB
mkfifo fifoBtoA
exec 16<>fifoAtoB # Open fifoAtoB at file descriptor 16 for sending commands
exec 17<>fifoBtoA # Open fifoBtoA at fd 17 for receiving responces from B
scriptB& # Run scriptB either in the background, or on another tty
# it must open the same files, one for reading "doThis", one for responding
# BTW, background process will receive SIGHUP if it try to read or write to tty
while some_condition; do
echo "$COMMAND">&16 # Send command to scriptB
read -u 17 RESPONCE # Read responce from scriptB
# process $RESPONCE
done
....
# after scriptB finishes, clean up
rm -f fifoAtoB fifoBtoA
# END scriptA --------------

##############################

# scriptB (called by A)
...
exec 17<>fifoAtoB
exec 18<>fifoBtoA
...
while some_condition; do
read -u 17 COMMAND # get command from scriptA
# do something with $COMMAND
echo "$RESPONCE">&18 # return the result. Again, do not
# try read or write to the tty from a background process
done
...
# END scriptB -----------


All times are GMT -5. The time now is 07:52 AM.