while和until循环练习

1.用二个以上的数字为参数,显示其中的最大值和最小值
#!/bin/bash

if [[ $# == 0 ]];then
  echo "no arguments"
  exit
fi
declare -i biggest=$1
declare -i smallest=$1
until [[ -z $1 ]];do
  if (( $1 > $biggest ));then
    biggest=$1
  fi
  if (( $1 < $smallest ));then
    smallest=$1
  fi
  shift
done
echo "the biggest integer is $biggest."
echo "the smallest integer is $smallest."
unset biggest
unset smallest
2.随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash

let rnd=$RANDOM%9+1
while true;do
  read -p "guess a integer less than ten:" input
  if (( $input < $rnd ));then
    echo "your input is too small."
  elif (( $input > $rnd ));then
    echo "your input is too big."
  else
    echo "right input."
    break
  fi
done
3.用文件名做为参数,统计所有参数文件的总行数
#!/bin/bash

declare -i sum=0
until [[ -z $1 ]];do
  if [[ -f $1 ]];then
    declare -i rows=$(cat $1 | wc -l)
    let sum+=$rows
    shift
  else
    echo "not a file."
    break
  fi
done
echo "the sum of all files's rows is $sum."
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值