Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 来自条件语句的变量_Bash_Variables_Conditional Statements - Fatal编程技术网

Bash 来自条件语句的变量

Bash 来自条件语句的变量,bash,variables,conditional-statements,Bash,Variables,Conditional Statements,我有几个使用bashshell的脚本,在条件语句中有一个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/-typ

我有几个使用bashshell的脚本,在条件语句中有一个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)
if [ -z "$RESULT" ]
then
    echo "no world writable found"
else
    echo "$RESULT has world write permissions"
fi

您只需获取输出并将其存储在变量中。如果它不是空的,您可以打印它的内容。这样,您只需要运行一次命令

RESULT=$(find / -type f -perm -002)
if [ -z "$RESULT" ]
then
    echo "no world writable found"
else
    echo "$RESULT has world write permissions"
fi
如果愿意,可以使用插入标题

REPORT=$(find /tmp -type f -perm -002 | sed '1s/^/Found world write permissions:\n/')
echo ${REPORT:-No world writable found.}
注意:您的示例似乎不正确,因为
find
可以返回多行

可以同时做到这两个方面:

find /tmp -type f -perm -002 | 
awk -- '1{print "Found world write permissions:";print};END{if(NR==0)print "No world writable found."}'
如果愿意,可以使用插入标题

REPORT=$(find /tmp -type f -perm -002 | sed '1s/^/Found world write permissions:\n/')
echo ${REPORT:-No world writable found.}
注意:您的示例似乎不正确,因为
find
可以返回多行

可以同时做到这两个方面:

find /tmp -type f -perm -002 | 
awk -- '1{print "Found world write permissions:";print};END{if(NR==0)print "No world writable found."}'

如果您不介意未找到消息
no world writable
,您可以使用单个
find
语句,仅此而已:

find / -type f -perm -002 -printf '%p has world write permissions\n'

如果需要存储返回的文件以备将来使用,请将其存储在数组中(假设为Bash):

#/bin/bash
文件=()
而IFS=read-r-d''f;做
文件+=(“$f”)
#您也可以打印此消息:
printf“%s”具有全局写入权限\n'$f“

完成<如果您不介意未找到消息
no world writable
,您可以使用单个
find
语句,仅此而已:

find / -type f -perm -002 -printf '%p has world write permissions\n'

如果需要存储返回的文件以备将来使用,请将其存储在数组中(假设为Bash):

#/bin/bash
文件=()
而IFS=read-r-d''f;做
文件+=(“$f”)
#您也可以打印此消息:
printf“%s”具有全局写入权限\n'$f“

done<将find的结果赋给变量有什么不对?
result=$(find/-type f-perm-002)
local result=$(find/-type f-perm-002)
如果包含在函数中,则是惯用的方法。注意,在原始版本中,您需要引用命令替换,因为如果它扩展到多个单词,
[
会抱怨操作数太多。这通常是一个很长的列表。管理层强调将最小更改作为首选项。因此,我只是想探索一下,可能会有一些我不知道的疯狂正则表达式,它会使用现有的内容在find命令中显示内容,而不会再次运行。分配resu有什么不对如果函数中包含find/-type f-perm-002)或
local result=$(find/-type f-perm-002)
,则find到变量的lt是惯用的方式。注意,在原始版本中,您需要引用命令替换,因为如果它扩展到多个单词,
[
会抱怨操作数太多。这通常是一个很长的列表。管理层强调将最小更改作为首选项。因此,我只是想探索一下,可能会有一些我不知道的疯狂正则表达式,它会使用存在的内容在find命令中显示内容,而不会再次运行。这很聪明。我从他那里学到了一些东西re.谢谢你的回答!这很聪明。我在这里学到了一些东西。谢谢你的回答!看了之后,这似乎是最优雅的方式。我会在这方面回击Mgmt。谢谢!看了之后,这似乎是最优雅的方式。我会在这方面回击Mgmt。谢谢!愤怒的选民关心会解释他的原因吗/她的行为?愤怒的选民关心会解释他/她的行为吗?