A question of "bad substition".

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
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

A question of "bad substition".

#1 Post by sunburnt »

I haven`t quite figured out what Bash likes or doesn`t like and why.

Examples:

Code: Select all

# This works ( notice compound "Len=" statenment ):
Str=123A456 ; First=${Str:0:1} ; Len=$((${#Str}-1)) ; Last=${Str:$Len:1}
echo -e '\n### First letter:  '$First'   \n### Last letter:  '$Last

# And this:
S='1 2 3'
S="${S//[^ ]/}"; echo ${#S}

# But this errors:
S='1 2 3'
echo ${#${S//[^ ]/}}
The string length can go inside a math evaluation.
But a string length can`t have a replacement in it.

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

#2 Post by technosaurus »

you can't "nest" substring manipulations
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].

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#3 Post by big_bass »

Hey sunburnt

I think you missed the idea
I was trying to project in your other thread about awk

this is very easy using arrays
code readabilty is a factor to when you have to keep
track of large files with multiple searches of unique data
# But this errors:
S='1 2 3'
echo ${#${S//[^ ]/}}

your string now has spaces perfect for arrays now



this is what I used as an example in the other thread
# cat a file into an array
arr=(`cat /etc/rc.d/PUPSTATE`)

#get the string length of each array
echo ${#arr[0]}
echo ${#arr[1]}
echo ${#arr[2]}
echo ${#arr[3]}

# get each array
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
echo ${arr[3]}

this is using your data getting the string length and
each variable *note all are ones because its only one character long

Code: Select all

Str="1 2 3"
# cat a file into an array
arr=($Str)

#get the string length of each array
echo ${#arr[0]}
echo ${#arr[1]}
echo ${#arr[2]}
echo ${#arr[3]}

# get each array
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
echo ${arr[3]}

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#4 Post by sunburnt »

big_bass; I knew about filling arrays from a literal string, a variable, and a file.
But I didn`t think about using the Len code in getting the array item`s lengths.

technosaurus; Yeah... And it appears Bash code only works with a variable
in the first position ( hense the "$(" ), but will use literals for the rest.

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#5 Post by big_bass »

I know that you know about arrays
but they are also very useful when doing loops on data
they are very fast this feature is often overlooked
for pure POSIX solutions

bash 4 has even more power with arrays
and you have a complete tool


this is easier

Code: Select all

S=(1 2 3)

# get each array 
echo ${S[0]}
echo ${S[1]}
echo ${S[2]}

#get the string length of each array 
echo ${#S[0]}
echo ${#S[1]}
echo ${#S[2]}
==================================
*this one will surprise you with the output*
==================================

Code: Select all


s="alphaBETA"
echo ${s^^}     
echo ${s,,}     
echo ${s^}    

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#6 Post by sunburnt »

Yep, I like Bash`s ability to work equally well with variables, arrays, and files.
Some of the exec. files insist on using files though. Very old school...

Arrays do a great job of tracking lines by number (index).
But they`re the same as a variable list when doing string searches.
Which is most of the time, only rarely am I forced to use line numbers.

A web page about Bash4`s new features showed associative arrays, very good!
Great for translating characters and words, and very fast at it also.


So far "disktype" is the only thing that gives an unmounted partition`s format.
And parsing it is string search territory, I wish exec commands gave raw data.
But that`s the purpose of "sysinfo". I still need to add file info to it.

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

#7 Post by technosaurus »

There is also guessfstype and blkid(full version or if enabled in busybox - see recent post on Barry's blog)
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
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post by sunburnt »

Thanks technosaurus; I`d tried blkid long ago, and it`s slower than disktype.
I`ve never heard of guessfstype, but Puppy doesn`t have it, and I can`t find it.

Fortunately the function "partinfo" is for human eyes, so slowness is okay.
I`ll just leave the function library code the way it is.

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

#9 Post by Karl Godt »

guessfstype
should be a very important binary in Puppy ; BUT it might be named

~ # guess_fstype /dev/hda1
ext2

The only disadvantage of guess_fstype might be , that it seems not to work on 259er Major blkdrives kernels .

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#10 Post by sunburnt »

Karl Godt; You`re right, it is called: guess_fstype .

I`ll take a look at using it as it`s fairly fast.

Thanks... Terry B.

Post Reply