码迷,mamicode.com
首页 > 系统相关 > 详细

LinuxShell脚本编程7-for和while

时间:2015-01-22 12:51:55      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

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:重新写入,覆盖原有的数据

LinuxShell脚本编程7-for和while

标签:

原文地址:http://www.cnblogs.com/sylovezp/p/4241133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!