![从 sudo 运行 Bash 表达式](https://linux22.com/image/82673/%E4%BB%8E%20sudo%20%E8%BF%90%E8%A1%8C%20Bash%20%E8%A1%A8%E8%BE%BE%E5%BC%8F.png)
我可以列出我最近的 apt-get 历史记录:-
zcat /var/log/apt/history.log.1.gz
我正在尝试对所有历史文件自动执行此操作。
我试过:
sudo for logf in $( /var/log/apt/history.log.?.gz ); do zcat $logf ; done
但出现错误-bash: syntax error near unexpected token do
for logf in $( /var/log/apt/history.log.?.gz ); do zcat $logf ; done
但出现错误-bash: /var/log/apt/history.log.1.gz: Permission denied
for logf in $( /var/log/apt/history.log.?.gz ); do sudo zcat $logf ; done
但出现错误-bash: /var/log/apt/history.log.1.gz: Permission denied
答案1
sudo
仅适用于二进制文件,不适用于 shell 命令结构。您必须使用 运行整个过程sudo
。在循环中多次调用没有多大意义sudo
。但它可能会缓存密码。
sudo bash -c 'for logf in /var/log/apt/history.log.?.gz; do zcat "$logf"; done'