Shell脚本练习
一、将数组 45,86,28,85, 15,19,56, 8 加起来
#!/bin/bash arr1=(45 86 28 85 15 19 56 8) sum=0 for i in ${arr1[*]}; do sum=$(($i + $sum)) done echo $sum
二、写一个脚本 执行脚本时 传入 几个 数字 ,将传入的数字中最大的那个输出出来
#!/bin/bash arr=($*) max=0 for iw in ${arr[*]} do if [ $iw -gt $max ] then max=$iw fi done echo $iw
三、写一个函数输入3个数,输出最大的数,如果不是3个数,显示输出错误
#!/bin/bash da(){ if [ $# -eq 3 ] then max=$1 [ $2 -gt $max ] && max=$2 [ $3 -gt $max ] && max=$3 echo max is $max elif [ $# -ne 3 ] then echo "wrong" fi } da 33 65 2
四、输出当前时间
#!/bin/bash funj(){ time=$(date "+%Y-%m-%d %H:%M:%S") echo "${time}" } funj ~