Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 24 May 2013, 00:13
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Non-BASH lower-to-UPPER case conversion of a-f
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [8 Posts]  
Author Message
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 831
Location: SE

PostPosted: Sun 29 Jan 2012, 21:15    Post subject:  Non-BASH lower-to-UPPER case conversion of a-f  

Hello Smile!

Is there a simple way to express the bashishm (at least, I think it's a bashism, correct me if I'm wrong)
Code:
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 Smile/ MHHP

_________________
Celeron 2.8 GHz, 1 GiB RAM, i82845 graphics, many partitions, Pupmode 12 (13)
Mostly running Slacko & 214X
Nämen, vaf.... ln -s /dev/null MHHP
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 836

PostPosted: Sun 29 Jan 2012, 21:45    Post subject:  

MinHundHettePerro,

Here's a snippet from somewhere.

uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz

Code:
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
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 1759

PostPosted: Mon 30 Jan 2012, 02:04    Post subject:  

'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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 3843

PostPosted: Mon 30 Jan 2012, 03:13    Post subject:  

@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:
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:
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:
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

_________________
Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 2677
Location: Kiel,Germany

PostPosted: Mon 30 Jan 2012, 11:01    Post subject:  

bash version 3 does not work with ^ on my side .
Back to top
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 1759

PostPosted: Mon 30 Jan 2012, 12:34    Post subject:  

Ah Karl, I am using bash3 also -that explains it.
Back to top
View user's profile Send private message 
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 831
Location: SE

PostPosted: Mon 30 Jan 2012, 15:53    Post subject:  

I was in slacko
Code:
# 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 ... Smile

Before, I've used constructs like
Code:
# 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 Smile/ MHHP

_________________
Celeron 2.8 GHz, 1 GiB RAM, i82845 graphics, many partitions, Pupmode 12 (13)
Mostly running Slacko & 214X
Nämen, vaf.... ln -s /dev/null MHHP
Back to top
View user's profile Send private message 
GustavoYz


Joined: 07 Jul 2010
Posts: 867
Location: .ar

PostPosted: Mon 30 Jan 2012, 16:19    Post subject: Re: Non-BASH lower-to-UPPER case conversion of a-f  

MinHundHettePerro wrote:
...non-bashy way?

If you're passing strings, this Perl way might work:
Code:
echo SOMETHING | perl -nle 'print lc'

_________________

Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [8 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0606s ][ Queries: 12 (0.0088s) ][ GZIP on ]