sudo internal or external to a loop?
Supposing: some set of files or other resources only accessible with temporarily elevated privileges obtained through sudo. How should a loop to iterate over them call the sudo command? There are two options: shove it inside the for loop or call the whole for loop from within a shell with elevated privs.
In this specific case I wanted to verify that a fresh RPM package had made the changes it claimed it had made.
This works fine for cases where there are only a few items to iterate through. If there are many, then it is necessary to start looking at the timeout values set for sudo.
for name in Tunnel BatchMode; do sudo grep -r $name /etc/ssh/; done
[sudo] password for ushf: /etc/ssh/sshd_config.rpmnew:#PermitTunnel no /etc/ssh/sshd_config:#PermitTunnel no /etc/ssh/ssh_config:# Tunnel no /etc/ssh/ssh_config:# TunnelDevice any:any /etc/ssh/ssh_config:# BatchMode no
Shove the whole thing into a shell
sudo sh -c 'for name in Tunnel BatchMode; do grep -r $name /etc/ssh/; done' [sudo] password for ushf: /etc/ssh/sshd_config.rpmnew:#PermitTunnel no /etc/ssh/sshd_config:#PermitTunnel no /etc/ssh/ssh_config:# Tunnel no /etc/ssh/ssh_config:# TunnelDevice any:any /etc/ssh/ssh_config:# BatchMode no