slightly tricky way of capturing additions to a logfile

D. Hugh Redelmeier hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org
Tue Aug 13 19:18:08 UTC 2013


| From: Randy Jonasz <rjonasz-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org>

| On Tue, Aug 13, 2013 at 1:03 AM, D. Hugh Redelmeier <hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org> wrote:

| > [root at redcherry-mimosa-com hugh]# exec {log}</var/log/messages
| > [root at redcherry-mimosa-com hugh]# cat <&${log} >/dev/null
| > [root at redcherry-mimosa-com hugh]# cat <&${log}
| > [root at redcherry-mimosa-com hugh]# os-prober
| > /dev/sda1:FreeDOS:FreeDOS:chain
| > [root at redcherry-mimosa-com hugh]# cat <&${log}
| 
| Interesting.  But I get the following error when trying these commands:
| 
| root at Nietzsche:/home/rjonasz# exec &{log}< /var/log/syslog
| [1] 5980
| [1]+  Done                    exec

You put in an & which changes everything.  It forked a new process!
And it fails to initialize $log.

| root at Nietzsche:/home/rjonasz# cat <&${log} >/dev/null
| bash: ${log}: ambiguous redirect
| 
| [1]+  Stopped                 cat

You now reference an undefined shell variable (log), so this expands
to
	cat <& >/dev/null
Which not only doesn't do what you want, but forks again.

| root at Nietzsche:/home/rjonasz# cat <&"${log}" >/dev/null
| bash: "${log}": Bad file descriptor

$log is still not defined.

Interesting how the quoting changed the shell's interpretation.


Lessons:

- it is hard to get unfamiliar and tricky notation exactly right

- the shell doesn't give diagnostics that are clear to mortals

- "set -u" ought to be the default.  With that option, references to
  undefined shell variables are treated as errors rather than being
  expanded to the empty string.
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list