Ignore special characters in loop

Hi All,

select app from the menu:  ABC DEF GHI JKL ALL  # ALL will select all the apps in the menu  echo "Enter your option" read option;  if [ ${option} = "ALL" ] then      <execute the below command> elif [ ${option} = "option" ]     # option is the 1 selection from menu...not ALL     <execute the below command> else    echo wrong option."" fi

question:

  • id user enters A* or AB?...... the scripts executes at
elif

.
i want to avoid special characters in if loop...below is the syntax.....but i want to know is there any other where i can eliminate all Special chars with out using below format.

if [ $option != "*" ] || [ $option != "?" ] || [ $option != "&" ]

Thank you in Advance!

Test for or eliminate? For the second, and the lower/upper case problem, try

read option
sdj$/%�keb()/&eWEqwe
echo $(tr 'a-z' 'A-Z' <<< ${option//[[:punct:]]})
SDJKEBEWEQWE

Again, needs a recent bash for the here string.

Hi Rudic,

sorry, i made a wrong question in above thread....
actual question: the loop should not execute if the user enters special character(s) with the combination of menu..

AB*
DE?
GH#
IJ.

How about:

read input
echo "$input" | grep -q [\#\?\*\.] && exit 1

Hope this helps

EDIT:
Besides and again, why not use a select menu as in other thread suggested, would help to avoid invalid inputs.

Try this (it allows A-Z and a-z only):

#!/usr/bin/ksh

read -r option

if [[ "$option" =~ [^[:alpha:]] ]]; then
 echo "Variable 'option' contains non-alphabetic stuff."
 exit 1
fi

echo "OK"

=~ requires ksh93 though.

Hi Sea/Jh,

Thanks for your response... but both didn't help me. i have an approach... it should allow only alphabets and - ( - "symbol").

i have command too:

echo "abcf*?123-" | sed 's/[^a-Z]*//g'

and

echo "abcf*?123-" | sed 's/[^-]*//g'

...

could you please help me to combine both. sed commands into 1 sed command.

out put required:

abcf-

man regex:

So:

echo "abcf*?123-" | sed 's/[^a-Z-]*//g'
abcf-