• Shell编程(七)函数


    1. 函数开始

    #!/bin/bash
    
    foo() 
    {
        echo "Function foo is called";
    }
    
    echo "-=start=-"
    
    foo
    
    echo "-=end=-"

    2. 带参数

    #!/bin/bash
    
    fun()
    {
        echo "hello"
        echo $0
        echo $1
        echo $2
        echo "Hello"
    }
    
    echo "--start--"
    fun aa bb 11
    echo "--end--"

    #!/bin/bash
    
    is_director()
    {
        DIR_NAME=$1
        if [ ! -d $DIR_NAME ]; then
            return 1
        else
            return 0
        fi
    }
    
    for DIR in "$@"; do
        if is_director "$DIR"
        then :
        else
            echo "$DIR doesn't exist. Creating is now..."
            mkdir $DIR > /dev/null 2>&1    # 运行失败打印,标准输出到/dev/null,标准出错指向1,1指向/dev/null
            if [ $? -ne 0 ]; then      # if !0 -> wrong
                echo "Cannot create directory $DIR"
                exit 1
            fi  
        fi  
    done

    注意:

    mkdir /aaa > /dev/null 2>&1

  • 相关阅读:
    dp
    数学分析 + 容斥原理
    容斥
    并查集
    矩阵hash + KMP
    扫描线
    位运算
    2015 Multi-University Training Contest 5 1009 MZL's Border
    iOS ZipArchive文件解压缩
    iOS GCD倒计时
  • 原文地址:https://www.cnblogs.com/douzujun/p/10372177.html
Copyright © 2020-2023  润新知