bash 如何在not_if条件下使用shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29942892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:53:55  来源:igfitidea点击:

how to use shell script in not_if condition

bashchefrecipe

提问by SASI

I want to use shell script output to validate the resource in guard condition.I am using below code still its not working.

我想使用 shell 脚本输出来验证保护条件下的资源。我使用下面的代码仍然无法正常工作。

bash "stampDisksuccess" do
                code <<-EOH
                whoami
        EOH
        user 'root'
        not_if {"`sudo sh /home/scripts/getStampedDisk.sh test`" == ''}
      end

Whether script(getStampedDisk.sh) returns blank message or some data, bash resource is getting executed. Where i am expecting it should not run when script returns blank data.

无论 script(getStampedDisk.sh) 返回空白消息还是一些数据,bash 资源都会被执行。当脚本返回空白数据时,我期望它不应该运行。

Correct me if anything missing.

如果有任何遗漏,请纠正我。

回答by Tensibai

Let's dissect it:

我们来剖析一下:

not_if {"`sudo sh /home/scripts/getStampedDisk.sh test`" == ''}
  • Not_if ... quite obvious, it's the guard
  • {Block opening
  • "string opening
  • backticks opening
  • command
  • closing backticks and quote
  • ==operator
  • ''constant empty string
  • }closing block
  • Not_if ...很明显,是守卫
  • {块开口
  • "弦开
  • 反引号开头
  • 命令
  • 关闭反引号和报价
  • ==操作员
  • ''常量空字符串
  • }关闭块

That's a lot of operations to test you have an empty string. The output from the backticks is not guaranteed to be really empty (it can have a newline for example) and then you compare it with an arbitrary empty string (not nil, it's a string with nothing). There's many way it can turn bad and it's quite hard to debug non printing characters.

这是很多操作来测试你有一个空字符串。反引号的输出不能保证真的是空的(例如它可以有一个换行符),然后你将它与一个任意的空字符串进行比较(不是 nil,它是一个没有任何内容的字符串)。它可以通过多种方式变坏,并且很难调试非打印字符。

However the shells have already an operator for this, it's -zfor the usual bash.

然而,shell 已经有一个用于此的操作符,它-z用于通常的 bash。

Quoting bash documentation:

引用 bash 文档:

-z string
    True if the length of string is zero.
-z string
    True if the length of string is zero.

Another helper is $()to evaluate a command inside the script

另一个助手是$()评估脚本中的命令

Last one the [[ ]]construction to tell we're using an operator and not a command.

最后一个[[ ]]结构告诉我们使用的是运算符而不是命令。

All together you end up with the command execution guard when expressed as string (Documentation on guards here)

当表示为字符串时,您最终会得到命令执行保护(此处的保护文档

not_if '[[ -z $(sudo sh /home/scripts/getStampedDisk.sh test) ]]' 

Quote from the guard documentation:

引自守卫文档:

A guard attribute accepts either a string value or a Ruby block value:

  • A string is executed as a shell command. If the command returns 0, the guard is applied. If the command returns any other value, then the guard attribute is not applied.
  • A block is executed as Ruby code that must return either true or false. If the block returns true, the guard attribute is applied. If the block returns false, the guard attribute is not applied.

守卫属性接受字符串值或 Ruby 块值:

  • 字符串作为 shell 命令执行。如果命令返回 0,则应用保护。如果命令返回任何其他值,则不应用防护属性。
  • 块作为 Ruby 代码执行,必须返回 true 或 false。如果块返回true,则应用guard 属性。如果块返回false,则不应用guard 属性。

If you don't use ruby in the guard, don't use a block, you're just adding layers witch will get you in trouble, specifically when you try to compare non printable or empty strings and it's harder to debug, try to stick with standard shell commands if you need to call a command or a script, you can test it in console and be sure it is not the problem later.

如果你不在守卫中使用 ruby​​,不要使用块,你只是添加层巫婆会给你带来麻烦,特别是当你尝试比较不可打印或空字符串并且更难调试时,尝试如果您需要调用命令或脚本,请坚持使用标准 shell 命令,您可以在控制台中对其进行测试,并确保以后不会出现问题。