在 Ubuntu 上,带有禁用功能的简单脚本不起作用

在 Ubuntu 上,带有禁用功能的简单脚本不起作用

好的,我编写了一个脚本来简单地显示大量信息和入门命令的链接(例如 shhttps://website.tld/scripts/getstarted.sh)并可以将其禁用,例如

commandenabled=false
if [[ commandenabled = false ]]; then
echo "command has been disabled by an administrator"
else
Rest Of Command Here

这对我来说不起作用,即使代码中有其他部分也是如此。我的代码:

#!/usr/bin/env

# Command Options
headname=unlac.net
commandenabled=true
commandname=ulcwelcome
version=0.0.1
author=ydupc
servername=ulctesting


if [[ $commandenabled = "false" ]]; then
    echo "\e[4m\e[1mErr: ULC-001, Command Disabled.\e[0m"
else
    echo "\e[2m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[$headname]=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\e[0m"
fi

有人知道为什么它仍然执行“else”语句之后的区域,即使设置为禁用也是如此吗?我在这里真的很困惑。

答案1

我猜测该脚本不是用 Bash 运行的。

舍邦(脚本的第一行)是错误的。应该是:

#!/bin/bash

那么你的 if 语句使用的是[[,这是一种“Bashism”——所以我猜 if 语句根本没有得到正确的评估。

我还猜测,如果使用单个方括号,则 if 语句会被正确评估[,这是默认的 Posix if 语句。

[ ]尝试使用适当的 shebang 或if 语句中的评估表达式再次运行脚本。

一个好主意是始终通过 运行你的脚本shellcheck,如下所示:

shellcheck <yourscript>

它检测错误的能力比我们人类强得多。

相关内容