Case sensitive in If 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: how can i make that input (ALL) is taken as case sensitive.

  • i don't want to write permutation and combination using or gate(||).
  • i don't want to declare one mare variable and translate the input and he new Variable.

if i use ALL,all,All,aLl,alL.........it should consider it as input.
Thank you in Advance!

Use this... for yes and no...

[yY][eE][sS]
[nN][oO]

Hi blackrageous,

Thank you for your reply. i have updated the thread. you are correct, when that is case with yes & no it works.... when the parameter changes as i updated the thread. i will use
[Aa][Ll][Ll] ..... but it will not work as expected.

Thank You!

Try

if [ "${option^^}" = "ALL" ]

Why not acutaly use select ?

select CHOICE in ABC DEF GHI JKL ALL;do
  case $CHOICE in
  ALL)  echo "Do everything" ;;
  ABC)  echo "Do ABC" ;;
  DEF)  echo "Do DEF" ;;
  GHI)  echo "Do GHI" ;;
  JKL)  echo "Do JKL" ;;
  *)    echo "Invalid choice!" ;;
  esac
  break
done

Hope this helps

In a recent bash, you could use

Hi Junior-helper;
tried the code, but didnt help.
error message:

: : bad substitution

Hi Sea,
i cant use select , because 1) Applications vary on servers. 2) in later part if we add one more app... again i need to modify the select code line. if we copy the script to other Machine againi need to modify the select line. just to avoid it... i have used command....

"ls -lrt <Path where app folders will be present> | grep -v total | awk '{print $9}' > $FilePath/FIle.txt"...... now the menu will be displyed from FIle.txt......
cat FIle.txt
ALL

Thank you Jh & Sea!

# Fill list
LIST=$HOME/*

# Select from list
select THAT in $LIST;do
  echo "$THAT"
  break
done

Its not like an if else statement is any diffrent regarding required modifications upon copy paste to another machine from the description you gave.

Furthermore, please use CODE tags around code examples, thank you.

What's your shell?
Is it possible that your shell is *not* bash ? Or rather old bash?
It works for me in bash, but dash will complain about bad substitution.

Hi Rudic,

i tried with below set of combinations:

if [ "${option^^}" = "ALL" ]
if [ "${option^}" = "ALL" ]
if [ "${option,}" = "ALL" ]
if [ "${option,,}" = "ALL" ]

but didn't help....Again same error

 ": : bad substitution"

Thank You!

---------- Post updated at 01:37 PM ---------- Previous update was at 01:34 PM ----------

$ echo $SHELL
/bin/ksh

in ksh - option will be in LOWER case whatever the input is:

typeset -l option
....
if [ "${option}" = 'all' ]

Or the poor-man case conversion...

if [ "$(echo ${option} | tr 'A-Z' 'a-z')" = 'all' ]

Hi vgersh99,

the 2nd one worked. Thank you All for your time & response!!!
But when i used 1st option saw below error:

 typeset: -l: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...

Thanks once again.

what shell interpreter are you using for your script?
Does the first line in the script looks similar to:

#!/bin/ksh

the typeset -l is specific to ksh . Make sure you're specifying what shell interpreter outght to be used in the script.

Hi vgersh99,

yes the script starts with

#!/bin/ksh
 

Hi vgersh99,

The 1st option worked.....but i should use it before i read a Variable

typeset -l option
read option;

i have one more confusion, 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 != "&" ]

Please advice. Thank you!