Programming and Scripting :: Beta testers needed



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. =)

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.

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. =)

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...

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.

Next Page...
original here.