Unix Shell - move to upper case

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2006 > move to upper case





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author move to upper case
Rob Bradford

2006-02-08, 7:49 am

All.

Using /bin/sh (can't alter that - customer requirement) on HP-UX 11i is
there any way to force a comand line parameter into upper case?

The script takes one parameter that should be three characters at max.

Any help would be appreciated.

Rob.


Chris F.A. Johnson

2006-02-08, 7:49 am

On 2006-02-08, Rob Bradford wrote:
> All.
>
> Using /bin/sh (can't alter that - customer requirement) on HP-UX 11i is
> there any way to force a comand line parameter into upper case?
>
> The script takes one parameter that should be three characters at max.


Is /bin/sh on HP-UX a POSIX shell?

If so, then you can do it with:

_upr()
{
_UPR=
case $1 in
a*) _UPR=A ;; b*) _UPR=B ;;
c*) _UPR=C ;; d*) _UPR=D ;;
e*) _UPR=E ;; f*) _UPR=F ;;
g*) _UPR=G ;; h*) _UPR=H ;;
i*) _UPR=I ;; j*) _UPR=J ;;
k*) _UPR=K ;; l*) _UPR=L ;;
m*) _UPR=M ;; n*) _UPR=N ;;
o*) _UPR=O ;; p*) _UPR=P ;;
q*) _UPR=Q ;; r*) _UPR=R ;;
s*) _UPR=S ;; t*) _UPR=T ;;
u*) _UPR=U ;; v*) _UPR=V ;;
w*) _UPR=W ;; x*) _UPR=X ;;
y*) _UPR=Y ;; z*) _UPR=Z ;;
*) _UPR=${1%${1#?}} ;;
esac
}

new=
x=$1
while [ -n "$x" ]
do
_upr "$x"
new=${new}$_UPR
x=${x#?}
done

set -- $new


Otherwise (slower than the above due to the call to an external
program, tr):

set -- `echo "$1" | tr '[a-z]' '[A-Z]'`

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Stephane Chazelas

2006-02-08, 7:49 am

On Wed, 8 Feb 2006 10:44:33 +0000 (UTC), Rob Bradford wrote:
> All.
>
> Using /bin/sh (can't alter that - customer requirement) on HP-UX 11i is
> there any way to force a comand line parameter into upper case?
>
> The script takes one parameter that should be three characters at max.
>
> Any help would be appreciated.

[...]

usage() {
IFS=" " command eval 'printf >&2 "Error: %s\n" "$*"'
printf 'Usage: %s ...\n' "${0##*/}" >&2
exit 1
}

[ "$#" -eq 1 ] || usage "bad number of arguments"
case $1 in
????*) usage "$1 is more than 3 characters long";;
*[[:lower:]]*) usage "$1 contains lower case letters";;
esac

Note that the definition of "character" and of "lowercase
character" will depend on the user's locale.

--
Stephane
Andrew McDermott

2006-02-08, 5:57 pm

"Rob Bradford" <rob.bradford@lineone.net> wrote in
news:dsci2h$gtg$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com:

> All.
>
> Using /bin/sh (can't alter that - customer requirement) on HP-UX
> 11i is there any way to force a comand line parameter into upper
> case?
>
> The script takes one parameter that should be three characters at
> max.


Assuming you want to force the user into typing the argument in upper
case, you could use expr:

ArgLength=`expr "$1" : '[A-Z]*$'`

check $ArgLength (I'd use case) for the values of 1, 2 or 3 for
a legal argument and any other value for an illegal argument.

>
> Any help would be appreciated.
>
> Rob.
>
>


Andrew McDermott
Michael Tosch

2006-02-08, 5:57 pm

Rob Bradford wrote:
> All.
>
> Using /bin/sh (can't alter that - customer requirement) on HP-UX 11i is
> there any way to force a comand line parameter into upper case?
>
> The script takes one parameter that should be three characters at max.
>
> Any help would be appreciated.
>
> Rob.
>
>


In HP-UX 11.23 (and IMHO in any Posix shell)
you can do

typeset -u arg
arg=$1
echo "$arg"

If you need to have the positional parameters:

typeset -u arg
set -f
arg=$@
set -- $arg
while test -n "$1"
do
echo "$1"
shift
done


--
Michael Tosch @ hp : com
Chris F.A. Johnson

2006-02-08, 5:57 pm

On 2006-02-08, Michael Tosch wrote:
> Rob Bradford wrote:
>
> In HP-UX 11.23 (and IMHO in any Posix shell)
> you can do
>
> typeset -u arg


There is no 'typeset' in the POSIX shell specification.

And, AFAIK, only ksh has 'typeset -u'.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Michael Tosch

2006-02-08, 5:57 pm

Chris F.A. Johnson wrote:
> On 2006-02-08, Michael Tosch wrote:
>
> There is no 'typeset' in the POSIX shell specification.
>
> And, AFAIK, only ksh has 'typeset -u'.
>


In HPUX 11.23 it works with /bin/sh
and is described in
man sh-posix

Which starts with

sh-posix: sh, rsh - standard and restricted POSIX.2-conformant
command shells


--
Michael Tosch @ hp : com
Janis Papanagnou

2006-02-08, 5:57 pm

Michael Tosch wrote:
> Chris F.A. Johnson wrote:

Not as far as I know. And in the book of Bolsky and Korn you can
read about portability: "Features of Ksh not in POSIX Shell [...]
Attributes other than readonly and export for variables [...]"
[vbcol=seagreen]

And pdksh.
[vbcol=seagreen]
> In HPUX 11.23 it works with /bin/sh
> and is described in
> man sh-posix


Maybe it is a ksh under a different name? That's not uncommon.

> Which starts with
>
> sh-posix: sh, rsh - standard and restricted POSIX.2-conformant
> command shells


That, the "sh, rsh", used to be the standard description of the
_ksh_ man page.

Janis
Sven Mascheck

2006-02-08, 5:57 pm

Janis Papanagnou wrote:
> Michael Tosch wrote:



Wrong way: A shell aiming at POSIX is not required
to disable its other extensions - and no shell will
do so, although it might be interesting for testing.

In this regard, modern ash variants (Free-, NetBSD, dash,
BusyBox) are most minimalistic, because there's no need
and no interest in extending them like other modern shells.

[vbcol=seagreen]
> Maybe it is a ksh under a different name? That's not uncommon.


The POSIX shell is and was a ksh88 variant on all commercial
unix flavours until now.
--
<http://www.in-ulm.de/~mascheck/vari...ells/#hpux11.23>
Dan Mercer

2006-02-08, 9:06 pm


"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message news:i5upb3-s4b.ln1@teksavvy.com...
: On 2006-02-08, Michael Tosch wrote:
: > Rob Bradford wrote:
: >> All.
: >>
: >> Using /bin/sh (can't alter that - customer requirement) on HP-UX 11i is
: >> there any way to force a comand line parameter into upper case?
: >>
: >> The script takes one parameter that should be three characters at max.
: >>
: >> Any help would be appreciated.
: >>
: >> Rob.
: >>
: >>
: >
: > In HP-UX 11.23 (and IMHO in any Posix shell)
: > you can do
: >
: > typeset -u arg
:
: There is no 'typeset' in the POSIX shell specification.
:
: And, AFAIK, only ksh has 'typeset -u'.

Most vendor POSIX shells are derived from ksh88i. HP's DOES
have typeset -u. This was a choice necessitated on their part
by using ksh for system admin scripts I think starting with
HP-UX 8.0.

Dan Mercer

:
: --
: Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
: Shell Scripting Recipes: | My code in this post, if any,
: A Problem-Solution Approach | is released under the
: 2005, Apress | GNU General Public Licence


Dan Mercer

2006-02-08, 9:06 pm


"Janis Papanagnou" <Janis_Papanagnou@hotmail.com> wrote in message news:dsdkgi$r6d$1@online.de...
: Michael Tosch wrote:
: > Chris F.A. Johnson wrote:
: >> On 2006-02-08, Michael Tosch wrote:
: >>> Rob Bradford wrote:
: >>>
: >>>> All.
: >>>>
: >>>> Using /bin/sh (can't alter that - customer requirement) on HP-UX 11i
: >>>> is there any way to force a comand line parameter into upper case?
: >>>>
: >>>> The script takes one parameter that should be three characters at max.
: >>>>
: >>>> Any help would be appreciated.
: >>>>
: >>>> Rob.
: >>>>
: >>> In HP-UX 11.23 (and IMHO in any Posix shell)
: >>> you can do
: >>>
: >>> typeset -u arg
:
: Not as far as I know. And in the book of Bolsky and Korn you can
: read about portability: "Features of Ksh not in POSIX Shell [...]
: Attributes other than readonly and export for variables [...]"
:
: >> There is no 'typeset' in the POSIX shell specification.
: >>
: >> And, AFAIK, only ksh has 'typeset -u'.
:
: And pdksh.
:
: > In HPUX 11.23 it works with /bin/sh
: > and is described in
: > man sh-posix
:
: Maybe it is a ksh under a different name? That's not uncommon.

No, but it was derived from ksh sources. To the best of my knowledge,
that was a shortcut a lot if not all the major vendors took.

Dan Mercer

:
: > Which starts with
: >
: > sh-posix: sh, rsh - standard and restricted POSIX.2-conformant
: > command shells
:
: That, the "sh, rsh", used to be the standard description of the
: _ksh_ man page.
:
: Janis


Janis Papanagnou

2006-02-08, 9:06 pm

Dan Mercer wrote:
> "Janis Papanagnou" <Janis_Papanagnou@hotmail.com> wrote in message news:dsdkgi$r6d$1@online.de...
> : Michael Tosch wrote:
> :
> : > In HPUX 11.23 it works with /bin/sh
> : > and is described in
> : > man sh-posix
> :
> : Maybe it is a ksh under a different name? That's not uncommon.
>
> No, but it was derived from ksh sources.


I know that. But the vendor specific versions still identify as "KSH
<some strange version number>" - at least that's what I remember from
AIX 3.x/4.x and HP-UX 9/10. How else should we call these vendor adapted
and AT&T source based ksh's other than a "ksh"?

Janis
Dan Mercer

2006-02-09, 2:55 am


"Janis Papanagnou" <Janis_Papanagnou@hotmail.com> wrote in message news:dse8i1$t20$1@online.de...
: Dan Mercer wrote:
: > "Janis Papanagnou" <Janis_Papanagnou@hotmail.com> wrote in message news:dsdkgi$r6d$1@online.de...
: > : Michael Tosch wrote:
: > :
: > : > In HPUX 11.23 it works with /bin/sh
: > : > and is described in
: > : > man sh-posix
: > :
: > : Maybe it is a ksh under a different name? That's not uncommon.
: >
: > No, but it was derived from ksh sources.
:
: I know that. But the vendor specific versions still identify as "KSH
: <some strange version number>" - at least that's what I remember from
: AIX 3.x/4.x and HP-UX 9/10. How else should we call these vendor adapted
: and AT&T source based ksh's other than a "ksh"?

Because, when there is a conflict between ksh88 behavior and posix, the
sh's follow posix. I think the value of $0 in a function might be one
of those.

Dan Mercer
:
: Janis


Stephane CHAZELAS

2006-02-09, 2:55 am

2006-02-08, 21:32(+01), Janis Papanagnou:
[...]
>
> And pdksh.

[...]

and zsh of course, though zsh has neater ways to do case
conversions.

It's neither in bash nor ash based POSIX shells, and is
definitely not POSIX.

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com