#!/bin/bash

foo() 
{
    echo "Function foo is called";
}

echo "-=start=-"

foo

echo "-=end=-"

Shell编程(七)函数

2. 带参数

#!/bin/bash

fun()
{
    echo "hello"
    echo $0
    echo $1
    echo $2
    echo "Hello"
}

echo "--start--"
fun aa bb 11
echo "--end--"

Shell编程(七)函数

#!/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

Shell编程(七)函数

注意:

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

Shell编程(七)函数

Shell编程(七)函数

 

相关文章:

  • 2022-12-23
  • 2021-11-20
  • 2018-11-22
  • 2021-04-14
  • 2022-12-23
  • 2021-06-22
  • 2022-02-16
  • 2022-12-23
猜你喜欢
  • 2021-08-25
  • 2021-12-10
  • 2021-12-10
  • 2021-12-11
  • 2021-09-18
相关资源
相似解决方案