1828926 Members
2905 Online
109986 Solutions
New Discussion

wait output in a script

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

wait output in a script

Hi all,

I am developing a script that does various functions and one of these is to check a user ssh connection from an AUTH server to the server where the script is running.

I want to make sure the user doesnt skip this step so I have written a bit of code:

read ANS?"Please copy and paste the authorised key for the CA user on $AUTHSERV here >>>"

if [[ $ANS = $KEY ]] ; then

print "ssh-rsa $ANS ca@AUTHSERV" > $CAHOME/.ssh/authorized_keys
chmod 644 $CAHOME/.ssh/authorized_keys
print "INFO: Key has been accepted, please test ssh connection from $AUTHSERV and ca account and run the command 'touch /
tmp/ldap_ssh_test' - - you have 5 minutes "
CheckSsh



else

print "The key you have entered does not match the key in $CONF/ldap.CFG, check ca user key on $AUTHSERV "
exit 204

fi

so when the key has been inputted correctly I want my script to run the CheckSsh funciton and wait for the user to run the touch command:

CheckSsh {

while [[ count -lt 60 ]]
do

if [[ -f /tmp/ldap_ssh_test ]] ; then
break

else

print ".\c"
sleep 5

fi

done

}

so the print ".\c" will print ...... every five seconds to show this is waiting.

I could also put in a sum to work out how much time the user has left.

my question is does anyone have any better syntax or ideas to display that the script is waiting?

thanks

chris.
hello
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: wait output in a script

Hi Chris:

Your technique is fine. A nice polish to your own suggestion is to have your script spawn your CheckSsh() function as a background process.

Then, capture the PID of that process:

# PID=$!

Now, let the main part of your script do other work if/as necessary. If at any time you want to cancel the CheckSSh() wait, simply issue do:

# kill ${PID} in the script that called the function in the first place.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: wait output in a script

Hi (again) Chris:

Here's a prototype demonstration.

# cat ./mywaiter
#!/usr/bin/sh
typeset PID
function spout
{
while true
do
echo ".\c"
kill -s 0 ${PID} > /dev/null 2>&1
[ $? != 0 ] && return 0 || sleep 1
done
}
function grind
{
typeset -i N=0
while (( N < 30 ))
do
(( N=${N}+1 ))
echo "pass_${N}" >> /tmp/sh.$$
sleep 1
done
}
grind &
PID=$!
echo "pid=$$/${PID} executing -- please wait\c"
spout
echo "ok"
exit 0

# ./mywaiter
pid=10614/10615 executing -- please wait..............................ok
#

# head -3 /tmp/sh.10614
pass_1
pass_2
pass_3


Regards!

...JRF...