Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing Enter Key

Status
Not open for further replies.

nAtHaNs

Technical User
Feb 14, 2002
57
US
hi all,

i have to make a script that calls ontape command and will automate the mounting of tapes to the drive. My problem is how to simulate/execute the enter key just like pressing the enter key on the keyboard since the ontape command requires that the enter key be pressed in order continue.

Can someone help me with this!

thank you!
 
Does your script always require the enter key or just sometimes? (different solutions) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi:

ontape is originally designed to be run in the foreground by a user. Right? Place the keystrokes the user would ordinarily perform in a file and do something like this:

automated_script < keystrokes

Regards,

Ed
Schaefer
 
Hi:

The problem is &quot;How do you emulate the user pressing the <CR> key&quot;. I thought about this some more and ran a quick test.

On short notice, this is what I've come up with: Place the ocatal representation of <CR> on just one line:

\012

I think that should work.

Regards,


Ed
Schaefer
 
Hi,

try the olded' solution with an input file containing an empty line. Jean Pierre.
 
hi ed,

yes that's what im trying to do, emulate the user pressing the <CR> key. Ive tried putting the \012 on line but the script has errors like:
#test script
\012

$./test
./test: 012: not found.
$

Nathan
 
Nathan,

You need to feed the correct keystrokes to your &quot;ontape&quot; script.

so - put the correct keystrokes into a file called keystrokes.ontape (that's the \012 business above, but also any other keystrokes you need)

and then run your ontape script like this

ontape_script < keystrokes.ontape
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Mike,
Could you elaborate on the &quot;\012 business &quot; I guess I've never used this method to send the Enter key, my simple test seem to view the \012 as &quot;\012&quot; i've used expect and perl to perform &quot;robot emulation&quot; and automate several manual tasks, could I just use the \012 business to automate telnet and ftp or is that different because it doesn't read from STDIN ? Thanks in addvance,

The chunk_monk
 
telnet's one of those programs that doesn't do well with ordinary stdin redirection..... bit of a pain really

I think I was wrong about putting \012 into a file, I've seen that trick (\xxx) used to specify control characters but never used it myself.

I've always inserted control characters using the ^V trick.

Or... and this might be easier in this case, use the here-document thing.

a_shell_script.sh <<!
yes
!

that runs a script called a_shell_script.sh and, when the script asks for input, gives it a 'yes'

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Ahhh yes, i too use the old ^V for control characters, just wanted to make sure i wasn't missing anything, thanks , haven't had the need to use here documents but from what i know that may be the trick to use for this particular requirement..later
 
hi mike,

I've tried redirecting the keystrokes (\012) to the ontape script but it still doesnt work. As i understand, the moment you execute ontape_script < keystrokes.ontape, it will not care if i have already changed the tape or not and will execute the keystrokes right away. Since my script tries to backup (level 0) the database, it will take awhile before my script needs the keystroke (enter key).

Please take a look at my sample script:

Exiter=1
LoopNumber=1
TapeCounter=1
WaitMessage=&quot;Mount volume&quot;
EndMessage=&quot;Backup successful&quot;
mksysb -i /dev/rfd0 > TapeMsg 2>&1 & # for testing purposes, i used mksysb instead of ontape

while [ $Exiter -eq 1 ]
do

# Look for the Wait Message and then Change the tape

TapeMessage=`/usr/bin/tail -1 /TapeMsg | /usr/bin/cut -f 1,2 -d &quot; &quot;`
BackupEnded=`/usr/bin/tail -1 /TapeMsg`
sleep 5

# just to keep track of the messages.
echo &quot;Loop Number : $LoopNumber&quot;
echo &quot;-------------------------------------------------------&quot;
echo &quot;TapeMessage = $TapeMessage&quot;
echo &quot;BackupEnded = $BackupEnded&quot;
LoopNumber=`expr $LoopNumber + 1`

if [ &quot;$TapeMessage&quot; = &quot;$WaitMessage&quot; ]
then

# Change the tape

echo &quot;Detected Tape Request at `date`\n&quot;
# will have several lines of tapeutil here for changing tapes
sleep 5
\012 # octal representation of Carriage Return or Enter key
TapeCounter=`expr $TapeCounter + 1`
fi

# Look for the End Message and exit the loop

if [ &quot;$BackupEnded&quot; = &quot;$EndMessage&quot; ]
then

echo &quot;Detected Program End\n&quot;
Exiter=0
fi

done

thank you.

Nathan
 
Hi Nathan - I see where you are confused now. The good news is we can clear up that confusion pretty quickly. The bad news is that (looking at a comment in your script) you need to do the tape change commands from inside your script as well - later though.

First of all:

The heart of your script is the mksysb command.

The command you use is a perfectly valid one, but let's have a look at it in more detail:

mksysb -i /dev/rfd0 > TapeMsg 2>&1 &

This runs the mksysb command, in the background, and pipes its standard output and standard error to a file calles TapeMsg.

But mksysb's standard input is *not* being redirected, and this is what you need to do.

The ordinary way of redirecting input would work like this:

mksysb -i /dev/rfd0 > TapeMsg 2>&1 <keysfile &

This will make mksysb run as before, but look in the file keysfile whenever it needs a response rather than look at the keyboard. The keystrokes in the file keysfile will sit there until they are asked for by mksysb.

All very well - but now the bad news. This isn't what you want to do.

From looing at your script you need to:

Watch for the tape change condition, change the tape, let mksysb carry on.

For this you will need to something like expect or Perl - do you have either of those packages available?

Alternatively - your tape changer. Can you get it to react to and end-of-tape condition by automatically changing the tape? Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
mike,

that is actually the root of my problem. my 3581 LTO does not react to end-of-tape condition (do you know how to configure this?). that's why i'm making this script.

your solution is excellent but it wont be able to monitor my tape condition and eventually change the tape.

i dont have perl or expect. maybe i still need to read on those packages so i can work on them.

thank you.
 
Nate,

Ok...

I have a cunning plan (said Baldric, holding a turnip)...

It depends upon you getting Perl - it's actually on your AIX application CD so you can install it from there - can you do that?

This may not work. It depends upon whether mksysb reads in STDIN in the normal way.

In a perl script, start mksysb so that it runs taking its STDIN from the Perl script - so you can feed it keystrokes from there - and putting its output into a text file.

From within the Perl script, once you've started the mksysb, you watch its output in the file.

You wait until you see the right message.

You change the tape, from within the Perl script (if you can do it from the shell you can do it from Perl)

You send mksysb an ENTER key.

That doesn't sound too bad. We would have to do some tests to make sure that mksysb will accept commands in that way, and that we can follow the file it creates as the file is written.

So -- Can you get Perl installed? Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Mike,

yes we can install perl but the problem is i'm not yet familiar with perl. can you give a sample script as a guide? thanks again.

nathan
 
Yes, a sample script shouldn't be a problem.

The main issue really is 'does mksysb read its STDIN for commands or does it open /dev/tty?'. Before we/you/I put any effort into writing a script we should answer that question.

First test -- Does

mksysb [other stuff] <<!

!

Give mksysb an ENTER key when it asks for it? Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Mike,

The command

mksysb [other stuff] <<!

!

still would require the enter key be pressed before it will continue to the next task.
 
That was my question - if you read my post.

Does this work? I don't have an AiX system to check this on.

Looks pretty moot anyway as Nathan seems to have given up. Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Nathan

If you need to ignore use of return key, why not change your terminal settings. In HP-Unix `stty raw` should help - I believe the argument is something else in AIX (-iocntl ?). Just remember to change the settings back soon after finished. Here is a 'press-any-key' script to illustrate what I'm saying.

#!/bin/sh

echon() {
if [ -n &quot;`echo -n`&quot; ] ; then
echo &quot;$@\c&quot;
else
echo -n &quot;$@&quot;
fi
}
getkey() {
TTYSTATE=`stty -g`
stty raw
stty -echo
key=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty &quot;$TTYSTATE&quot;
echo &quot;\nkey is ${key}&quot; | cat -vu
}

echon &quot;Press any key to continue: &quot;
getkey
# END OF SCRIPT

Better yet, if you are waiting on a particular key you might want to catch it with a basic C program that reads characters until the desired one is found. It then puts it to stdout. Try this for letter 'd'.

#include <stdio.h>
#include <stdlib.h>

main()
{
char character;
character = 0;
while ((int)character != 100) character = getchar();
printf(&quot;%c\n&quot;, character);
exit(EXIT_SUCCESS);
}

And then replace
key=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
with your compiled program name
key=`a.out`

BTW, I thought escape is 27 in ASCII (base-10) and 12 is form feed.

Best of luck.
ND
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top