用shell來找最大和最小的數

 假如一個文本文件file是這樣的,裏面全部是數字

 

 

  1. 740     15972   18307   16248   17862   29009   13603   2239    14893   11235   19566   13175   3291    14677   29570   15409   8693    19836   1979    13319   28403 
  2.  
  3. 14432   5202    577     30424   25355   30710   31333   7042    23863   9974    7782    7068    28282   24031   24930   24524   4867    27169   6649    16103   13968 
  4.  
  5. 19824   19394   28645   16627   2036    4570    3696    4016    17889   32100   18448   23091   32678   16104   15678   30620   14669   22721   21716   24644   30504 
  6.  
  7. 28784   20159   21767   20946   11915   26635   15348   18565   9970    29316   5622    29365   25193   22249   31402   29764   25946   2650    14885   25278   21099 

 

編寫一個shell腳本用來找出這個文件中最大數字和最小數字
 
 
  1. #!/bin/bash 
  2. MAX=`for i in $(cat file);do echo $i;done|sort -nr|head -1` 
  3. MIN=`for i in $(cat file);do echo $i;done|sort -nr|tail -1` 
  4. echo "the Max number is : $MAX" 
  5. echo "the Min number is : $MIN" 
 
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章