#!bin/bash
#a Simple Shell Script Example
#a Function
function say_hello()
{
echo -n "Enter Your Name,Please. :"
read name
echo "Hello $name"
}
echo "Program Starts ...."
say_hello
echo "Program Ends."
if条件判断
输出两者中较大的数
1 #! /bin/bash
2
3 read -p "please inout a:" a
4 read -p "please input b:" b
5 if [ $a -gt $b ]
6 then
7 echo "the max is : $a"
8 elif [ $a -lt $b ]
9 then
10 echo "the max is : $b"
11 else
12 echo "$a = $b"
13 fi
14
for循环
输出1~10
1 #! /bin/bash
2
3
4 for ((i=1;i<=10;i++))
5 do
6 echo $i
7 done
1 !/bin/bash
2 for i in `seq 1 20`
3 do
4 echo $i
5 done
计算1+2+3+…+100之和并输出
1 #! /bin/bash
2
3
4 sum=0
5 for ((i=1;i<=100;i++))
6 do
7 let sum+=$i
8 done
9 echo $sum
while循环
输出1~10
1 #! /bin/bash
2
3 i=0
4 while [ $i -lt 10 ]
5 do
6 let i++
7 echo $i
8 done
编写一个shell脚本,输入10个整数,要求输出最大值,最小值以及求
和结果。
1 #!/bin/bash
2
3 max=0
4 min=100000
5 sum=0
6 for i in `seq 10`
7 do
8 read -p "please input a number:" n
9 sum=$(($sum+$n))
10 if [ $n -gt $max ]
11 then
12 max=$n
13 fi
14 if [ $n -lt $min ]
15 then
16 min=$n
17 fi
18 done
19 echo "the max is : $max"
20 echo "the min is : $min"
21 echo "the sum is : $sum"