Non-BASH lower-to-UPPER case conversion of a-f

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

Non-BASH lower-to-UPPER case conversion of a-f

#1 Post by MinHundHettePerro »

Hello :)!

Is there a simple way to express the bashishm (at least, I think it's a bashism, correct me if I'm wrong)

Code: Select all

echo "${WHATEVER^^}"
in a non-bashy way?

(Reason for asking: Reading binary files through hexdump and echoing to bc, calls for a lower-to-UPPER case conversion of a-f.)

Cheers :)/ MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#2 Post by seaside »

MinHundHettePerro,

Here's a snippet from somewhere.

uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz

Code: Select all

uc(){ #usage: uc "some string" -> "SOME STRING"
i=0
while ([ $i -lt ${#1} ]) do
    CUR=${1:$i:1}
    case $lowers in
        *$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
        *)OUTPUT="${OUTPUT}$CUR";;
    esac
    i=$((i+1))
done
echo "${OUTPUT}"
}
cheers,
s

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#3 Post by amigo »

'echo "${WHATEVER^^}"'
Where did you get that? I've never seen such syntax and it does not work here at all.

'echo $WHATEVER | tr [a-f] [A-F]'
does what you want.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

@amigo - ^^ another undocumented bashism

I was trying to rewrite ndiswrapper in shell and came up with these

this one is to be used in case statements, but haven't really tested it yet

Code: Select all

anycase(){ #make a string usable by case as case insensitive
#input hello world output [hH][eE][lL][lL][oO]" "[wW][oO][rR][lL][dD]
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
i=0
while ([ $i -lt ${#1} ]) do
	CUR=${1:$i:1}
	case "$uppers" in
		*$CUR*)CUR="${uppers%$CUR*}";OUTPUT="${OUTPUT}[${lowers:${#CUR}:1}${uppers:${#CUR}:1}]";;
		*)case "$lowers" in
			*$CUR*)CUR="${lowers%$CUR*}";OUTPUT="${OUTPUT}[${lowers:${#CUR}:1}${uppers:${#CUR}:1}]";;
			*)OUTPUT="${OUTPUT}\"$CUR\"";;
		esac
	esac
	i=$((i+1))
done
printf "${OUTPUT}"
}

Code: Select all

lc(){ #convert string to lower case and print it
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
i=0
while ([ $i -lt ${#1} ]) do
	CUR=${1:$i:1}
	case $uppers in
		*$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
		*)OUTPUT="${OUTPUT}$CUR";;
	esac
	i=$((i+1))
done
printf "${OUTPUT}"
}
which really just does this:

Code: Select all

tolower() { #change input $@ to lower case
echo "$@" | tr [:upper:] [:lower:]
}
for upper case just do the reverse

Edit: btw, the reason why I started the anycase function, is because of the case insensitivity in Windows, ... otherwise you would need to convert every single line to lower case for comparison (which could take a while in large files) ... its faster in that case to have one case insensitive case statement
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#5 Post by Karl Godt »

bash version 3 does not work with ^ on my side .

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#6 Post by amigo »

Ah Karl, I am using bash3 also -that explains it.

User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

#7 Post by MinHundHettePerro »

I was in slacko

Code: Select all

# bash --version
GNU bash, version 4.1.0(1)-release (i486-t2-linux-gnu)
Good thing I asked, as the ^-syntax wouldn't have been too portable ...

Anyway, thanks for the tr reminder ... :)

Before, I've used constructs like

Code: Select all

# WHATEVER="0123456789abCDef"
# A="${WHATEVER//a/A}";A="${A//b/B}";A="${A//c/C}";A="${A//d/D}";A="${A//e/E}";A="${A//f/F}" ;echo "${A}"
0123456789ABCDEF
# 
and just fell for the simplicity of ^^ .....

... I'd better avoid these bleeding-edge bashisms ...

Cheers :)/ MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

Re: Non-BASH lower-to-UPPER case conversion of a-f

#8 Post by GustavoYz »

MinHundHettePerro wrote:...non-bashy way?
If you're passing strings, this Perl way might work:

Code: Select all

echo SOMETHING | perl -nle 'print lc'

Post Reply