【问题标题】:Shell script evaluation failing on windows 10 using cygwin and git bash使用 cygwin 和 git bash 在 Windows 10 上进行 Shell 脚本评估失败
【发布时间】:2018-05-12 19:49:06
【问题描述】:
export testval='hdhhdhdh'
echo $testval
if [ ! -d ${testval} ]; then
echo -e "${RED}Please specify jettyScheduler as an environmental 
variable${RESET}"
exit
fi

尽管在脚本中明确设置了 testval,但上述评估失败。 我有一台装有 windows 10 操作系统的新笔记本电脑,以上在 Windows 7 上使用 cygwin 运行良好。在 windows 10 上使用 cygwin 和 git bash 也是如此。有人可以帮忙吗?

【问题讨论】:

  • 您需要提供更多信息,例如您的期望、您得到的、您从哪里运行脚本等等。
  • 您好,脚本是在 Windows 10 OS 上从 cygwin 运行的。事实上,当我使用 if [ -z ${testval} ] 语法时它开始工作,if [! -d ${testval}] 无法正常运行? .脚本在上下文中的期望是它不应该打印 echo 语句,因为它应该只在未在环境中设置 testval 时才打印语句,但情况并非如此。让我知道是否需要任何其他补充

标签: windows cygwin git-bash


【解决方案1】:

你误解了test -d 的作用——它检查目录是否存在,所以你看到的是预期的(假设'hdhhdhdh' 不是目录的名称)。如果您只想检查是否设置了环境,那么test -z(或相反的test -n)是一种方法,正如您所见。

附:如果变量可能包含空格,您还应该养成引用变量 ("$testval") 的习惯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2018-04-17
    • 2020-09-16
    • 2013-03-22
    • 2015-12-10
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多