LinuxShell脚本编程7-for和while

1、for的使用

#! /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++))

==========

统计文件数目

#! /bin/bash
i=0
for name1 in `ls /SYTest`
do
        echo $name1
        i=`expr $i + 1`
done
echo $i

2、while的使用

#!/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

3、>>与>的使用

例子:一个录入雇员信息的shell脚本

#! /bin/bash
while true
do 
        echo "employee info(cContinue,qQuit):"
        read choice
        case $choice in
                c) 
                        echo "enter your name:"
                        read name1
                        echo "enter your age:"
                        read age1
                        echo "name is:"${name1}";age is :"${age1} >>employee.txt
;;
                q)
                        exit
                        ;;
        esac
done

=========

>>与>的区别:

 >>employee.txt:追加保存到employee.txt文件中,如果文件不存在会自动创建
>employee.txt:重新写入,覆盖原有的数据
原文地址:https://www.cnblogs.com/sylovezp/p/4241133.html