即使退出命令后,Shell 脚本仍继续运行

发布于 2024-12-11 19:47:12 字数 327 浏览 0 评论 0原文

我的 shell 脚本如下所示:

#!/bin/bash

# Make sure only root can run our script
[ $EUID -ne 0 ] && (echo "This script must be run as root" 1>&2) || (exit 1)

# other script continues here...

当我使用非 root 用户运行上述脚本时,它会打印消息“此脚本...”,但它不会在那里退出,它会继续执行剩余的脚本。我做错了什么?

注意:我不想使用 if 条件。

My shell script is as shown below:

#!/bin/bash

# Make sure only root can run our script
[ $EUID -ne 0 ] && (echo "This script must be run as root" 1>&2) || (exit 1)

# other script continues here...

When I run above script with non-root user, it prints message "This script..." but it doe not exit there, it continues with the remaining script. What am I doing wrong?

Note: I don't want to use if condition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

将军与妓 2024-12-18 19:47:12

您正在子 shell 中运行 echoexit。 exit 调用只会留下该子 shell,这有点毫无意义。

尝试使用:

#! /bin/sh
if [ $EUID -ne 0 ] ; then
    echo "This script must be run as root" 1>&2
    exit 1
fi
echo hello

如果由于某种原因您不需要 if 条件,只需使用:

#! /bin/sh
[ $EUID -ne 0 ] && echo "This script must be run as root" 1>&2 && exit 1
echo hello

注意:没有 () 和固定布尔条件。警告:如果 echo 失败,该测试也将无法退出。 if 版本更安全(并且更具可读性,更易于维护 IMO)。

You're running echo and exit in subshells. The exit call will only leave that subshell, which is a bit pointless.

Try with:

#! /bin/sh
if [ $EUID -ne 0 ] ; then
    echo "This script must be run as root" 1>&2
    exit 1
fi
echo hello

If for some reason you don't want an if condition, just use:

#! /bin/sh
[ $EUID -ne 0 ] && echo "This script must be run as root" 1>&2 && exit 1
echo hello

Note: no () and fixed boolean condition. Warning: if echo fails, that test will also fail to exit. The if version is safer (and more readable, easier to maintain IMO).

似梦非梦 2024-12-18 19:47:12

我认为您需要 && 而不是 ||,因为您想要 echo 退出(而不是 echo 或 退出)。

此外,(exit 1) 将运行一个退出的子 shell,而不是退出当前的 shell。

以下脚本显示了您所需要的内容:

#!/bin/bash
[ $1 -ne 0 ] && (echo "This script must be run as root." 1>&2) && exit 1
echo Continuing...

使用 ./myscript 0 运行此脚本将为您提供:

Continuing...

./myscript 1 将为您提供:

This script must be run as root.

我相信这就是您正在寻找的内容。

I think you need && rather than||, since you want to echo and exit (not echo or exit).

In addition (exit 1) will run a sub-shell that exits rather than exiting your current shell.

The following script shows what you need:

#!/bin/bash
[ $1 -ne 0 ] && (echo "This script must be run as root." 1>&2) && exit 1
echo Continuing...

Running this with ./myscript 0 gives you:

Continuing...

while ./myscript 1 gives you:

This script must be run as root.

I believe that's what you were looking for.

眼泪都笑了 2024-12-18 19:47:12

我将其写为:

(( $EUID != 0 )) && { echo "This script must be run as root" 1>&2; exit 1; }

使用 { } 进行分组,该分组在当前 shell 中执行。请注意,大括号周围的空格和结尾的分号是必需的。

I would write that as:

(( $EUID != 0 )) && { echo "This script must be run as root" 1>&2; exit 1; }

Using { } for grouping, which executes in the current shell. Note that the spaces around the braces and the ending semi-colon are required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文