【问题标题】:Source other scripts in Linux startup script在 Linux 启动脚本中获取其他脚本
【发布时间】:2013-08-25 09:43:44
【问题描述】:

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

    #!/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 "$@"'

谢谢!

【问题讨论】:

  • 你的第一个样本是整个脚本吗?
  • 是的,我只是随机挑选了一个简单的!所有启动脚本共享相同的起始行 #!/bin/sh /etc/rc.common
  • 文件开头的“hash bang”可以为解释器提供可选参数。所以通常你可以有#!/usr/bin/sh [options]。但是脚本文件不被视为 shell 的“选项”,而且我还没有看到以这种方式提供 shell 脚本作为参数的情况。我在bash 中做了一个快速测试用例,并没有观察到有用的行为(即bash 忽略了可选的脚本文件名参数)。您的 rc.common 文件展示了包含另一个脚本的正确方法(以点 (.) 开头)。
  • 我检查了我在 Fedora 中的所有 rc.d 脚本,但没有一个使用您所展示的机制。你有哪个 Linux 发行版?
  • 它是 Openwrt,一个用于 32 内存或更多内存的无线路由器的轻量级发行版。

标签: linux bash shell scripting


【解决方案1】:

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

解释器脚本

解释器脚本是具有执行权限的文本文件 已启用,其第一行的格式为:

#! interpreter [optional-arg]

解释器必须是可执行文件的有效路径名 本身不是脚本。如果 execve() 的文件名参数指定了一个 解释器脚本,然后解释器将被调用 以下论点:

interpreter [optional-arg] filename arg...

其中 arg... 是 argv 参数指向的一系列单词 execve().

对于便携式使用,可选参数应该不存在,或者 指定为单个单词(即,它不应包含空格); [...]

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

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

“#!”中命名的脚本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 时产生不同的行为

【讨论】:

  • 感谢详细的回复!但我仍然不太明白如何通过简单地将函数名称附加到启动脚本的完整路径来调用脚本中的函数。例如,“/etc/init.d/vsftpd test”将在 /etc/init.d/vsftpd 或 /etc/rc.common 中执行名为“test”的函数。再次感谢!
猜你喜欢
  • 2023-03-13
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 2020-01-28
  • 2010-11-24
  • 1970-01-01
相关资源
最近更新 更多