linux sh : check sh function is exist; multiple line comment;

前言

想做linux安装程序,动手时,又想到一个前置任务.

想到要将制作安装工程的配置和安装工程本身的配置放到ini中.
换个安装工程时,可以最大限度的重用安装脚本。
查到github上的一个工程,学到了检测脚本函数是否存在的实现。

要将实验结果贴到工程中存档,又去查了sh中多行注释的写法。sh中的多行注释,以前没用过。这很实用。

实验

# !/bin/sh

# @file /linux_installer_v1/make_linux_installation_program.sh

. ./helper/sh.ini_file_opt

# multiple line comment, "<<MY_COMMENT" and "MY_COMMENT" must on column 0, "MY_COMMENT" can be other text
# "multiple line comment begin"
<<MY_NOTE
if type -x obj(is a file or system cmd)

type -a aptitude : rc = 0, rv = aptitude 是 /usr/bin/aptitude
type -f aptitude : rc = 0, rv = aptitude 是 /usr/bin/aptitude
type -p aptitude : rc = 0, rv = /usr/bin/aptitude
type -t aptitude : rc = 0, rv = file
type -P aptitude : rc = 0, rv = /usr/bin/aptitude

if type -x obj(is a function on sh)

type -a shini_get_section_key_s_value : rc = 0, rv = shini_get_section_key_s_value 是函数
shini_get_section_key_s_value () 
{ 
    # sh function code will be all print here
    ...
}
type -p shini_get_section_key_s_value : rc = 0, rv = 
type -t shini_get_section_key_s_value : rc = 0, rv = function
type -P shini_get_section_key_s_value : rc = 1, rv = 
...

MY_NOTE
# "multiple line comment end"

# @fn func_is_sh_function_exist
# @brief check is shell script function or system command exist ?
# @param IN $1, fucntion name or system command name
# @return 0 is exist, other value is not exist
#
function func_is_sh_function_exist() {
    local str_function_name=$1
    local argc=$#
    local rc=0
    local rv=""

    if [ $argc -le 0 ]
    then
        echo "error : too few parameter"
        return 255
    fi

    if [ -z  $str_function_name ]
    then
        echo "error : function name is empty"
        return 255
    fi

    rv=$(type -t $str_function_name)
    rc=$?

    if [ $rc -ne 0 ]
    then
        return $rc
    fi

    # ok, the function is exist, can call it
    return 0
}

function main() {
    local rv=""
    local rc=0
    local str_cmd=""

    clear
    echo "execute make_linux_installation_program.sh..."

    # rv=$(func_is_sh_function_exist) # test case
    # rv=$(func_is_sh_function_exist "") # test case
    rv=$(func_is_sh_function_exist "shini_get_section_key_s_value")
    rc=$?
    if [ 0 -ne $rc ]
    then
        echo "$rv"
        return $rc
    else
        echo "ok : func_is_sh_function_exist exist, can call it"
    fi

    rv=$(shini_get_section_key_s_value "./config/installer.ini" "section2" "value2")
    rc=$?
    echo "rc=$rc, rv=$rv"

    echo "execute make_linux_installation_program.sh END"
    return 0
}

main
exit 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值