PDA

View Full Version : Script - loop



satimis
5th November 2004, 01:33 PM
Hi folks,

I have following script as looping. It takes up space.

Script:-
....
# Choosing preset directores for copying
REPLY="B"
until [ -z "$REPLY" ]

do
echo "Choose any of the following:"
echo
echo "[B]123"
echo "[C]123"
echo "[D]123"
echo "[A]ll
echo
echo "[Enter] = Exit"
echo

read

case "$REPLY" in

# Accept upper or lowercase input...
"B" | "b" )
B123
;;

"C" | "c" )
C123
;;

"D" | "d" )
D123
;;

"A" | "a" )
B123
C123
D123
;;

* )
#Default option.
# Do nothing for other keys
;;

esac
done

.....
.....

It has been rearranged in a shorter form to save space as follow;

Script:-

....
....
choices="[B]123 [C]123 [D]123 [A]ll [Enter]=Exit"
echo "Choose one of the following:"
for choice in $choices ; do echo $choice ; done

read reply

case $reply in
[Bb]*) B123 ;;
[Cc]*) C123 ;;
[Dd]*) D123 ;;
[Aa]*) B123 ; C123 ; D123 ;;
*) echo "Exiting..." ; exit ;;
esac
.....
.....

But it is not a loop only running once. Is there any suggestion. TIA

B.R.
satimis

zjimward
5th November 2004, 07:16 PM
Do the following:

reply="B"
until [ -z "$reply" ]
do

choices="[B]123 [C]123 [D]123 [A]ll [Enter]=Exit"
echo "Choose one of the following:"
for choice in $choices ; do echo $choice ; done

read reply

case $reply in
[Bb]*) B123 ;;
[Cc]*) C123 ;;
[Dd]*) D123 ;;
[Aa]*) B123 ; C123 ; D123 ;;
*) echo "Exiting..." ; exit ;;
esac

done


This will exit if you do not select a valid answer. If you want to stay in the loop when a bad response is given then change:

*) echo "Exiting..." ; exit ;;

to:

"") echo "Exiting..." ; exit ;;

Hope this helps.

satimis
6th November 2004, 04:32 AM
Hi zjimward],


This will exit if you do not select a valid answer. If you want to stay in the loop when a bad response is given then change:

*) echo "Exiting..." ; exit ;;

to:

"") echo "Exiting..." ; exit ;;I tried both versions. Script exited finally, instead of continue to next command,

What I expect to do is choosing either 1 or 2 directories rather than either 1 or ALL. Sorry for not explaining clear in my first posting.

B.R.
satimis

zjimward
6th November 2004, 06:29 PM
I'm not real sure I understand exactly what you want to do, but if you want to execute one of the commands in the case...esac and exit then do the following:

reply="B"
until [ -z "$reply" ]
do

choices="[B]123 [C]123 [D]123 [A]ll [Enter]=Exit"
echo "Choose one of the following:"
for choice in $choices ; do echo $choice ; done

read reply

case $reply in
[Bb]*) echo "Bb" ;;
[Cc]*) echo "Cc" ;;
[Dd]*) echo "Dd" ;;
[Aa]*) echo "Aa" ;;
*) echo "Exiting..." ; exit ;;
esac

reply=""

done

You can also re-write the end of the script as follows:

...
until [ -z "$reply" ]
do
...
case $reply in
[Bb]*) echo "Bb" ;;
[Cc]*) echo "Cc" ;;
[Dd]*) echo "Dd" ;;
[Aa]*) echo "Aa" ;;
esac

echo "Exiting..."

break

done

exit

I hope these examples help you.

satimis
8th November 2004, 01:07 PM
Hi zjimward,

Tks for your advice.


You can also re-write the end of the script as follows:

...
until [ -z "$reply" ]
do
...
case $reply in
[Bb]*) echo "Bb" ;;
[Cc]*) echo "Cc" ;;
[Dd]*) echo "Dd" ;;
[Aa]*) echo "Aa" ;;
esac

echo "Exiting..."

break

done

exitJust changing following line using "break" to replace "exit" then it works continue to next command.

*) echo "Exiting..." ; exit ;;
replaced with
*) echo "Exiting..." ; break ;;

B.R.
satimis