Operating System - HP-UX
1846576 Members
2069 Online
110256 Solutions
New Discussion

in .profile, how to write "waiting 10 sec" and "user reply = enter"

 
SOLVED
Go to solution
Gary L
Super Advisor

in .profile, how to write "waiting 10 sec" and "user reply = enter"


Hi

I have been modifying the ".profile" file.
...
echo "A B : \c"
read REPLY

if [ "$REPLY" = "A" -o "$REPLY" = "a" ]
then
do actionA
fi
...

if [ "$REPLY" = "B" -o "$REPLY" = "b" ]
then
do actionB
fi
...

How to do/write when $REPLY = enter (user doesn't input A or B, just click "enter") or waiting 10 sec then system auto choose A, in other words: if [ "$REPLY" = " (enter)???" -o waiting = 10sec] then do actionA fi. How to write waiting 10 sec and input "enter"

Any answers will be very appreciate.

-G
8 REPLIES 8
Steven Schweda
Honored Contributor
Solution

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

A forum search for:
sleep trap
found (among other things) my answer in:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1119616

Even if it offers nothing else, my example
uses a ten-second delay, so it should need a
bit less editing than it might otherwise.
Gary L
Super Advisor

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

Hi Steven

Thank you very much for your fast reply.
I will check the hyperlink that your posted.

Have a good day.

-G
Dennis Handly
Acclaimed Contributor

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

>if [ "$REPLY" = "A" -o "$REPLY" = "a" ]

If you are doing lots of ifs, you may want to switch to a case:
case in "$REPLY"
A|a) actionA ;;
B|b) actionB ;;
*) # not one of the above
;;
esac

If you just have one letter replies and/or where you don't care about upper vs lower case you can have it upshifted or downshifted:
typeset -u REPLY
typeset -l REPLY

You can also use select to give a menu of selections.
Bill Hassell
Honored Contributor

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

The only timeout mechanism in HP-UX is the line command (man line) and it works perfectly for this application:

echo "A B: \c"
REPLY=$(line -t 10)
case "$REPLY" in
a|A ) doActionA
;;
b|B ) doActionB2
doActionB2
;;
"" ) doEnterOnly
;;
* ) doNeitherAorB
;;
esac

Now if you require "A" or "B" and must exit if no answer (just "enter") or no answer in 10 seconds, change to an endless loop until an acceptable answer is given:

while :
do
echo "A B: \c"
REPLY=$(line -t 10)
case "$REPLY" in
a|A|b|B ) break ;;
"" ) echo "No answer, exiting"
exit
;;
* ) echo "wrong answer, try again"
;;
esac
done

The $REPLY variable will have the answer given. The case statement allows for partial answers using regular expressions as in a*|A*|b*|B* ) to match anything starting with a,b,A,B and so on.


Bill Hassell, sysadmin
Gary L
Super Advisor

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

Hi Dennis and Bill

Thank you very much for your good suggestions and details above.

Have a good day.

-Gary
Gary L
Super Advisor

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

Hi Bill or others

One more question for you:

Actually, my goal is that: When oracle user login, System will gives him two or more options: A and B (C,D...) for enter different databasesA, B ... If oralce user doesn't choose any one, just wait 10 sec, system will auto execute actionA goes to the databaseA eq. the user choose A. In other words, in file /home/oracle/.profile, I wanna set actionA, databaseA is the default if user doesn't choose anything.

Does it could be achieved in HP-UX script?

Thanks

-G
James R. Ferguson
Acclaimed Contributor

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

Hi Gary:

> In other words, in file /home/oracle/.profile, I wanna set actionA, databaseA is the default if user doesn't choose anything.

Bill has already shown you how to do this. An empty response, denoted by "" will signify either the absence of any user input in the alloted 10-seconds or that the user simply pressed the return/enter key.

A bit more of the script that Bill offered you looks like the following. Like Dennis, I like to use the 'typeset' to unconditionally translate all input to lowercase thereby making subsequent conditional testing easier:

# cat .somepiece
#!/usr/bin/sh
typeset -l REPLY
function doActionA
{
echo "doing A"
}
function doActionB2
{
echo "doing B"
}
function doNeitherAorB
{
echo "doing neither A nor B"
}
echo "A B: \c"
REPLY=$(line -t 10)
case "$REPLY" in
a ) doActionA
;;
b ) doActionB2
doActionB2
;;
"" ) echo "Default action taken!"
doActionA
;;
* ) doNeitherAorB
;;
esac

...If this is the sole objective of the user's profile, I would consider having the login profile 'exec' a complete, external script where the script can be maintained unto itself. Shell functions in login profiles can also have annoying side effects. Be aware, too, that adding user input logic to a login profile means that unless you test for "interactivity" with a terminal, the profile becomes difficult to source (read) for other purposes. These are whole discussions aside.

Regards!

...JRF...
Gary L
Super Advisor

Re: in .profile, how to write "waiting 10 sec" and "user reply = enter"

Hi James

Thanks for your reply.
I will check it later as a urgent meeting.

-G