linux - 在 Linux 启动脚本中获取其他脚本

标签 linux bash shell scripting

我遇到了以下脚本,有些东西我不能完全理解

    #!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=50

start() {
        mkdir -m 0755 -p /var/run/vsftpd
        service_start /usr/sbin/vsftpd
}

stop() {
        service_stop /usr/sbin/vsftpd
}

这里如何使用'/etc/rc.common'?

这是rc.common的内容

#!/bin/sh
# Copyright (C) 2006-2011 OpenWrt.org

. $IPKG_INSTROOT/lib/functions.sh
. $IPKG_INSTROOT/lib/functions/service.sh

initscript=$1
action=${2:-help}
shift 2

start() {
        return 0
}

stop() {
        return 0
}

reload() {
        return 1
}

restart() {
        trap '' TERM
        stop "$@"
        start "$@"
}

boot() {
        start "$@"
}

shutdown() {
        stop
}

disable() {
        name="$(basename "${initscript}")"
        rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
        rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
}

enable() {
        name="$(basename "${initscript}")"
        disable
        [ -n "$START" -o -n "$STOP" ] || {
                echo "/etc/init.d/$name does not have a START or STOP value"
                return 1
        }
        [ "$START" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
        [ "$STOP"  ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}"
}

enabled() {
        name="$(basename "${initscript}")"
        [ -x "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" ]
}

depends() {
        return 0
}

help() {
        cat <<EOF
Syntax: $initscript [command]

Available commands:
        start   Start the service
        stop    Stop the service
        restart Restart the service
        reload  Reload configuration files (or restart if that fails)
        enable  Enable service autostart
        disable Disable service autostart
$EXTRA_HELP
EOF
}

. "$initscript"

ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
list_contains ALL_COMMANDS "$action" || action=help
[ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'
$action "$@"

希望你们中的一些人能阐明这一点。谢谢!

PS:我不太明白的另一件事是如何通过简单地将函数名称附加到启动脚本的完整路径来调用脚本中的函数。例如,'/etc/init.d/vsftpd test' 将在/etc/init.d/vsftpd 或/etc/rc.common 中执行一个名为 'test' 的函数。但是如果 'test' 函数同时定义在启动脚本和/etc/rc.common,前者中的函数会运行,而rc.common中的函数不会运行。

还有,为什么不是

'[ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'

简单写成

'[ "$action" = "reload" ] && action='eval reload "$@" || restart "$@"'

谢谢!

最佳答案

来自 execve(2) 在一个相当新的 Linux 系统上:

Interpreter scripts

An interpreter script is a text file that has execute permission enabled and whose first line is of the form:

#! interpreter [optional-arg]

The interpreter must be a valid pathname for an executable which is not itself a script. If the filename argument of execve() specifies an interpreter script, then interpreter will be invoked with the following arguments:

interpreter [optional-arg] filename arg...

where arg... is the series of words pointed to by the argv argument of execve().

For portable use, optional-arg should either be absent, or be specified as a single word (i.e., it should not contain white space); [...]

我还没有看到很多使用 #!/bin/sh filename 习惯用法的脚本。我发现它的使用令人困惑。

也许一个简单的测试就能说明问题。这些文件应该存在于/tmp/test 中,在这种情况下,考虑到 test_interpreter.sh 中解释器行的详细信息,这很重要。

以“#!”命名的脚本line (rc_interpreter_line) 安排在最初调用的脚本中运行命令,这是我通过 sourcing_script 变量和 shift 命令执行的。您在问题中引用的代码做了这个链接的一个相当复杂的版本。如果没有这种链接,运行的只是解释器行中命名的文件。

rc_interpreter_line 的内容

echo '===='
echo $0 "$@"

TESTVAR=set

sourcing_script=$1
shift

. "$sourcing_script" "$@"

echo '===='

test_interpreter.sh 的内容

#!/bin/sh /tmp/test/rc_interpreter_line

echo '-----'
echo "My file name is test_interpreter.sh, but \$0 is $0"

echo Command line: $0 "$@"

echo "TESTVAR is '$TESTVAR'"
echo '-----'

exit 0

权限:

sh-4.2$ ls -l
total 8
-rw-r--r-- 1 dev dev 104 Aug 24 13:36 rc_interpreter_line
-rwxr-xr-x 1 dev dev 191 Aug 24 13:36 test_interpreter.sh

样本运行。先直接运行test_interpreter.sh。

sh-4.2$ ./test_interpreter.sh -opt arg1 arg2
====
/tmp/test/rc_interpreter_line ./test_interpreter.sh -opt arg1 arg2
-----
My file name is test_interpreter.sh, but $0 is /tmp/test/rc_interpreter_line
Command line: /tmp/test/rc_interpreter_line -opt arg1 arg2
TESTVAR is 'set'
-----

第二个更清楚地调用 shell。没有触发 execve(2) 行为,所以这次 shell 运行只是运行 test_interpreter.sh 中的命令,将第一行视为注释。

sh-4.2$ sh test_interpreter.sh -opt arg1 arg2
-----
My file name is test_interpreter.sh, but $0 is test_interpreter.sh
Command line: test_interpreter.sh -opt arg1 arg2
TESTVAR is ''
-----

但我个人的偏好是完全避免使用该成语。在脚本的早期简单地使用命令对我来说要清楚得多,例如:

. /etc/rc.common

...而不是依赖“有趣的”'#!'行,这样做会在使用 ./my_scriptsh my_script

时产生不同的行为

关于linux - 在 Linux 启动脚本中获取其他脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18419119/

相关文章:

Linux : launch a specific action when another process is terminated

linux - 在Docker文件中使用pip安装AWS CLI

python - Bash - 标准输出文本在重定向到文件时被截断

bash - Bash脚本错误检查

string - 带有额外空格的多行字符串(保留缩进)

c - x86-64 System V abi - 参数传递的参数分类

linux - 如何从数据行中删除换行符

bash tee 去除颜色

python - 无法从 python 子进程 : permission denied 执行 shell 脚本

linux - 在shell中,如果文件大于100MB,如何删除文件,否则移动