[英]GLOBIGNORE does not work with sudo command
I'm using a mini shell script in order to 'tail' (in real time) a bunch of log files. 我正在使用一个微型shell脚本来“尾部”(实时)一堆日志文件。
#!/bin/sh
oldGLOBIGNORE=$GLOBIGNORE
export GLOBIGNORE='foo-bar.log'
sudo -E tail -f -n0 /var/log/*.log
GLOBIGNORE=$oldGLOBIGNORE
As you can see, I want to log all files except the one named foo-bar.log
. 如您所见,我想记录
foo-bar.log
为foo-bar.log
文件以外的所有文件。
the -E
option of sudo should allow me to keep the GLOBIGNORE
variable but it looks like it does not work. sudo的
-E
选项应该允许我保留GLOBIGNORE
变量,但它似乎不起作用。
I'm testing on Ubuntu 10.04, bash 4.1.5. 我正在Ubuntu 10.04,bash 4.1.5上进行测试。
Any clue ? 有什么线索吗?
Firstly — GLOBIGNORE
relates to the full filepath resulting from filename-expansion, not just the last part. 首先
GLOBIGNORE
与文件名扩展产生的完整文件路径有关,而不仅仅是最后一部分。 So you actually want to write GLOBIGNORE='/var/log/foo-bar.log'
. 因此,您实际上想编写
GLOBIGNORE='/var/log/foo-bar.log'
。
Secondly — you don't actually need to export GLOBIGNORE
into the environment and add -E
, because the /var/log/*.log
gets expanded by Bash before it even invokes sudo
. 其次-您实际上不需要将
GLOBIGNORE
导出到环境中并添加-E
,因为/var/log/*.log
在被Bash甚至调用sudo
之前/var/log/*.log
被扩展。
Thirdly — your approach to saving the old value of GLOBIGNORE
and restoring it afterward is less than ideal, because the behavior when GLOBIGNORE
is unset is different from its behavior when it's set-but-empty, and your script can never restore it to being unset. 第三,保存
GLOBIGNORE
的旧值并在以后恢复它的方法不太理想,因为取消设置GLOBIGNORE
时的行为与GLOBIGNORE
为空但为空时的行为不同,并且脚本无法将其恢复为未设置状态。 Fortunately, the script doesn't need to restore it (since it's not as though a script's variables could continue to have effect after the script returns), so you can just remove that stuff. 幸运的是,脚本不需要还原它(因为脚本返回后好像并不是脚本的变量可以继续起作用),因此您只需删除这些内容即可。
All told, you can write: 总而言之,您可以编写:
#!/bin/sh
GLOBIGNORE=/var/log/foo-bar.log
sudo tail -f -n0 /var/log/*.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.