LINUX:编写一个shell脚本,并利用函数实现数列求和运算
答案:4 悬赏:40 手机版
解决时间 2021-01-22 06:11
- 提问者网友:暮烟疏雨之际
- 2021-01-21 21:32
LINUX:编写一个shell脚本,并利用函数实现数列求和运算
最佳答案
- 五星知识达人网友:孤独的牧羊人
- 2021-01-21 22:44
#!/bin/sh
fun()
{
sum=0
for i in `seq $1 $2`
do
sum=`expr $i + $sum`
done
echo $sum
}
echo "Please input 2 number:"
read a b
expr $a + 0 1>/dev/null 2>&1
if [[ $? -ne 0 ]];then
echo "wrong number"
exit 1;
fi
expr $b + 0 1>/dev/null 2>&1
if [[ $? -ne 0 ]];then
echo "wrong number"
exit 1;
fi
if [[ $a -ge $b ]];then
echo "wrong number"
exit 1;
fi
fun $a $b
fun()
{
sum=0
for i in `seq $1 $2`
do
sum=`expr $i + $sum`
done
echo $sum
}
echo "Please input 2 number:"
read a b
expr $a + 0 1>/dev/null 2>&1
if [[ $? -ne 0 ]];then
echo "wrong number"
exit 1;
fi
expr $b + 0 1>/dev/null 2>&1
if [[ $? -ne 0 ]];then
echo "wrong number"
exit 1;
fi
if [[ $a -ge $b ]];then
echo "wrong number"
exit 1;
fi
fun $a $b
全部回答
- 1楼网友:長槍戰八方
- 2021-01-22 00:47
#!/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
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
- 2楼网友:杯酒困英雄
- 2021-01-22 00:00
#!/bin/bash
sum()
{
total=0
for num in `seq $1 $2`
do
let total+=num
done
echo $total
}
read -p "Input number1: " num1
read -p "Input number2: " num2
if [ $num1 -ge $num2 ]; then
echo "Wrong number!"
exit 1
fi
sum $num1 $num2
exit 0
sum()
{
total=0
for num in `seq $1 $2`
do
let total+=num
done
echo $total
}
read -p "Input number1: " num1
read -p "Input number2: " num2
if [ $num1 -ge $num2 ]; then
echo "Wrong number!"
exit 1
fi
sum $num1 $num2
exit 0
- 3楼网友:空山清雨
- 2021-01-21 23:31
#!/bin/sh
#
#
sum(){
a=$1
b=$2
c=$a
while [ $c -le $b ]
do
sum=$(($sum+$c))
c=$(($c+1))
done
echo $sum
}
read -p "Please input the number1:" x
read -p "Please input the number2:" y
if [ $x -ge $y ];then
echo "wrong number!"
else
sum $x $y
fi
exit 0
#
#
sum(){
a=$1
b=$2
c=$a
while [ $c -le $b ]
do
sum=$(($sum+$c))
c=$(($c+1))
done
echo $sum
}
read -p "Please input the number1:" x
read -p "Please input the number2:" y
if [ $x -ge $y ];then
echo "wrong number!"
else
sum $x $y
fi
exit 0
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯