SHELL開發時常用函數

    版本為 22:18, 20 Nov 2024

    到這個版本。

    返回到 版本存檔.

    查閱目前版本

    一些很實用的函數範例

    函數回傳字串

    https://linuxhint.com/return-string-bash-functions/

    Example-1: Using Global Variable

    function F1()
    {
        retval='I like programming'
    }
    
    retval='I hate programming'
    echo $retval
    F1
    echo $retval
    

    Example-2: Using Function Command

    function F2()
    {
        local  retval='Using BASH Function'
        echo "$retval"
    }
    
    getval=$(F2)   
    echo $getval
    

    Example-3: Using Variable

    function F3()
    {
        local arg1=$1
        
        if [[ $arg1 != "" ]]; 
        then
            retval="BASH function with variable"
        else
            echo "No Argument"
        fi
    }
    
    getval1="Bash Function"
    F3 $getval1
    echo $retval
    getval2=$(F3)
    echo $getval2
    

    is_root

    用途:檢查是否以 root 帳號執行

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    check_os

    用途:檢查 OS 版本

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    Check_Myself

    用途:檢查自身程序是否已經執行中,如果有,程序立即跳離,可避免同一個 script 重複被執行。

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    getopts 指令參數

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    Pause 暫停

    用法: pause

    可用於程式開發及偵錯

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    Error 錯誤訊息

    用途:輸出錯誤訊息

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)
    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    詢問 Yes/No

    用法: getyn "Would you like to install WANPIPE now? [y]"

    $NONINTERACTIVE  如果程式執行時,不想讓 user 做任何的輸入時,可以為 1
    NONINTERACTIVE=1 ,或者 = 空白,以顯示相關的提示訊息

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)
    Powered by MindTouch Core