现在的位置: 首页 > 综合 > 正文

Shell 学习(七、循环语句的学习(for和while))

2014年02月02日 ⁄ 综合 ⁄ 共 1229字 ⁄ 字号 评论关闭

//============================================
#!/bin/bash

for a in `seq 1 2 10`
do
        echo $a
done

a初始值为1, 然后 a=a+2的操作, 一直到 a<=10
---------------------
for((i=1;i<=10;i=i+2))
do
        echo $i
done

for((i=1;i<=10;i++))

[17rumen@localhost ~]$ ./my_07.sh
1
3
5
7
9

//===============================================
统计文件数目
#!/bin/bash

i=0
for name1 in `ls /etc`
do
        echo $name1
        i=`expr $i + 1`
done

echo $i

//==============================================
#!/bin/bash

a=0

while [ $a -le 10 ]
do
        ((a=a+1))

        if [ $a -eq 5 ]
        then
                continue
        elif [ $a -eq 8 ]
        then
                break
        fi

        echo $a
done
-------------------
[17rumen@localhost ~]$ ./my_07.sh
1
2
3
4
6
7

//==========================================
下面是一个录入客户资料的shell脚本

#!/bin/bash

while true
do
        echo "登记客户资料(c继续,q退出):"
        read choice

        case $choice in
                c)
                        echo "请输入客户名字:"
                        read name1
                        echo "请输入客户年龄:"
                        read age1
                        echo "姓名:"${name1}" - 年龄:"${age1} >>customer.txt
                        ;;
                q)
                        exit
                        ;;
        esac
done

--------------------
>> 和 > 区别

>>customer.txt 追加保存到customer.txt文件中, 如果文件不存在会自动创建。

>customer.txt 就会重新写入, 覆盖原有的数据

抱歉!评论已关闭.