前言
想做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