Shell编程-数值相加的三种方式

来源:互联网 发布:淘宝客服主管日常管理 编辑:程序博客网 时间:2024/05/21 17:40

此为以前写的代码,整理中,未完待续。

问题

对依次用户输入的4个数值求和

解决

#!/bin/bash# the first progam# read -p "1 num:" num[0]# read -p "2 num:" num[1]# declare -a num[2]=3# echo ${num[*]}# declare -i sum=0sum=0for i in 0 1 2 3    do        read -p "$i num:" -t 30 num[$i]        a=num[$i]      # sum=$[$sum+$a]      # Method one      # sum=$(( $sum+$a ))      # sum=$(($sum+$a))       b=$sum            # Method two           declare -i sum=$a+$b     # Method three          # b=$sum            # sum=$(expr $b + $a)              doneecho $sum

分析

  1. shell 数组表示法
  2. 循环语句
  3. 累积求和
0 0
原创粉丝点击