cancel
Showing results for 
Search instead for 
Did you mean: 

bash & typeset / declare

SOLVED
Go to solution
Patrick Wallek
Honored Contributor

bash & typeset / declare

I am just starting to get into some Linux administration with Suse Linux Enterprise Server (SLES) 10. I currently have 1 test machine, but plans for 3 or 4 more this year.

Anyway, I am starting to write some bash shell scripts in SLES. The problem I have is that apparently 'typeset -u' is not supported in bash. I am used to using this in HP-UX (Posix shell) to force a variable to be upper case. This is very handy when getting user input.

Is there some functional equivalent to HP-UX 'typeset -u' in bash?

I'm using an awk hack to force the variable to uppercase, but I'd rather not have that extra overhead.

The version of bash I have is:

$ echo $BASH_VERSION
3.1.17(1)-release

6 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: bash & typeset / declare

Hi Patrick!

It appears that uppercase and lowercase typeset (declare) options don't exist in the Bash shell. You're left with:

# VAR="patrick wallek"
# TOUPPER="$(echo "${VAR}"|tr 'a-z' 'A-Z')"

At least that's probably cheaper than 'awk'.

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: bash & typeset / declare

Jim,

I never think about tr for some reason. You're probably right about it being cheaper than awk.

I was doing a bit of research earlier and I do have ksh on SLES and ksh does have the '-u' available in typeset. That could be an option as well.

Stuart Browne
Honored Contributor

Re: bash & typeset / declare

If you're wanting it for comparison of user-input to a predefined string, you can use:

shopt nocaseglob

This tells the inbuilt 'test' ([) to ignore character case in comparisons.

With the 'ksh' options (there's ksh, and pdksh), be careful. From my experience, they aren't 100% compatable with most other ksh versions.
One long-haired git at your service...
Ivan Ferreira
Honored Contributor

Re: bash & typeset / declare

This will also work:

# TOUPPER="$(echo "${VAR}"|tr [:lower:] [:upper:])"
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Patrick Wallek
Honored Contributor

Re: bash & typeset / declare

I know the "tr" statements will work. For some reason I've got a mental block on 'tr' and always forget about it.

The really handy thing is the 'shopt' bash built-in.

The "nocaseglob" did not work and on further research (the man page) it appears that nocaseglob is for case-insensitive filename matching.

However, "nocasematch" is what I need. It is used when matching patterns in case statements or when doing tests.

shopt -s nocasematch

causes y to equal Y ; yes = Yes = yEs = yeS = YES

which is what I want.

Stuart Browne
Honored Contributor

Re: bash & typeset / declare

Oops, apologies.. Read the man page to grab that one.. Must have done hte whole eye-hand-wrong-line-coordination thing again.. ;)

There's a reason I print documentation out :P

(0 pts)
One long-haired git at your service...