Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (6) </ 1 2 [3] 4 5 6 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: Beta testers needed, bash script testing< Next Oldest | Next Newest >
Zucca Offline





Group: Members
Posts: 524
Joined: Feb. 2006
Posted: Jan. 29 2008,13:54 QUOTE

Some improvements

I'm currently replacing as many if+grep combinations as possible with case statement, but is there a way to make case go trough _every_ <pattern>) <code to execute> that meets the criteria?
Here's an example:
Code Sample
PATTERN="one two three"
case $PATTERN in
*one*)
 echo "One matched";;
*two*)
 echo "Two matcched";;
*three*)
 echo "Three matched";;
esac

Now it only echoes "One matched". I would like it to echo all of those. Any way possible with _one_ case statement?

And I impoved calculation a bit with a nice function:
Code Sample
   # calculator function
   function calc {
       
       # First test if $CALCEXE contains any information if not then set a
       # value. Is this a good (fast/efficient) way to do it?
       test -n "$CALCEXE" || CALCEXE=`which perl` || CALCEXE=`which bc` || CALCEXE="FAIL"
       
       case $CALCEXE in
       *perl)
           $CALCEXE -e "printf '%.${DECIMALS}f',${@}"
       ;;
       *bc)
           echo -e "scale=$DECIMALS\n${@}" | $CALCEXE
       ;;
       FAIL)
           echo -n "Could not calculate: excutable missing"
           exit 1
       ;;
       esac
   }

I think it's quite lightweight. I would like to add awk also there if it can calculate same way as perl and bc. I read somewhere that awk is more common on *NIX systems than perl or bc.
Only basic calculation operators are needed: + - * and /, plus decimal support. I would also like to replace sed with awk if it can perform search & replace easily, like sed.
So anyone who knows awk, please tell. =)


--------------
Do you have it? - http://dy.fi/mak
Back to top
Profile PM WEB ICQ MSN 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 29 2008,15:29 QUOTE

Quote
Now it only echoes "One matched". I would like it to echo all of those. Any way possible with _one_ case statement?
Case doesn't work that way. It compares the string to each option in order, and stops when it finds a match.

You can accomplish what you need in a loop:
Code Sample
for i in $PATTERN; do
case $i in
one) echo "One matched";;
two) echo "Two matched";;
three) echo "Three matched";;
esac
done
This may not be the most efficient way, but it should work.


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
Zucca Offline





Group: Members
Posts: 524
Joined: Feb. 2006
Posted: Jan. 29 2008,16:47 QUOTE

Quote (mikshaw @ Jan. 29 2008,12:29)
You can accomplish what you need in a loop:
Code Sample
for i in $PATTERN; do
case $i in
one) echo "One matched";;
two) echo "Two matched";;
three) echo "Three matched";;
esac
done
This may not be the most efficient way, but it should work.

Well I think many case-sentences in a row might be more efficient.
But I could test it with time command. =)


--------------
Do you have it? - http://dy.fi/mak
Back to top
Profile PM WEB ICQ MSN 
Zucca Offline





Group: Members
Posts: 524
Joined: Feb. 2006
Posted: Jan. 29 2008,17:17 QUOTE

Results

I tested with this bash code:
Code Sample
echo "With for -loop"
time for i in $@
do
 case $i in
   1) echo 1;;
   2) echo 2;;
   3) echo 3;;
   4) echo 4;;
   5) echo 5;;
   6) echo 6;;
   7) echo 7;;
   8) echo 8;;
   9) echo 9;;
   ten) echo ten;;
 esac
done

echo "Cases in a row"
time (
case $@ in
 *1*) echo 1;;
esac
case $@ in
 *2*) echo 2;;
esac
case $@ in
 *3*) echo 3;;
esac
case $@ in
 *4*) echo 4;;
esac
case $@ in
 *5*) echo 5;;
esac
case $@ in
 *6*) echo 6;;
esac
case $@ in
 *7*) echo 7;;
esac
case $@ in
 *8*) echo 8;;
esac
case $@ in
 *9*) echo 9;;
esac
case $@ in
 *ten*) echo ten;;
esac
)


And here's when I ran it:
Quote
$ sh test.sh 1 2 3 4 5 6 7 8 9 ten
With for -loop
1
2
3
4
5
6
7
8
9
ten

real    0m0.006s
user    0m0.000s
sys     0m0.000s
Cases in a row
1
2
3
4
5
6
7
8
9
ten

real    0m0.007s
user    0m0.000s
sys     0m0.000s


I have to say that I was a bit surprised. =) I ran that many times. Times got a bit different each time, but for loop+case won each time.

I need open several other processes to make CPU more busy to get more accurate results.

Edit: Ok. There was no singnificant difference. Maybe lowering clockspeed will show more...


--------------
Do you have it? - http://dy.fi/mak
Back to top
Profile PM WEB ICQ MSN 
^thehatsrule^ Offline





Group: Members
Posts: 3275
Joined: July 2006
Posted: Jan. 29 2008,17:29 QUOTE

Quote
I have to say that I was a bit surprised. =) I ran that many times. Times got a bit different each time, but for loop+case won each time.
That's not too surprising actually... in your many cases script it continously passes all the arguments and looks through all of them each time, whereas the for loop implementation doesn't have wildcards in the case statement and only compares one parameter at a time.  And having a loop like that seems to be pretty common to me.

Quote
I would also like to replace sed with awk if it can perform search & replace easily, like sed.
I'd stick with sed because it should be a 'lighter' binary (unless you have some reason to worry about having a missing sed)

Also, if you can, avoid using search and replace at all if possible.    For example, looking at your code in your first post, you can make a function that can print the output from variables instead of replacing the text in OUTPUT_FORMAT.
Back to top
Profile PM 
28 replies since Jan. 24 2008,16:16 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (6) </ 1 2 [3] 4 5 6 >/
reply to topic new topic new poll
Quick Reply: Beta testers needed

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code