linux shell两个文件数值相加

linux shell两个文件数值相加,第1张

#!/bin/bash

n=1

for i in `cat 1.txt`do # 循环1.txt

num=`sed -n "${n}p" 2.txt` # 获取2.txt对行的数字

sum=$(($num + $i)) # 两数相加

echo $sum # 打印结果

n=$(($n + 1))

done

#!/bin/bash

echo

-n

"Enter

the

first

num:"

read

first

echo

-n

"Enter

the

second

num:"

read

second

sum=$(echo

$first+$second|知bc)

echo

"$first+$second=$sum"

测试没问题!touch

一个文件,然道后vi它,直接vi也可以,然后插入,复制回粘贴,保存退出,更改权限,就可以执行了。答

 #!/bin/bash

 sum=0

 if [ $# -ne 2 ]

 then

    echo "Please input two numbers!"

 elif [ $1 -gt $2 ]

 then

    echo "The seconde number must be  great the first number."

 else

     for i in $(seq $1 $2)

     do

       sum=`expr $sum + $i`

     done

     echo "\"$1~$2\" sum is $sum"

 fi

 #执行结果

#[root@localhost opt]# ./b.sh 3 6

#"3~6" sum is 18

#[root@localhost opt]# ./b.sh 3 

#Please input two numbers!

#[root@localhost opt]# ./b.sh 3 5 6

#Please input two numbers!

#[root@localhost opt]# ./b.sh 3 2

#The seconde number must be  great the first number.

#[root@localhost opt]# ./b.sh 3 25

#"3~25" sum is 322


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8997466.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-23
下一篇 2023-04-23

发表评论

登录后才能评论

评论列表(0条)

保存