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:重新写入,覆盖原有的数据
  • 相关阅读:
    最大流EK算法/DINIC算法学习
    hdu-3065-AC自动机
    51nod-1636-dp
    nyoj-1316-二分
    HDU-4510-日期
    HDU-2896-AC自动机
    51nod-1385-贪心-构造
    SpringMVC实现Restful风格的WebService
    SpringMVC使用中遇到的问题总结
    Boostrap(3)
  • 原文地址:https://www.cnblogs.com/sylovezp/p/4241133.html
Copyright © 2011-2022 走看看