1748256 Members
3955 Online
108760 Solutions
New Discussion юеВ

Re: Help script MENU

 
SOLVED
Go to solution
Fernando Jose P de Souz
Regular Advisor

Help script MENU

Hi,

I using select in my script, but i would like to include an exit or quit in a option in menu.

tput clear
export PATH=/usr/sbin:/usr/bin:/usr/contrib/bin:/opt/perl/bin:/opt/ignite/bin:

clear
# Diretorio das imagens
export CAM1=/var/opt/ignite/recovery/archives/PJHPXC01
export IGN_LOG=/home/ignite

echo
echo
echo
echo "Deseja verificar log de alguma imagem (y/n) ? \c"
read OPCAO

if [ "$OPCAO" = "y" -o "$OPCAO" = "Y" ]
then
PS3='Qual das imagens acima deseja ver o log? '
echo
select file in $(ls -tr $IGN_LOG | grep logimage)
do
echo $file
cat $IGN_LOG/$file | more
break
done
exit 0
echo "[ Enter ]"
read
echo
echo "Tecle [Enter] para voltar ao MENU"
read
else
if [ "$OPCAO" = "n" -o "$OPCAO" = "N" ]
then
echo
echo "[ Enter ]"
read
exit
else
echo "Opcao Invalida"
read
exit
fi
fi

clear




WAIT

Fernando.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Help script MENU

Hi:

I dislike the 'select' statement for menus for the problem you have. The 'select' parameter is set to null for non-matches leaving you limited control.

You could handle invalid selections as a "break":

#!/usr/bin/sh
select thing in A B C
do
echo "${thing} seen"
[ -z "${thing}" ] && { echo "BAD THING!"; break; }
done
echo "exiting"

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Help script MENU

Hi (again):

I would suggest a different approach. You appear to be offering a list of files for viewing. You could build and display the list by writing the filenames, one per line, in a temporary file. Then 'cat -n' the temporary file for the user. This shows them a number and a filename.

Read the user's selection; validate that the number selected is in range and 'cat' the filename associated with the number selected as recorded in the temporary file.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: Help script MENU

>JRF: I would suggest a different approach.

Why is this any better than select?
Fernando's select nicely gets the files and adds a number before each. It validates the number and either re-prompts or returns an empty string. (This is what's missing.)

>iI would like to include an exit or quit in a option in menu.

As JRF mentioned, you need to handle the empty string. If you want to add more options, you just fiddle with the pipeline:
... $(ls -tr $IGN_LOG | grep logimage; echo exit quit)

The user won't be able to type "exit" or "quit" but there will be a number for that option.

If you want to see what the the user typed, you can use something like:
select ident in $(ls p* ; echo exit quit); do
echo "Returned: $ident"
if [ "$ident" = "" ]; then
echo "Invalid: user typed: $REPLY"
break
fi
case $ident in
exit) exit ;;
quit) break 2 ;;
*) echo "other" ;;
esac
done
echo "After select"
Fernando Jose P de Souz
Regular Advisor

Re: Help script MENU

Denis, ok.

But when i choose other option, the output:

^ZM-\^A^C^L^B`^C$^B$^CM-T^B^D^BM-x^BM-D^B^T^DM-4^CM-`^D^L^DM-^H^DM-`^CM-8^CP^BH^D`^BM-^L^D8
^Ch^E^T^E^D^C4^CM-^@^CM-^\^Bt^BM-(^B8^^P^F.cshrc_^P^F.login`^.profilea^P^E.exrcb^X^L.profile.oldc
^T
adm_ignited^X^Lcreate_imagee^\^Pverifica_montadof^\^O.verifica_imageg ^S.verifica_image.oldh^.formatai^temp.txtj^X
.create_imagek^P^C^BM-d.swp^\^Q.verifica_montadoq^X^N.verifica_logs^AM-r^X^LPJHPXC01.tar^AM-q^\^Qignite_backup.tar^A
Z^\^Oformat.crontab.^A]^\^Qlogimage.crontab.^AM-.^L^A1^A\,^_logimage.crontab.20080803-02:24^A[,^_logimage.crontab.20080803-02:25
^A`(^]format.crontab.20080803-08:49^Aa(^]format.crontab.20080803-08:50^Ag,^_logimage.crontab.20080804-10:39^A^,^_logimage.crontab.2008
0804-10:40^AM-^U$^X.verifica_logs.05-8-2008^AM-^V^P^Dquit^AM-"^ZM-l^Dexit




My script changed:
tput clear
export PATH=/usr/sbin:/usr/bin:/usr/contrib/bin:/opt/perl/bin:/opt/ignite/bin:

clear
# Diretorio das imagens
export CAM1=/var/opt/ignite/recovery/archives/PJHPXC01
export IGN_LOG=/home/ignite

echo
echo
echo
echo "Deseja verificar log de alguma imagem (y/n) ? \c"
read OPCAO

if [ "$OPCAO" = "y" -o "$OPCAO" = "Y" ]
then
PS3='Qual das imagens acima deseja ver o log? '
file2=`ls -tr $IGN_LOG | grep logimage`
select ident in $(ls -tr $IGN_LOG | grep logimage; echo exit); do
#echo "Returned: `cat $IGN_LOG/$ident 2> /tmp/lixo.txt`" | more
cat $IGN_LOG/$ident 2> /tmp/lixo.txt | more
exit
if [ "$ident" = "" ]; then
echo "Opcao Invalida: $REPLY"
break
fi
case $ident in
exit) exit ;;
*) echo "other" ;;
esac
done
echo "Selecionar outra opcao"
echo "Tecle [Enter] para voltar ao MENU"
read
else
if [ "$OPCAO" = "n" -o "$OPCAO" = "N" ]
then
echo
echo "[ Enter ]"
read
exit
else
echo "Opcao Invalida"
read
exit
fi
fi

clear

James R. Ferguson
Acclaimed Contributor

Re: Help script MENU

Hi (again) Fernando:

As I originally said, "The 'select' parameter is set to null for non-matches leaving you limited control".

Now, Dennis offered a way to add the "quit" prompt by munging with the data in your pipe. Nice touch, Dennis.

But, as I said, non-matches leave you with an empty (undefined) argument. Thus when you do:

# cat $IGN_LOG/$ident 2> /tmp/lixo.txt | more

...and the value of 'ident' is empty, you are really reading the *directory* leading to garbage output!

Thus, at the least, move the check for an empty 'ident' ahead of the 'cat'.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Help script MENU

>JRF: non-matches leave you with an empty (undefined) argument.

Right. As I mentioned, you can also check $REPLY for the actual invalid input.