zoukankan      html  css  js  c++  java
  • 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:重新写入,覆盖原有的数据
  • 相关阅读:
    正则表达式 \n和\r
    【转】单循环赛赛程安排算法研究
    Iterator效率
    Map获取键值
    PL/SQL语法详解(pdf)
    Iterator模式
    测试js函数的静态页面
    【转】java的一些基本概念
    Oracle 11g用户解锁
    oracle官方文档
  • 原文地址:https://www.cnblogs.com/sylovezp/p/4241133.html
Copyright © 2011-2022 走看看