Shell 學習(七、循環語句的學習(for和while))

//============================================
#!/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 就會重新寫入, 覆蓋原有的數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章