Operating System - HP-UX
1753447 Members
5104 Online
108794 Solutions
New Discussion юеВ

Re: Aliases for values in Shell Scripts

 
Allasso
New Member

Aliases for values in Shell Scripts

Hello,

Is there such a thing as designating aliases for values in a shell script. for example:

echo "do you want to..."
read variable
case $variable in
y )do_command
;;
n )do_other_command
;;
esac

Now suppose that I want to do_command also if variable equals Y, yes, ok, or "" (return)?

Is there a way to declare all these values equal to each other (especially globally), so I don't have to redundantly list each possibility, or have a long string of "OR"s in an if statement?
7 REPLIES 7
Patrick Wallek
Honored Contributor

Re: Aliases for values in Shell Scripts

You can do this in your case statement. A '|' (pipe symbol) between values means do a 'logical or' (a or b = a|b ).

I would also check the $variable and if it is blank, then set it to yes if that is your default.

if [[ "${variable}" = "" ]] ; then
variable=y
fi

case $variable in
y|Y|yes|Yes|YES|OK|ok) do_command ;;

n|N|No|NO) do_other_command ;;

*) error ;;

esac
A. Clay Stephenson
Acclaimed Contributor

Re: Aliases for values in Shell Scripts

case $variable in
y|Y|yes|Yes|ok|OK) do_command
;;

but you can make your task much easier by forcing the variable to lowercase:

typeset -l lcvar=""
typeset variable=""
read variable
lvcar=${variable) # convert to lowercase
case ${lcvar} in
y|yes|okay|ok) do_command;;
If it ain't broke, I can fix that.
Allasso
New Member

Re: Aliases for values in Shell Scripts

Oh, Thank You both!

There seems to be a LOT of different little nuances in this stuff. There are a lot of pages and tutorials out there, but they each seem to cover real basic stuff, or little nips and tucks here and there.

If you can recommend a good, comprehensive tutorial, especially one covering bash, I would be very happy.
James R. Ferguson
Acclaimed Contributor

Re: Aliases for values in Shell Scripts

Hi:

This isn't exacatly a tutorial, but its an excellent guide:

http://www.gnu.org/software/bash/manual/bash.html

Regards!

...JRF...
Allasso
New Member

Re: Aliases for values in Shell Scripts

thanks, James. I've only scanned it, but it looks good so far.
Sandman!
Honored Contributor

Re: Aliases for values in Shell Scripts

Here's another excellent site for bash scripting:

http://www.tldp.org/LDP/abs/html/
Allasso
New Member

Re: Aliases for values in Shell Scripts

thanks guys, these both are really good and comprehensive.