zoukankan      html  css  js  c++  java
  • shell编程基础二

    一、流程控制

    while循环:只要条件满足一直循环

    read -p "请输入一个数字:" white_data
    while  [ ${white_data} -lt 20 ]
    do
        echo ${white_data}
        white_data=$((white_data + 1))
    done

    until循环:只要条件不满足一直循环

    until [ ${white_data} -qt 20 ]
    do
        echo ${white_data}
    done

    case:类似于Java中的switch case

    case $2 in
        +)
             echo "$1 + $3 = $(($1 + $3))"
                ;;
        -)
             echo "$1 - $3 = $(($1 - $3))"
                ;;
        *)
             echo "$1 * $3 = $(($1 * $3))"
                ;;
        /)
             echo "$1 / $3 = $(($1 / $3))"
                ;;
        *)
        # 如果都不满足 执行*)下的语句
            echo "$2 not "
    esac

    case 后面为值, in后面 )前面为判断条件,如果符合会执行下面得命令,上方脚本实现的是基本的算术运算,当所有条件不满足时执行 *)下的命令。

    二、获取命令行参数

    echo "第一个参数为$1"
    echo "第二个参数为$2"
    echo "shell脚本名为 $0"
    echo "shell脚本的参数个数为 $#"
    echo "shell脚本的所有参数为 $*"

    三、定义函数

    方法1:

    function hello()
    {
        echo "hello"
    }
    hello

    hello为方法名,在{}中写命令,使用hello调用函数

    方法2:

    greet()
    {
        echo "greet, ${LOGNAME}, today is $(date)"
    }
    greet
    

    greet为方法名

    带参数的方法:

    # 带参数的方法
    read -p "请输入姓名: " name
    # 与input相同功能
    read -p "请输入年龄: " age
    function info
    {
        echo -e "姓名为$1,
     年龄为$2"
    }
    info
    或者 info zhangsan 20 在调用方法时传参
  • 相关阅读:
    抽象类使用细节
    super关键字
    JDK,JRE,JVM三者之间的爱恨情仇
    LinkedHashSet
    HashSet扩容成红黑树机制
    Set之HashSet
    finally关键字
    Hashcode方法
    equals方法和==的区别
    LinkedList
  • 原文地址:https://www.cnblogs.com/congyiwei/p/14454287.html
Copyright © 2011-2022 走看看