Linux Shell学习 之 /etc/init.d/functions 详解

本文最后更新于2017年10月28日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

简单学习一下Linux下原生的functions脚本提供的功能,可以为自己编写Bash脚本提供一些参考。

正文:

参考解答:
[root@sec-test ~]# ls -lt /etc/init.d/functions
-rw-r--r-- 1 root root 17500 May  3 06:17 /etc/init.d/functions

[root@sec-test ~]# rpm -qf /etc/init.d/functions
initscripts-9.49.39-1.el7.x86_64

&

# 字符串包含判断
# returns OK if $1 contains $2
strstr() {
  [ "${1#*$2*}" = "$1" ] && return 1
  return 0
}

# 判断给出的第一个参数表示的文件名是否为特定后缀
# Check whether file $1 is a backup or rpm-generated file and should be ignored
is_ignored_file() {
    case "$1" in
	*~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
	    return 0
	    ;;
    esac
    return 1
}

# 判断提供的第一个参数是否为「t/T/y/Y/yes/YES/Yes/.../true/...」
# Evaluate shvar-style booleans
is_true() {
    case "$1" in
	[tT] | [yY] | [yY][eE][sS] | [tT][rR][uU][eE])
	return 0
	;;
    esac
    return 1
}

# 判断提供的第一个参数是否为「f/F/n/N/no/NO/No/.../false/...」
# Evaluate shvar-style booleans
is_false() {
    case "$1" in
	[fF] | [nN] | [nN][oO] | [fF][aA][lL][sS][eE])
	return 0
	;;
    esac
    return 1
}

# 判断参数中给定的pid对应进程是否存在
# Check if any of $pid (could be plural) are running
checkpid() {
	local i

	for i in $* ; do
		[ -d "/proc/$i" ] && return 0
	done
	return 1
}

#起到类似readlink命令的作用
__readlink() {
    ls -bl "$@" 2>/dev/null| awk '{ print $NF }'
}

# 输出指定进程的pid列表(通过调用pidof命令实现)
# Output PIDs of matching processes, found using pidof
__pids_pidof() {
	pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
		pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
}

# 以守护进程的方式启动一个进程
# A function to start a program.
daemon() {
}

# 杀掉进程的函数
# A function to stop a program.
killproc() {
}

# 多彩打印
echo_success() {
}
echo_failure() {
}
echo_warning() {
}
...

# 记录操作并以多彩样式输出其执行结果
# Run some action. Log its output.
action() {
  local STRING rc

  STRING=$1
  echo -n "$STRING "
  shift
  "$@" && success $"$STRING" || failure $"$STRING"
  rc=$?
  echo
  return $rc
}

&

#!/bin/bash
# Date:        20171024
# set -x

# Source function library.
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
else
	echo '"/etc/init.d/functions" not exists'
	echo 'try "rpm -ql initscripts | grep --color /functions"'
	exit 0
fi

export PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin/:/usr/local/sbin

#action 'date +"%F %T"
#' date +"%F %T"
## [ "$?" -eq 0 ] && echo_success || echo_failure
#
## date +"%F %T"
#action 'rpm -ql /etc/init.d/functions
#' rpm -ql /etc/init.d/functions
#
## [ "$?" -eq 0 ] && echo_success || echo_failure
#$SETCOLOR_SUCCESS && echo "success" && $SETCOLOR_NORMAL
#$SETCOLOR_FAILURE && echo "failure" && $SETCOLOR_NORMAL
#$SETCOLOR_WARNING && echo "warning" && $SETCOLOR_NORMAL
#
#echo_s() {
#    $SETCOLOR_SUCCESS && echo "$@" && $SETCOLOR_NORMAL
#}
#
#echo_f() {
#    $SETCOLOR_FAILURE && echo "$@" && $SETCOLOR_NORMAL
#}
#
#echo_w() {
#    $SETCOLOR_WARNING && echo "$@" && $SETCOLOR_NORMAL
#}
#
#echo_s haha
#echo_f haha
#echo_w haha


s=$"Starting $prog (via systemctl): "    #在字符串前面添加 $ 是为啥?
echo $s

s2="Starting $prog (via systemctl): "    #测试来看没啥区别
echo $s2

#获取屏幕宽度
[ -z "${COLUMNS:-}" ] && COLUMNS=80
echo $COLUMNS

action 'checkpid 1' checkpid 1
action 'checkpid 19999' checkpid 19999

# returns OK if $1 contains $2
action 'strstr()' strstr "this is the whole string." "search_string"

action 'is_true yes' is_true yes
action 'is_true yeS' is_true yeS
action 'is_true Yes' is_true Yes
action 'is_true yEs' is_true yEs
action 'is_true yy' is_true yy

action 'is_false no' is_false no
action 'is_false No' is_false No
action 'is_false N' is_false N
action 'is_false false' is_false false
action 'is_false x' is_false x
参考链接:

=END=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/3648.html

One thought on “Linux Shell学习 之 /etc/init.d/functions 详解”

  1. Shell 函数式编程
    http://blog.xiayf.cn/2018/05/16/functional-shell-code/
    `
    true_then_run() {
    condition=$1
    action=$2

    (${condition})
    if [ $? -eq 0 ]
    then
    (${action})
    else
    echo “‘$1’ is false, don’t run ‘$2′”
    fi
    }

    false_then_run() {
    condition=$1
    action=$2

    (${condition})
    if [ $? -ne 0 ]
    then
    (${action})
    else
    echo “‘$1’ is true, don’t run ‘$2′”
    fi
    }

    map() {
    for item in $2
    do
    ($1 ${item})
    done
    }

    reduce() {
    operator=$1
    #
    list=($2)
    list_len=${#list[@]}
    #
    result=””
    if [ ${list_len} -gt 0 ]
    then
    result=${list[0]}
    fi
    #
    if [ ${list_len} -gt 1 ]
    then
    for item in ${list[@]:1}
    do
    result=$(${operator} ${result} ${item})
    done
    fi
    echo ${result}
    }

    filter() {
    for item in $2
    do
    ($1 ${item})
    if [ $? -eq 0 ]
    then
    echo ${item}
    fi
    done
    }

    sum() {
    v=0
    for arg in “$@”
    do
    v=$(($v+${arg}))
    done
    echo $v
    }
    `

发表评论

电子邮件地址不会被公开。 必填项已用*标注