網頁

2016年11月25日 星期五

UNIX入門課程1051_ex5

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業五: shell programming(for, if)
寫一個 shell 程式, 把參數中最大的數印出來.
For example:
$ ./ex5.sh
./ex5.sh numbers
$ ./ex5.sh 8 -1 9
9
./ex5.sh 8 -1 9 11 3 4
11
$


解答:

#!/bin/bash
max=0     #set default Max value
field=$#  #total arg 
  for ((i=1; i<=field; i++))
  do
    if [ $1 -gt $max ]; then
      max=$1
    fi
    shift
  done
echo $max


執行結果: