是否可以在 /etc/rc.local 中有条件?

ade*_*hox 4 shell shell-script test rc syntax

内有条件可以/etc/rc.local吗?我检查了很多问答,大多数人都建议chmod +x在上面运行,但我的问题不同。它实际上没有条件的情况下对我有用,但没有其他条件。

#!/bin/sh

if [[ -e /usr/src/an-existing-file ]]
then
    echo "seen" >> /etc/rclocalmadethis
fi
Run Code Online (Sandbox Code Playgroud)

这是我运行时看到的奇怪错误systemctl status rc-local.service

rc.local[481]: /etc/rc.local: 3: /etc/rc.local: [[: not found
Run Code Online (Sandbox Code Playgroud)

这是我rc.local在完全相同的位置ls -lah /etc/

-rwxr-xr-x  1 root root    292 Sep 19 09:13 rc.local
Run Code Online (Sandbox Code Playgroud)

我使用的是 Debian 10 标准版。

And*_*ton 18

[[ ... ]]语法是无效的/bin/sh。尝试:

if [ -e /usr/src/an-existing-file ]
then
    echo "seen" >> /etc/rclocalmadethis
fi
Run Code Online (Sandbox Code Playgroud)

请注意,有时它会起作用,因为/bin/sh -> /bin/bash或其他一些支持该语法的 shell,但您不能依赖于这种情况(如您在此处看到的)。

您可以运行ls -l /bin/sh以了解此信息,例如:

lrwxrwxrwx 1 root root 4 Jul 18  2019 /bin/sh -> dash
Run Code Online (Sandbox Code Playgroud)