Code Sample |
PATTERN="one two three" case $PATTERN in *one*) echo "One matched";; *two*) echo "Two matcched";; *three*) echo "Three matched";; esac |
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 } |
Quote |
Now it only echoes "One matched". I would like it to echo all of those. Any way possible with _one_ case statement? |
Code Sample |
for i in $PATTERN; do case $i in one) echo "One matched";; two) echo "Two matched";; three) echo "Three matched";; esac done |
Quote (mikshaw @ Jan. 29 2008,12:29) | ||
You can accomplish what you need in a loop:
|
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 ) |
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 |
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. |
Quote |
I would also like to replace sed with awk if it can perform search & replace easily, like sed. |