几个简单的shell编程

一、输出1到100之间的整数之和

sum=0
i=1
for i in {1..100}
do
    sum=$((sum+i))
done
echo "The sum of integers from 1 and 100 is:$sum"

几个简单的shell编程_第1张图片

sum=0
for ((i=0;i<=100;i++))
do
    sum=$[$sum+$i] #sum=$((sum+i))
done
echo "The sum of integers between 1 and 100 is:$sum"

几个简单的shell编程_第2张图片

二、输出1到100之间的偶数之和

sum=0
for ((i=2;i<=100;i+=2))
do
    sum=$((sum+i))
done
echo "The sum of even numbers between 1 and 100 is:$sum"

几个简单的shell编程_第3张图片

sum=0
for i in {2..100..2}
do
    sum=$((sum+i))
done
echo "The sum of odd numbers between 1 and 100:$sum"

几个简单的shell编程_第4张图片

三、输出1到100之间奇数之和

sum=0
for ((i=1;i<=100;i+=2))
do
    sum=$((sum+i))
done
echo "The sum of odd numbers between 1 and 100 is:$sum"

几个简单的shell编程_第5张图片

sum=0
for i in {1..100..2}
do
    sum=$((sum+i))
done
echo "The sum of even numbers between 1 and 100:$sum"

几个简单的shell编程_第6张图片

四、输出1到100之间的素数

echo "1到100之间的素数为:"
for ((i=2; i<=100; i++))
do
    flag=0
    for ((j=2; j> sushu.txt
    fi
done

几个简单的shell编程_第7张图片

几个简单的shell编程_第8张图片

你可能感兴趣的:(Linux,shell)