1830245 Members
1855 Online
110000 Solutions
New Discussion

Looping a ksh script

 
SOLVED
Go to solution
Christopher Hildrum_1
Frequent Advisor

Looping a ksh script

I know this will be easy for some of you ...

I have created a login termination script. I want to continue looping this script until I type q on the command line. I need to run it manually (policy) and check for login/ids in my script.

How can I create a basic loop to start and return to the beginning after completing each process.

More explaination:

while (I input something)
run my script
go back to top until I input "q".

Thanks in advance,

Chris
13 REPLIES 13
Hai Nguyen_1
Honored Contributor

Re: Looping a ksh script

Chris,

try this:

while true;
do
your commands here;
done;

Use Control+C to stop the loop.

Hai
Marco Santerre
Honored Contributor

Re: Looping a ksh script

Why don't you do something like this :

while [ 1 ]
do

run script
echo "Quit (q) ? "
read key_stroke

if [ "$key_stroke" = "q" ]
then break
fi
done
Cooperation is doing with a smile what you have to do anyhow.
James R. Ferguson
Acclaimed Contributor

Re: Looping a ksh script

Hi CHris:

Here's one way:

#!/usr/bin/sh
typeset -l REPLY
while [ "${REPLY}" != q ]
do
echo "Choose your poison..."
read REPLY
[ "${REPLY}" = q ] && continue
echo "looping"
done
echo "exiting now"
exit 0

Note the use of the 'typeset' to declare that automatic translation to lowercase characters should be made for any REPLY. This makes' testing for responses short and easy -- no convern for uppercase versus lowercase or mixed case.

Regards!

...JRF...
Michael Steele_2
Honored Contributor
Solution

Re: Looping a ksh script

FILE=/etc/passwd
TEST=103
LOGFILE=$HOME/logfile
>$LOGFILE

cat $FILE | while read a b c d e f g
do
if [[ $d=$TEST ]]
then
break
elseif
continue
fi
done >> $LOGFILE
Support Fatherhood - Stop Family Law
Christopher Hildrum_1
Frequent Advisor

Re: Looping a ksh script

Thanks you all -

I like the different takes on each issue - I will probably use them all in one way or another down the road.

michael - I was looking for the "continue"!.

How simple!

Thanks -
Chris
twang
Honored Contributor

Re: Looping a ksh script

You may take a look at this loop:

while : ;
do
echo "Display Menu"
resp=*
read resp
case $resp in
[M,m])
cmd
;;

[q,Q])
echo "$program: $exit_code"
exit 0
;;

*)
echo Invalid Responce
sleep 1
;;
esac
done
Christopher Hildrum_1
Frequent Advisor

Re: Looping a ksh script

Let me add something else to this one for you:

I am entering a UID and searching a local file that contains all UIDs,etc:

Top part of script:
#########################
print -n 'User ID to lock ?\n'
read USER
if [[ `/software/master/find_uid.sh "$USER:"` = "" ]];then
print "No accounts available"
return
else
continue
fi
#################
How do I return to re-input a new UID if there is no information in my file?


Thanks again !
Chris
Hai Nguyen_1
Honored Contributor

Re: Looping a ksh script

Chris,

"continue" will do it!

Hai
Christopher Hildrum_1
Frequent Advisor

Re: Looping a ksh script

Hai -

continue continues moving through the script - I need to replace the "return" with something that will "start the script over or re-enter the UID.

Chris
Hai Nguyen_1
Honored Contributor

Re: Looping a ksh script

Chris,

I modified your part to become:

while true;
do
print -n 'User ID to lock ?\n'
read USER
if [[ `/software/master/find_uid.sh "$USER:"` = "" ]];then
print "No accounts available"
continue
else
break
fi
done
twang
Honored Contributor

Re: Looping a ksh script

RETURN=""
while [ test $RETURN != "yes" ]
do
echo "Display Menu"
resp=*
read resp
case $resp in
[R,r])
run cmd....
exit_code=$?
;;
[C,c])
RETURN="yes"
;;

[q,Q])
echo "$program: $exit_code"
exit 0
;;

*)
echo Invalid Responce
sleep 1
;;
esac
done

I have revised the above code, it continues if "C" or "c" is entered.
Christopher Hildrum_1
Frequent Advisor

Re: Looping a ksh script

Thanks Hai and all - I think I got it now !

Chris
vasundhara
Frequent Advisor

Re: Looping a ksh script

Hi Chris,

Just an addition.

"continue" is very useful only in loop controls. i.e. when you are using while, for etc... If the statement "continue" is not in a loop, it is equal to not writing the statement itself.

Do not use "continue" frequently in one script as the performance comes down. I read it in some book that in big loops if you are using continue, the internal stack needs to be loaded many times and so the speed comes down. For small loops, you can use it.

Thanks and Regards
VJ.