gpt4 book ai didi

Linux 脚本在两台服务器的 ksh 上表现不同

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:30:18 25 4
gpt4 key购买 nike

以下代码块来自 Linux 脚本。它在具有相同操作系统的不同 Linux 服务器上提供不同的输出。

SECURE="YES"
if [[ !((-n "$SECURE") && (( "$SECURE" == "YES") || ("$SECURE" == "NO"))) ]]
then
echo -e "Validation failed for varilable SECURE: $SECURE"
else
echo "Validation passed"
fi

Server-1 输出:

Validation failed for varilable SECURE: YES

服务器 2 输出:

Validation passed

不确定为什么输出不同。有关此的任何详细信息都会有所帮助。谢谢。

更新:我在两台服务器中都添加了 #!/bin/bash 作为第一行,它们给出了与“验证通过”相同的输出。

当我添加 #!/bin/ksh 时,我得到:

Server-1 输出:

Validation failed for varilable SECURE: YES

服务器 2 输出:

syntax error: `!( ( -n "$SECURE" ) && ( ( "$SECURE" == "YES" ) || ( "$SECURE" == "NO" )))' missing expression operator

默认情况下,两台服务器都使用 ksh。调用此命令:

env | grep SHELL 

显示:

SHELL=/bin/ksh

但为什么输出不同呢?

更新 2:我发现两台服务器的 KSH 版本不同。一台服务器有 pdksh,另一台有 ksh93。谁能告诉我这两个版本之间的区别?

最佳答案

我知道这是一个老问题,但是......

我认为KSH-93 Frequently Asked Questions - Q17有一个简短的答案。

Q17. What is pdksh and is it related to ksh or KornShell?

A17. pdksh is a public domain version of a UNIX shell that is unrelated to ksh. It supports most of the 1988 KornShell language features and some of the 1993 features. Some KornShell scripts will not run with pdksh.

现在,根据 ksh88 的手册,te OP 的脚本应该可以工作。也许解析器期望运算符之间有空格,甚至他可能发现了错误。

无论如何,这应该适用于两个版本(即使使用 SECURE=""):

SECURE="YES"
if [[ "$SECURE" == "YES" || "$SECURE" == "NO" ]]
then
echo "Validation passed"
else
echo "Validation failed for varilable SECURE: '$SECURE'"
fi

关于Linux 脚本在两台服务器的 ksh 上表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28450985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com