| Author |
Message |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Fri 11 Nov 2011, 15:11 Post subject:
A question of "bad substition". |
|
I haven`t quite figured out what Bash likes or doesn`t like and why.
Examples:
| Code: | # 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.
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Sat 12 Nov 2011, 03:41 Post subject:
|
|
you can't "nest" substring manipulations
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
big_bass

Joined: 13 Aug 2007 Posts: 1736
|
Posted: Sat 12 Nov 2011, 10:41 Post subject:
|
|
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
| Quote: |
# 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
| Quote: | # 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: | 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]} |
_________________ slackware 14
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Sat 12 Nov 2011, 14:39 Post subject:
|
|
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.
|
|
Back to top
|
|
 |
big_bass

Joined: 13 Aug 2007 Posts: 1736
|
Posted: Sat 12 Nov 2011, 19:08 Post subject:
|
|
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: | 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: |
s="alphaBETA"
echo ${s^^}
echo ${s,,}
echo ${s^} |
_________________ slackware 14
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Sat 12 Nov 2011, 20:34 Post subject:
|
|
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.
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Sun 13 Nov 2011, 00:26 Post subject:
|
|
There is also guessfstype and blkid(full version or if enabled in busybox - see recent post on Barry's blog)
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Sun 13 Nov 2011, 00:55 Post subject:
|
|
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.
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2678 Location: Kiel,Germany
|
Posted: Sun 13 Nov 2011, 11:44 Post subject:
|
|
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 .
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Sun 13 Nov 2011, 16:52 Post subject:
|
|
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.
|
|
Back to top
|
|
 |
|