【发布时间】:2014-10-17 12:36:42
【问题描述】:
我拥有一些使用 Bash shell 的脚本,条件语句中有一个 find 语句。
类似这样的:
if [ -z $(find / -type f -perm -002) ] ; then echo "no world writable found"
我想在哪里显示找到的内容而不是world write perms found。
我能做到:
echo $(find / -type f -perm -002) has world write permissions
或将变量设置为$(find / -type f -perm -002)。
但想知道是否有更好的方法来做到这一点。还有其他方法可以将 find 语句的内容作为变量检索吗?
【问题讨论】:
-
将查找结果分配给变量有什么问题?
-
result=$(find / -type f -perm -002)或local result=$(find / -type f -perm -002)如果包含在函数中将是惯用的方式。 -
注意,在原文中,你需要引用命令替换,因为如果它扩展到多个单词,
[会抱怨操作数太多。 -
这通常是一个很长的列表。管理层强调最小的变化是偏好。因此,只是探索可能存在一些我不知道的疯狂正则表达式的可能性,它们将使用现有的内容来呈现 find 命令中的内容而不使其再次运行。
标签: bash variables conditional-statements