简体   繁体   English

在什么情况下,文件无法在bash中工作?

[英]In what scenarios does file globbing not work in bash?

If I run sudo du -sh <some_directory>/* on a directory which requires root access it works and lists out the total sizes for all directories in . 如果我在需要root用户访问权限的目录上运行sudo du -sh <some_directory>/* ,它将起作用并列出中所有目录的总大小。 However I have found at least one scenario in which this does not happen: 但是,我发现至少有一种情况不会发生这种情况:

sudo du -sh /var/lib/docker/*

http://tldp.org/LDP/abs/html/globbingref.html doesn't mention any scenarios in which this wouldn't be expected to work. http://tldp.org/LDP/abs/html/globbingref.html没有提到任何无法预期的情况。 Does anyone have any ideas why this might not work? 有谁知道为什么这可能行不通? Is it related to Docker or du or is it wholly related to globbing. 它与Docker或du还是与全球性相关。

The comments already mentioned it in brief, but to have a decent answer on the question and a more detailed explanation: 评论已经简短地提到了它,但是对于这个问题有一个不错的答案,并且有更详细的解释:

In the command 在命令中

sudo du -sh /var/lib/docker/*

The globbing you are performing takes place before the sudo is executed. 正在执行的glob发生执行sudo 之前 Only the call of du is done with root permissions. 只有root权限才能完成du的调用。 That means if the directory /var/lib/docker/ is restricted for you as a normal user and you cannot read it (missing r permissions), then the globbing asterisk will not evaluate to anything. 这意味着如果您以普通用户的身份限制目录/var/lib/docker/ ,而您却无法读取它(缺少r权限),那么星号不会评估任何内容。 Default setting in the bash then is to leave it unchanged, so the string remains /var/lib/docker/* . bash中的默认设置是使其保持不变,因此该字符串仍为/var/lib/docker/*

Then the arguments du , -sh , and /var/lib/docker/* are passed to sudo which then executes du with root permissions and passes the arguments -sh and /var/lib/docker/* . 然后将参数du-sh/var/lib/docker/*传递给sudo ,然后以root权限执行du并传递参数-sh/var/lib/docker/* du then tries to find a file with exactly this name and will probably find nothing because no file in there is named * . 然后du尝试查找具有该名称的文件,并且可能什么也找不到,因为其中没有文件名为*

Do achieve what you want you need to make the globbing be done with root permissions also. 确实实现所需的目标,也需要使用root权限来完成通配。 For this you need to start a shell (only shells do the globbing) with root permissions: 为此,您需要使用root用户权限启动一个shell(只有shell可以进行globbing):

sudo bash -c 'du -sh /var/lib/docker/*'

This way, the arguments bash , -c and du -sh /var/lib/docker/* are passed to the command sudo . 这样,参数bash-cdu -sh /var/lib/docker/*传递给命令sudo Then sudo starts the bash with root permissions and passes the commands -c , du -sh /var/lib/docker/* . 然后sudo以root权限启动bash并传递命令-cdu -sh /var/lib/docker/* Then the bash understands because of the -c options that is supposed to evaluate and execute the command du -sh /var/lib/docker/* . 然后, bash理解了-c选项,该选项应该评估并执行命令du -sh /var/lib/docker/* It then splits the command by spaces into the "words" du , -sh , and /var/lib/docker/* . 然后,将命令按空格-sh为“单词” du-sh/var/lib/docker/* Now it performs any necessary globbing expansion (with root permissions, so it is allowed to read the contents of the directory /var/lib/docker/ ) on each of the words. 现在,它对每个单词执行任何必要的全局扩展(具有root权限,因此允许读取目录/var/lib/docker/ )。 It will replace the last word by /var/lib/docker/aufs , /var/lib/docker/builder , /var/lib/docker/buildkit , and several more. 它将用/var/lib/docker/aufs/var/lib/docker/builder/var/lib/docker/buildkit替换最后一个单词。 As a last step it will call du with -sh and the result of the globbing expansion. 最后一步,它将使用-sh和globlob扩展的结果调用du Root permissions will be inherited for this, so du then also runs with them. 根权限将为此继承,因此du也将与它们一起运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM