while和for循环
while循环
手动输入一个值,求0到该值的累加和
#!/bin/bash
#test9.sh
read -p "input a number,we will sum 0 to this number:" number
total=0
while [ $number -ge 0 ]
do
echo $number
total=$(($total+$number))
number=$(($number-1))
sleep 1
done
echo $total
for循环
把系统中所有的用户输出,并重定向与user.txt中
#!/bin/bash
#test10.sh
for i in `cat /etc/passwd|awk -F: '{print $1}'`
do
echo $i
echo $i>>user.txt
done
输入一个值,判断是什么按键类型
#!/bin/bash
#test10.sh
read -p "please input a key:" key
case $key in
[a-z]|[A-Z])
echo '字母键!!!'
;;
[0-9])
echo '数字键!!!'
;;
*)
echo '其他功能键!!!'
;;
esac
function函数
#!/bin/bash
#test11.sh
function add(){
num=$1
tot=0
for((i=1;i<$num;i++))
do
tot=$(($tot+$i))
done
echo $tot
}
add 10