Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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
Unix 在RHEL 7上使用bash shell-为什么在我不使用';不使用-exec?_Unix_Gnu Findutils_Find Util - Fatal编程技术网

Unix 在RHEL 7上使用bash shell-为什么在我不使用';不使用-exec?

Unix 在RHEL 7上使用bash shell-为什么在我不使用';不使用-exec?,unix,gnu-findutils,find-util,Unix,Gnu Findutils,Find Util,我正在查找所有setuid/setgid文件。 当我不使用-exec时,它会按预期工作: # find /usr/bin -type f -perm -4000 -o -perm -2000 /usr/bin/wall /usr/bin/ksu /usr/bin/chage /usr/bin/chfn /usr/bin/chsh /usr/bin/fusermount /usr/bin/passwd /usr/bin/write /usr/bin/su /usr/bin/umount /usr

我正在查找所有setuid/setgid文件。 当我不使用
-exec
时,它会按预期工作:

# find /usr/bin -type f -perm -4000 -o -perm -2000 
/usr/bin/wall
/usr/bin/ksu
/usr/bin/chage
/usr/bin/chfn
/usr/bin/chsh
/usr/bin/fusermount
/usr/bin/passwd
/usr/bin/write
/usr/bin/su
/usr/bin/umount
/usr/bin/gpasswd
/usr/bin/newgrp
/usr/bin/mount
/usr/bin/pkexec
/usr/bin/crontab
/usr/bin/cgclassify
/usr/bin/cgexec
/usr/bin/ssh-agent
/usr/bin/Xorg
/usr/bin/at
/usr/bin/sudo
/usr/bin/locate
/usr/bin/staprun
当我使用
-exec
时,我只得到结果的一个子集:

# find /usr/bin -type f -perm -4000 -o -perm -2000 -exec ls -l {} \;
-r-xr-sr-x. 1 root tty 15344 Jan 27  2014 /usr/bin/wall
-rwxr-sr-x. 1 root tty 19536 Aug 21  2015 /usr/bin/write
-rwxr-sr-x. 1 root cgred 15624 Sep 21  2014 /usr/bin/cgclassify
-rwxr-sr-x. 1 root cgred 15584 Sep 21  2014 /usr/bin/cgexec
---x--s--x. 1 root nobody 306304 Sep 24  2015 /usr/bin/ssh-agent
-rwx--s--x. 1 root slocate 40504 Jan 26  2014 /usr/bin/locate

为什么?

您只在
-o
的右侧使用
-exec
。因此,它被解析为:

# What's actually happening
find /usr/bin '(' -type f -perm -4000 ')' -o '(' -perm -2000 -exec ls -l {} \; ')'
显然,这不是你想要的


要使其适用于条件的两侧,请添加一些用于分组的参数:

# What you want to happen
find /usr/bin -type f '(' -perm -4000 -o -perm -2000 ')' -exec ls -l {} +

原因是,如果不指定显式操作,
find
将假定
-print
作为默认操作。当您自己添加一个动作时,它会关闭该默认值,因此只有您显式指定动作的项才能获得一个动作

也就是说:

# These commands are all equivalent:
find /usr/bin -type f -perm -4000 -o -perm -2000
find /usr/bin '(' -type f -perm -4000 -o -perm -2000 ')' -print
find /usr/bin '(' '(' -type f -perm -4000 ')' -o '(' -perm -2000 ')' ')' -print
请注意最后一个,它在默认行为中暴露了一个警告:您可能希望
-type f
应用于
-o
的两侧,但是如果没有显式分组,它会被放在左侧,就像显式
-exec
打印
被放在右侧一样


这个故事的寓意是:在find中使用
-o
时,要明确分组,除非您非常确定默认行为是您想要的。

您只在
-o
的右侧使用
-exec
。因此,它被解析为:

# What's actually happening
find /usr/bin '(' -type f -perm -4000 ')' -o '(' -perm -2000 -exec ls -l {} \; ')'
显然,这不是你想要的


要使其适用于条件的两侧,请添加一些用于分组的参数:

# What you want to happen
find /usr/bin -type f '(' -perm -4000 -o -perm -2000 ')' -exec ls -l {} +

原因是,如果不指定显式操作,
find
将假定
-print
作为默认操作。当您自己添加一个动作时,它会关闭该默认值,因此只有您显式指定动作的项才能获得一个动作

也就是说:

# These commands are all equivalent:
find /usr/bin -type f -perm -4000 -o -perm -2000
find /usr/bin '(' -type f -perm -4000 -o -perm -2000 ')' -print
find /usr/bin '(' '(' -type f -perm -4000 ')' -o '(' -perm -2000 ')' ')' -print
请注意最后一个,它在默认行为中暴露了一个警告:您可能希望
-type f
应用于
-o
的两侧,但是如果没有显式分组,它会被放在左侧,就像显式
-exec
打印
被放在右侧一样


这个故事的寓意是:在find中使用
-o
时,要明确分组,除非你非常确定默认行为是你想要的。

最后你会有与显式
-print
完全相同的行为;
-exec
没有什么特别之处。顺便说一句,
find
在任何方面都不是
bash
的一部分。您可以从Python或C开始<代码>查找< /C>,即使没有安装在系统上的BASH,它也会以同样的方式运行……另一方面,考虑<代码> -Exc++…代码>而不是
exec…\,只要有可能——前者(自2006年起成为POSIX标准的一部分)向每次调用传递尽可能多的参数,从而最大限度地减少运行的外部命令数量和相同调用的性能开销;
-exec
没有什么特别之处。顺便说一句,
find
在任何方面都不是
bash
的一部分。您可以从Python或C开始<代码>查找< /C>,即使没有安装在系统上的BASH,它也会以同样的方式运行……另一方面,考虑<代码> -Exc++…代码>而不是
exec…\
,只要可能——前者(自2006年起成为POSIX标准的一部分)将尽可能多的参数传递给每个调用,从而将运行的外部命令数量和相同调用的性能开销降至最低。