2013年4月16日 星期二

UNIX IN NCNU filtering numbers (shell script)


寫一 sh 的 script file, 例如為 ex7.sh
從參數讀進 lower bound 與 upper bound,
從 stdin 讀進 numbers.
把介於 lower bound 與 upper bound 的數以六個一列印出.
如下例所示.
$ cat N 
3 4 5 6 7 8 9 10
9 88 23 
121
98 78 34 288
345 909
3 2 1
$ ./ex7.sh 40 50 < N
$ ./ex7.sh 4 50 < N
     4      5      6      7      8      9 
    10      9     23     34 
$ ./ex7.sh 1 10 < N
     3      4      5      6      7      8 
     9     10      9      3      2      1 
$ ./ex7.sh 11 100 < N
    88     23     98     78     34 

///////////////////////////////////////////////////////////////////////

#!/bin/bash
count=0
lower=$1
upper=$2
while read line;do
  for number in $line;do
        if [ "$number" -le "$upper" ];then
                 if [ "$lower" -le "$number" ];then
                        if [ "$count" -lt 5 ];then
                                echo -n "$number "
                                count=`expr $count + 1`
                        else
                                echo $number
                                count=0
                        fi
                fi
        fi
  done
done
if [ "$count" -gt 0 ];then
echo
fi

沒有留言:

張貼留言