Bash

资源1:http://tldp.org/LDP/abs/html/index.html

资源2:

1. 函数

Shell 函数定义

function_name()
{
    list of commands
    [return value]        
}
或者
function function_name()
{
    list of commands
    [return value]        
}
  • 函数的返回值,可以显示的增加retrun;如果不加,会将最后一条命令运行结果作为返回值。
  • 函数的返回值只能是整数,一般用来表示执行成功与否,0表示成功,其他表示失败。
  • 函数调用时不需要加括号。
  • 函数的返回值在调用函数后通过$?来获得。(***如下例子****)
    #!/bin/bash
    funWithReturn(){
        echo "The function is to get the sum of two numbers..."
        echo -n "Input first number: "
        read aNum
        echo -n "Input another number: "
        read anotherNum
        echo "The two numbers are $aNum and $anotherNum !"
        return $(($aNum+$anotherNum))
    }
    funWithReturn
    # Capture value returnd by last command
    ret=$?
    echo "The sum of two numbers is $ret !"

2. exit 用来退出脚本,也可以带退出状态。 如exit 5.

3.Shell判断字符串包含关系的几种方法

参考:http://www.jianshu.com/p/ce3f23f80cad

其中建议使用以下这种:利用字符串运算符

strA="helloworld"
strB="low"
if [[ $strA =~ $strB ]]
then
    echo "包含"
else
    echo "不包含"
fi

利用字符串运算符 =~ 直接判断strA是否包含strB。(这不是比第一个方法还要简洁吗摔!)

4. Shell 命令常见用法

命令常见用法说明
command & 在子 shell 的后台 中执行 command
command1 | command2 通过管道将 command1 的标准输出作为 command2 的标准输入(并行执行)
command1 2>&1 | command2 通过管道将 command1 的标准输出和标准错误作为 command2 的标准输入(并行执行)
command1 ; command2 按顺序执行 command1 和 command2
command1 && command2 执行 command1;如果成功,按顺序执行 command2(如果 command1 和 command2 都执行成功了,返回 success )
command1 || command2 执行 command1;如果不成功,按顺序执行 command2(如果 command1 或 command2 执行成功,返回 success )
command > foo 将 command 的标准输出重定向到文件 foo(覆盖)
command 2> foo 将 command 的标准错误重定向到文件 foo(覆盖)
command >> foo 将 command 的标准输出重定向到文件 foo(附加)
command 2>> foo 将 command 的标准错误重定向到文件 foo(附加)
command > foo 2>&1 将 command 的标准输出和标准错误重定向到文件 foo
command < foo 将 command 的标准输入重定向到文件 foo
command << delimiter 将 command 的标准输入重定向到下面的命令行,直到遇到“delimiter”(here document)
command <<- delimiter 将 command 的标准输入重定向到下面的命令行,直到遇到“delimiter”(here document,命令行中开头的制表符会被忽略)


 

原文地址:https://www.cnblogs.com/rexhu/p/5531169.html