先决知识
运算符:
-eq //等于
-ne //不等于
-gt //大于 (greater )
-lt //小于 (less)
-ge //大于等于
-le //小于等于
数学运算
//Linux中求1-100的和
for 语句:
#!/bin/sh
s=0
for ((i=1;i<=100;i++))
do
s=$(($s+$i))
done
echo "1+2+3...+100=$s"
while语句:
#!/bin/sh
s=0
i=1
while test $i -le 100
do
let s=s+i
let i=i+1
done
echo "1+2+3+...+100=$s"
until语句:
#!/bin/sh
s=0
i=1
until test $i -gt 100
do
let s=s+i
let i=i+1
done
echo "1+2+3+...+100=$s"
|
|
//Linux中求n阶乘的和
#!/bin/sh
read n
s=0
for ((i=1;i<=n;i=i+1))
do
t=1
for ((j=1;j<=i;j=j+1))
do
let t=t*j
done
let s=s+t
done
echo "1!+2!+...+$n!=$s"
|
|
//linux中输出九九乘法表
#!/bin/bash
for ((i=1; i<=9; i++)); do
for ((j=1; j<=i; j++)); do
x=$((i * j))
echo -n "$i*$j=$x "
done
echo -e
done
|
|
//linux中用’*'输出直角三角形
#!/bin/sh
for ((i=1;i<=5;i=i+1))
do
for ((j=1;j<=2*i-1;j=j+1))
do
echo -n "*"
done
echo -e
done
|
|
//linux中用’*'输出等腰三角形
#!/bin/sh
for ((i=1;i<=5;i=i+1))
do
for ((t=1;t<=11-i;t=t+1))
do
echo -n " "
done
for ((j=1;j<=2*i-1;j=j+1))
do
echo -n "*"
done
echo -e
done