Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting the value of shell variable to uppercase 5

Status
Not open for further replies.

mikrom

Programmer
Mar 27, 2002
2,997
SK
I need to convert the value of shell variable to uppercase and till now I have done this:
cmd_upper.sh
Code:
[COLOR=#0000ff]#!/usr/bin/qsh[/color]
[COLOR=#0000ff]# convert command line arg to uppercase[/color]

[COLOR=#804040][b]echo[/b][/color][COLOR=#ff00ff] [/color][COLOR=#804040][b]"[/b][/color][COLOR=#ff00ff]original [/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]cmdarg  = '[/color][COLOR=#a020f0]$1[/color][COLOR=#ff00ff]'[/color][COLOR=#804040][b]"[/b][/color]
[COLOR=#008080]CMDARG[/color]=[COLOR=#6a5acd]`echo [/color][COLOR=#a020f0]$1[/color][COLOR=#6a5acd] [/color][COLOR=#804040][b]|[/b][/color][COLOR=#6a5acd] tr [/color][COLOR=#804040][b]'[/b][/color][COLOR=#ff00ff][:lower:][/color][COLOR=#804040][b]'[/b][/color][COLOR=#6a5acd] [/color][COLOR=#804040][b]'[/b][/color][COLOR=#ff00ff][:upper:][/color][COLOR=#804040][b]'[/b][/color][COLOR=#6a5acd] [/color][COLOR=#804040][b]|[/b][/color][COLOR=#6a5acd] tr -d [/color][COLOR=#804040][b]'[/b][/color][COLOR=#ff00ff]\n[/color][COLOR=#804040][b]'[/b][/color][COLOR=#6a5acd]`[/color]
[COLOR=#804040][b]echo[/b][/color][COLOR=#ff00ff] [/color][COLOR=#804040][b]"[/b][/color][COLOR=#ff00ff]uppercase [/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]CMDARG = '[/color][COLOR=#a020f0]$CMDARG[/color][COLOR=#ff00ff]'[/color][COLOR=#804040][b]"[/b][/color]
It seems to work ...
Code:
> cmd_upper.sh myCommandLineArg          
  original $cmdarg  = 'myCommandLineArg' 
  uppercase $CMDARG = 'MYCOMMANDLINEARG'
[highlight]... but isn't there a simpler or nicer way how to do it ?[/highlight]
 
in ksh you can use this (according to what I read on iSeries infocenter, qsh has typeset also - synonym of the declare builtin in qsh):

Code:
typeset -u CMDARG
CMDARG=$1
echo orig is $1
echo ucase is $CMDARG

HTH,

p5wizard
 
I tried your recipe in qshell on iSeries and it works fine.
Code:
> /home/MIKROM/p5wizard.sh myCommandLineArg 
  orig is myCommandLineArg                  
  ucase is MYCOMMANDLINEARG
Thank you very much !
 
So what does that do - qshell on iSeries? Is that a unix-like shell under OS/400? Just curious.

HTH,

p5wizard
 
I thought that Qshell is an IBM clone of bash, but what you suggested doesn't probably work in bash - but I'm not sure, because I have only MSYS and in MSYS it doesn't work.
Qshell is an UNIX like environment in?ide of iSeries (something similar like omvs on mainfarame) and it works internal with EBCDIC.
Other unix like environment (better said AIX like environment) is PASE shell, which works with ASCII.
If you want to try it, then create account on to the free iseries at , log on and start the Qshell with the command STRQSH or the PASE shell with call QP2TERM.
 
Ok, I'll have a go at it. And you're probably right about bash. It doesn't know about typeset -u (throws an error on my Mac OS X 10.6 as well).

HTH,

p5wizard
 
p5wizard, in bash use declare instead of typeset:
declare -u CMDARG=$1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, bash on Mac OS 1.6 says no:

Code:
$ declare -u var
-bash: declare: -u: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

$ set|grep BASH_VERS
BASH_VERSINFO=([0]="3" [1]="2" [2]="48" [3]="1" [4]="release" [5]="x86_64-apple-darwin10.0")
BASH_VERSION='3.2.48(1)-release'

I'll try on an AIX box and some LINUX boxes next week.

HTH,

p5wizard
 
PHV,
i tried what you suggested in qshell on iSeries and it works !
 
Hi

p5wizard, PHV's suggestion works for me :
Code:
[blue]master #[/blue] declare -u var=myCommandLineArg

[blue]master #[/blue] echo $var
MYCOMMANDLINEARG

[blue]master #[/blue] echo $BASH_VERSION
4.1.9(1)-release
[tt][small][red][ignore][off-topic][/ignore][/red][/small][/tt]
But on Bash 4 personally I prefer parameter expansion :
Code:
[blue]master #[/blue] var=myCommandLineArg

[blue]master #[/blue] echo ${var^^}
MYCOMMANDLINEARG
[tt][small][red][ignore][/off-topic][/ignore][/red][/small][/tt]


Feherke.
 
typeset works on qshell also because it is implemented as an alias for declare (look at iSeries infocenter)

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top