bash ライブラリ集

更新日:2018年7月7日

bash ライブラリ集を作成

関数名 概要
common  
 align  
  left パラメーターで指定した文字数の範囲で左側に配置する関数
  center パラメーターで指定した文字数の範囲で中央に配置する関数
  right パラメーターで指定した文字数の範囲で右側に配置する関数
 get  
  string パラメーターで指定した数の文字列を返す関数
  cpu-information パラメーターで指定したCPUの情報を返す関数
  memory-information パラメーターで指定したメモリーの情報を返す関数
  config パラメーター(セクション名、キー名)で指定した値を返す関数
  duplicate-activation 重複起動の状態を返す関数

common/align/left


#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/align/left
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: AlignLeft
# DESCRIPTION: Position the character string in the left.
# EXAMPLE:
# Result="$(AlignLeft 'String' Length)" # Return=${?} #------------------------------------------------------------------------------ function AlignLeft() { local var_String local -i var_Length local -i var_StringLength local -i var_RightSpanLength local var_Result local -i var_Return var_String="${1}" var_Length=${2} var_StringLength=${#1} if [ ${var_Length} -gt ${var_StringLength} ]; then var_RightSpanLength=$(( ${var_Length} - ${var_StringLength} )) var_Result="${var_String}" var_Result+="$(eval $(echo -n "printf '%${var_RightSpanLength}s'"))" var_Return=0 else var_Result="${var_String}" var_Return=1 fi var_Result="$(echo -n "${var_Result}" | cut -c 1-${var_Length})" echo -n "${var_Result}" return ${var_Return} unset var_String unset var_Length unset var_StringLength unset var_RightSpanLength unset var_Result unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Position the character string in the left. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -s 'String' -l Length _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagString declare -a var_ValueString declare -i var_FlagStringLength declare -a var_ValueStringLength while getopts ":hs:l:" OPT do case ${OPT} in s) var_FlagString=1 var_ValueString=${OPTARG} ;; l) var_FlagStringLength=1 var_ValueStringLength=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagString+UNDEF}" ]] && \ [[ -n "${var_FlagStringLength+UNDEF}" ]] && \ echo "$(AlignLeft \ "${var_ValueString[0]}" \ ${var_ValueStringLength[0]})" exit ${?}

common/align/center


#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/align/center
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: AlignCenter
# DESCRIPTION: Position the character string in the center.
# EXAMPLE:
# Result="$(AlignCenter 'String' Length)" # Return=${?} #------------------------------------------------------------------------------ function AlignCenter() { local var_String local -i var_Length local -i var_StringLength local -i var_LeftSpanLength local -i var_RightSpanLength local var_Result local -i var_Return var_String="${1}" var_Length=${2} var_StringLength=${#1} if [ ${var_Length} -gt ${var_StringLength} ]; then var_LeftSpanLength=$(( (${var_Length} - ${var_StringLength}) / 2 )) var_RightSpanLength=$(( ${var_Length} - (${var_StringLength} + ${var_LeftSpanLength}) )) var_Result="$(eval $(echo -n "printf '%${var_LeftSpanLength}s'"))" var_Result+="${var_String}" var_Result+="$(eval $(echo -n "printf '%${var_RightSpanLength}s'"))" var_Return=0 else var_Result="${var_String}" var_Return=1 fi var_Result="$(echo -n "${var_Result}" | cut -c 1-${var_Length})" echo -n "${var_Result}" return ${var_Return} unset var_String unset var_Length unset var_StringLength unset var_LeftSpanLength unset var_RightSpanLength unset var_Result unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Position the character string in the center. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -s 'String' -l Length _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagString declare -a var_ValueString declare -i var_FlagStringLength declare -a var_ValueStringLength while getopts ":hs:l:" OPT do case ${OPT} in s) var_FlagString=1 var_ValueString=${OPTARG} ;; l) var_FlagStringLength=1 var_ValueStringLength=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagString+UNDEF}" ]] && \ [[ -n "${var_FlagStringLength+UNDEF}" ]] && \ echo "$(AlignCenter \ "${var_ValueString[0]}" \ ${var_ValueStringLength[0]})" exit ${?}

common/align/right

#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/align/right
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: AlignRight
# DESCRIPTION: Position the character string in the right.
# EXAMPLE:
# Result="$(AlignRight 'String' Length)" # Return=${?} #------------------------------------------------------------------------------ function AlignRight() { local var_String local -i var_Length local -i var_StringLength local -i var_LeftSpanLength local var_Result local -i var_Return var_String="${1}" var_Length=${2} var_StringLength=${#1} if [ ${var_Length} -gt ${var_StringLength} ]; then var_LeftSpanLength=$(( ${var_Length} - ${var_StringLength} )) var_Result="$(eval $(echo -n "printf '%${var_LeftSpanLength}s'"))" var_Result+="${var_String}" var_Return=0 else var_Result="${var_String}" var_Return=1 fi var_Result="$(echo -n "${var_Result}" | rev | cut -c 1-${var_Length} | rev)" echo -n "${var_Result}" return ${var_Return} unset var_String unset var_Length unset var_StringLength unset var_LeftSpanLength unset var_Result unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Position the character string in the right. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -s 'String' -l Length _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagString declare -a var_ValueString declare -i var_FlagStringLength declare -a var_ValueStringLength while getopts ":hs:l:" OPT do case ${OPT} in s) var_FlagString=1 var_ValueString=${OPTARG} ;; l) var_FlagStringLength=1 var_ValueStringLength=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagString+UNDEF}" ]] && \ [[ -n "${var_FlagStringLength+UNDEF}" ]] && \ echo "$(AlignRight \ "${var_ValueString[0]}" \ ${var_ValueStringLength[0]})" exit ${?}

common/get/string


#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/get/string
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: PrintString
# DESCRIPTION: Print the specified number of strings.
# EXAMPLE:
# Result="$(PrintString 'String' Length)" # Return=${?} #------------------------------------------------------------------------------ function PrintString() { local var_String local -i var_Length local var_Result local -i var_Return=0 var_String="$(echo "${1}" | sed -e 's/-/─/g')" var_Length=${2} var_Result="$(eval $(echo "printf '${var_String}''%.s' {1..${var_Length}}"))" echo -n "${var_Result}" return ${var_Return} unset var_String unset var_Length unset var_Result unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Print the specified number of strings. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -s 'String' -l Length _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagString declare -a var_ValueString declare -i var_FlagStringLength declare -a var_ValueStringLength while getopts ":hs:l:" OPT do case ${OPT} in s) var_FlagString=1 var_ValueString=${OPTARG} ;; l) var_FlagStringLength=1 var_ValueStringLength=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagString+UNDEF}" ]] && \ [[ -n "${var_FlagStringLength+UNDEF}" ]] && \ echo "$(PrintString \ "${var_ValueString[0]}" \ ${var_ValueStringLength[0]})" exit ${?}

common/get/cpu-information


#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/get/cpu-information
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: GetCpuInformation
# DESCRIPTION: Get cpu information.
# EXAMPLE:
# Result="$(GetCpuInformation 'Type')" # Return=${?} #------------------------------------------------------------------------------ function GetCpuInformation() { local var_GetType local var_Process local var_Result local -i var_Return var_GetType="${1}" var_Process='/proc/cpuinfo' if [ -f "${var_Process}" ]; then var_Result="$(grep -e "^${var_GetType}[[:blank:]]*:" "${var_Process}" | \ head -n 1 | \ awk -F ':' '{print $2}' | \ sed -e 's/^[ ]*//g')" var_Return=0 else var_Result='' var_Return=1 fi echo -n "${var_Result}" return ${var_Return} unset var_GetType unset var_Process unset var_Result unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Get cpu information. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -t 'Type' $(echo -e -n "${var_ChangeColor}")type list:$(echo -e -n "${var_DefaultColor}") $(cat /proc/cpuinfo | \ sed '/^$/d' | \ sed -e 's/:[ ].*//g' | \ sed -e 's/://g' | \ sed -e 's/^/ /g') _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagGetType declare -a var_ValueGetType while getopts ":ht:" OPT do case ${OPT} in t) var_FlagGetType=1 var_ValueGetType=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagGetType+UNDEF}" ]] && \ echo "$(GetCpuInformation "${var_ValueGetType[0]}")" exit ${?}

common/get/memory-information

#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/get/memory-information
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: GetMemoryInformation
# DESCRIPTION: Get memory information.
# EXAMPLE:
# Result="$(GetMemoryInformation 'Type')" # Return=${?} #------------------------------------------------------------------------------ function GetMemoryInformation() { local var_GetType local var_Process local var_Result local -i var_Return var_GetType="${1}" var_Process='/proc/meminfo' if [ -f "${var_Process}" ]; then var_Result="$(grep -e "^${var_GetType}:" "${var_Process}" | \ head -n 1 | \ awk -F ':' '{print $2}' | \ sed -e 's/^[ ]*//g')" var_Return=0 else var_Result='' var_Return=1 fi echo -n "${var_Result}" return ${var_Return} unset var_GetType unset var_Process unset var_Result unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Get memory information. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -t 'Type' $(echo -e -n "${var_ChangeColor}")type list:$(echo -e -n "${var_DefaultColor}") $(cat /proc/meminfo | \ sed '/^$/d' | \ sed -e 's/:[ ].*//g' | \ sed -e 's/^/ /g') _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagGetType declare -a var_ValueGetType while getopts ":ht:" OPT do case ${OPT} in t) var_FlagGetType=1 var_ValueGetType=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagGetType+UNDEF}" ]] && \ echo "$(GetMemoryInformation "${var_ValueGetType[0]}")" exit ${?}

common/get/config


#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/get/config
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: GetConfig
# DESCRIPTION: Get setting value from setting file.
# EXAMPLE:
# Result$(GetConfig 'Config file' 'Section name' 'Key name') # Return=${?} #------------------------------------------------------------------------------ function GetConfig() { local var_Config local var_Section local var_Key local var_Value local var_Temp local -i var_Line local -i var_Return var_Config="${1}" var_Section="${2}" var_Key="${3}" grep -e "^\[${var_Section}\]" "${var_Config}" >& /dev/null if [ ${?} = 0 ]; then var_Temp="$(mktemp)" var_Line=$(( $(wc -l "${var_Config}" | \ awk '{print $1}') - \ $(grep -e "^\[${var_Section}\]" -n "${var_Config}" | \ head -n 1 | \ awk -F ':' '{print $1}') )) tail -n ${var_Line} "${var_Config}" | \ grep -v '^#' > "${var_Temp}" var_Line=$(( $(grep -e '^\[' -n "${var_Temp}" | \ head -n 1 | \ awk -F ':' '{print $1}') - 1 )) if [ ${var_Line} -gt 0 ]; then var_Value="$(head -n ${var_Line} "${var_Temp}" | \ grep -e "${var_Key}=" | \ awk -F '=' '{print $2}')" else var_Value="$(grep -e "${var_Key}=" "${var_Temp}" | \ awk -F '=' '{print $2}')" fi rm -f "${var_Temp}" var_Return=0 else var_Value='' var_Return=1 fi echo -n "${var_Value}" return ${var_Return} unset var_Config unset var_Section unset var_Key unset var_Value unset var_Temp unset var_Line unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Get setting value from setting file. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -c 'Config file' -s 'Section name' -k 'Key name' _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagConfigFile declare -a var_ValueConfigFile declare -i var_FlagSectionName declare -a var_ValueSectionName declare -i var_FlagKeyName declare -a var_ValueKeyName while getopts ":hc:s:k:" OPT do case ${OPT} in c) var_FlagConfigFile=1 var_ValueConfigFile=${OPTARG} ;; s) var_FlagSectionName=1 var_ValueSectionName=${OPTARG} ;; k) var_FlagKeyName=1 var_ValueKeyName=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagConfigFile+UNDEF}" ]] && \ [[ -n "${var_FlagSectionName+UNDEF}" ]] && \ [[ -n "${var_FlagKeyName+UNDEF}" ]] && \ echo "$(GetConfig \ "${var_ValueConfigFile[0]}" \ "${var_ValueSectionName[0]}" \ "${var_ValueKeyName[0]}")" exit ${?}

設定ファイル(.conf)サンプル


#
# example 1
#
[Section_A]
Key_A1=Value A1
Kek_A2=Value A2
Key_A3=Value A3
#
# example 2
#
[Section_B]
Key_B1=Value B1
Key_B2=Value B2
Key_B3=Value B3
#
# example 3
#
[Section_C]
Key_C1=Value C1
Key_C2=Value C2
Key_C3=Value C3

common/get/duplicate-activation

#!/usr/bin/bash

#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#
# (C) 1971-2017 Matt.
#
# SCRIPT NAME: common/get/duplicate-activation
# VERSION: 1.0
# CREATED DATE: 2017/07/07
# UPDATE DATE: 2017/07/07
#
#_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#------------------------------------------------------------------------------
# FUNCTION NAME: GetDuplicateActivation
# DESCRIPTION: Get the state of duplicate activation.
# EXAMPLE:
# Result="$(GetDuplicateActivation 'Script name')" #------------------------------------------------------------------------------ function GetDuplicateActivation() { local var_ScriptName local -i var_ProcessCount local -i var_Result local -i var_Return=0 var_ScriptName="${1}" var_ProcessCount=$(ps a | grep -e "${var_ScriptName}"'$' | wc -l) if [ ${var_ProcessCount} -gt 5 ]; then var_Result=1 else var_Result=0 fi echo -n "${var_Result}" return ${var_Return} unset var_ScriptName unset var_ProcessCount unset var_Result unset var_Return } #------------------------------------------------------------------------------ # FUNCTION NAME: ViewHelp # DESCRIPTION: Display explanation of usage. #------------------------------------------------------------------------------ function ViewHelp() { local -r var_ChangeColor='\033[32m\033[40m' local -r var_DefaultColor='\033[00m' local var_Command local -i var_Return=0 var_Command="$(basename "${0}")" cat << _EOF_ $(echo -e -n "${var_ChangeColor}")description:$(echo -e -n "${var_DefaultColor}") Get the state of duplicate activation. $(echo -e -n "${var_ChangeColor}")usage:$(echo -e -n "${var_DefaultColor}") ${var_Command} -s 'Script name' _EOF_ exit ${var_Return} } #------------------------------------------------------------------------------ # DESCRIPTION: Main. #------------------------------------------------------------------------------ declare -a OPT declare -i var_FlagScriptName declare -a var_ValueScriptName while getopts ":hs:" OPT do case ${OPT} in s) var_FlagScriptName=1 var_ValueScriptName=${OPTARG} ;; h) ViewHelp ;; :) echo '[ERROR] Option argument is undefined.' ;; \?) echo '[ERROR] Undefined options.' ;; esac done shift $(( ${OPTIND} - 1 )) [[ -n "${var_FlagScriptName+UNDEF}" ]] && \ echo $(GetDuplicateActivation "${var_ValueScriptName[0]}") exit ${?}