Operating System - HP-UX
1753882 Members
7450 Online
108809 Solutions
New Discussion юеВ

Accept caps and lowercase in script

 
SOLVED
Go to solution
dev44
Regular Advisor

Accept caps and lowercase in script

Hi,

I have the following:

elif [ $ANS = ALL ]; then
echo "worked"


My script works great if the input is exactly ALL. How do I allow "all" "All" etc...
whatever
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor
Solution

Re: Accept caps and lowercase in script

Hi:

An easy way is to use 'typeset -l' which cause everything to be lower-cased:

...
typeset -l ANS
read ANS
...

Now you need only compare in lowercase.

Regards!

...JRF...
dev44
Regular Advisor

Re: Accept caps and lowercase in script

Hi James...thanks for the response. It didn't recognize it as ALL or all with typeset -l ANS in there. Also, sometimes the ans will be numeric...will typeset -l cause problems with that?
whatever
Mel Burslan
Honored Contributor

Re: Accept caps and lowercase in script

are you sure it is not working ? Did you specify -l switch correctly (that is not a capital i, it is a lowercase L)

also, numeric entries should not matter as they will be interpreted as strings. It will be a problem if you making a numeric comparison in your if [] constructs, but for the code segment you provided, entering numerics is fine
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Accept caps and lowercase in script

Hi (again):

I think you missed my point:

# typeset -l ANS;read ANS;[ "$ANS" = "all" ] && echo "OK" || echo "NOT_OK"

> Also, sometimes the ans will be numeric...will typeset -l cause problems with that?

No, the conversion to all lowercase only affects letters.

Regards!

...JRF...
dev44
Regular Advisor

Re: Accept caps and lowercase in script

#! /usr/bin/ksh

out=/tmp/omni.out
if [ -f $out ]; then
rm $out
fi

ps -ef |grep -i omni |awk '{print $2}' >$out
ps -ef |grep -i omni
echo "Please select the Process ID to kill (second column),"
echo " or type all if you want all omni processes killed. \n"
typeset -l ANS
read ANS

if grep -q $ANS $out; then
echo "This is a valid PID...killing process"
# /usr/local/bin/sudo kill -9 $ANS
elif [ $ANS = ALL ]; then
echo "All omni processes will be terminated"
# for i in `cat $out`; do
# /usr/local/bin/sudo kill -9 $i
# done
else
echo "This PID is not valid. Run the script again and"
echo "select your PID from the second column, or type ALL to kill all omni processes."
fi
rm $out


Output:

Please select the Process ID to kill (second column),
or type all if you want all omni processes killed.

all ->input by me
This PID is not valid. Run the script again and
select your PID from the second column, or type ALL to kill all omni processes.

I also typed ALL and it didn't like it either. When typeset -l isn't there I can type ALL and it works....
whatever
dev44
Regular Advisor

Re: Accept caps and lowercase in script

I think I found it....I have

[$ANS = ALL] and should be [ $ANS = all ]

Thanks...
whatever
James R. Ferguson
Acclaimed Contributor

Re: Accept caps and lowercase in script

Hi:

By using the 'typeset -l' _every_ letter is translated to lowercase in the variable typeset!

Your test:

[ $ANS = ALL ]

...will never evaluate true. You want to make it:

[ $ANS = all ]

After all (no pun) this allows you to say "ALL", "All", "aLL" or "aLl" any way you choose!

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: Accept caps and lowercase in script

Shalom,

Seems like its time to actually test your script, versus asking us to evaluate it.


Resources
http://www.unix.com/shell-programming-scripting/31743-accepting-upper-lower-case.html

http://www.daniweb.com/forums/thread136257.html

Converting input:
http://www.cyberciti.biz/faq/linux-unix-shell-programming-converting-lowercase-uppercase/

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Fredrik.eriksson
Valued Contributor

Re: Accept caps and lowercase in script

You can also use case for this (atleast in bash).

case $ANS in
case ALL|all)
Commands goes here
;;
case [Aa][Ll][Ll])
This method should work too, commands goes here
;;
case *)
if it didn't match, commands goes here
;;
esac

Best regards
Fredrik eriksson