通过 shell 脚本检查正则表达式时收到“权限被拒绝”消息

通过 shell 脚本检查正则表达式时收到“权限被拒绝”消息

检查以下 shell 脚本:

#!/bin/bash
check=yes
if [[ $check =~ yes|no ]]; then
    echo yes or no.
else
    echo I did not understood the pattern.
fi

在我的 OSX 中运行,一切都按预期进行。只要给我yes or nor.留言即可。但是当在 Ubuntu 14.04.5 LTS 服务器上运行它时,出现错误:

teste.sh: 3: teste.sh: [[: Permission denied
teste.sh: 3: teste.sh: no: Permission denied
I did not understood the pattern.

已经检查了 OSX 和 Ubuntu shell 版本。 OSX 提供了较旧的 shell 版本:

[email protected]:/tmp/>$SHELL --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.

在 Ubuntu 中:

t4h@web129:~$ $SHELL --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

对我来说,shell 信息并没有多大启发。

是否有不同的方法来检查 shell 中的正则表达式,该方法将在任何 *nix 系统中平等地运行并与不同的 shell 版本兼容?

答案1

这是一个需要用作解释器的bash脚本。bash

当您使用 运行它时sh,您没有使用bash.是的,有些sh实际上是bash经过伪装的,但这并不重要(bash在所有情况下它都不会表现得像“普通”)。将shbash视为互斥的解释器,就好像它们是perlpython(即不要尝试混合它们)。

运行脚本

$ bash teste.sh

或者使其可执行并直接运行它:

$ chmod +x teste.sh
$ ./teste.sh

最后一种方法可能更可取。这让脚本通过它的#!-line 决定应该使用哪个解释器。

相关内容