如何在 not_if 条件下使用 shell 脚本
how to use shell script in not_if condition
我想使用 shell 脚本输出来验证 guard 中的资源 condition.I 我正在使用下面的代码仍然无法正常工作。
bash "stampDisksuccess" do
code <<-EOH
whoami
EOH
user 'root'
not_if {"`sudo sh /home/scripts/getStampedDisk.sh test`" == ''}
end
无论是脚本(getStampedDisk.sh) returns 空白消息还是一些数据,bash 资源正在执行。当脚本 returns 空白数据时,我期望它不应该 运行。
如有遗漏请指正。
我们来剖析一下:
not_if {"`sudo sh /home/scripts/getStampedDisk.sh test`" == ''}
- Not_if ...很明显,就是守卫
{
阻止打开
"
字符串开头
- 反引号开头
- 命令
- 关闭反引号和引号
==
运算符
''
常量空字符串
}
关闭块
测试你有一个空字符串的操作太多了。反引号的输出不能保证真的是空的(例如它可以有一个换行符)然后你将它与任意空字符串(不是 nil,它是一个什么都没有的字符串)进行比较。它有很多变坏的方法,而且很难调试非打印字符。
但是 shell 已经有一个运算符,它是 -z
通常的 bash。
引用 bash 文档:
-z string
True if the length of string is zero.
另一个助手是 $()
来评估脚本中的命令
最后一个 [[ ]]
构造告诉我们使用的是运算符而不是命令。
当表示为字符串 (Documentation on guards here)
时,您最终会得到命令执行保护
not_if '[[ -z $(sudo sh /home/scripts/getStampedDisk.sh test) ]]'
引自守卫文档:
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 命令,您可以在控制台中对其进行测试,并确保稍后不会出现问题。
我想使用 shell 脚本输出来验证 guard 中的资源 condition.I 我正在使用下面的代码仍然无法正常工作。
bash "stampDisksuccess" do
code <<-EOH
whoami
EOH
user 'root'
not_if {"`sudo sh /home/scripts/getStampedDisk.sh test`" == ''}
end
无论是脚本(getStampedDisk.sh) returns 空白消息还是一些数据,bash 资源正在执行。当脚本 returns 空白数据时,我期望它不应该 运行。
如有遗漏请指正。
我们来剖析一下:
not_if {"`sudo sh /home/scripts/getStampedDisk.sh test`" == ''}
- Not_if ...很明显,就是守卫
{
阻止打开"
字符串开头- 反引号开头
- 命令
- 关闭反引号和引号
==
运算符''
常量空字符串}
关闭块
测试你有一个空字符串的操作太多了。反引号的输出不能保证真的是空的(例如它可以有一个换行符)然后你将它与任意空字符串(不是 nil,它是一个什么都没有的字符串)进行比较。它有很多变坏的方法,而且很难调试非打印字符。
但是 shell 已经有一个运算符,它是 -z
通常的 bash。
引用 bash 文档:
-z string True if the length of string is zero.
另一个助手是 $()
来评估脚本中的命令
最后一个 [[ ]]
构造告诉我们使用的是运算符而不是命令。
当表示为字符串 (Documentation on guards here)
时,您最终会得到命令执行保护not_if '[[ -z $(sudo sh /home/scripts/getStampedDisk.sh test) ]]'
引自守卫文档:
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 命令,您可以在控制台中对其进行测试,并确保稍后不会出现问题。