In bash, I notice that if a command using redirection would fail, any programs which run prior to that are not run.
For example, this program opens the file "a" and writes 50 bytes to file "a". However, running this command with redirection to a file with insufficient permissions (~root/log), yields no change in file size of "a".
$ ./write_file.py >> ~root/log
-bash: /var/root/log: Permission denied
cdal at Mac in ~/experimental/unix_write
$ ls -lt
total 16
-rw-rw-r-- 1 cdal staff 0 Apr 27 08:54 a <-- SHOULD BE 50 BYTES
One would think the program would run, capture any output (but also write to the file "a"), and then fail to write any output to ~root/log. Instead the program is never run.
Why is this, and how does bash choose the order of the "checks" it performs prior to executing a program? Are other checks performed as well?
p.s. I'm trying to determine whether a program run under cron actually ran when redirected to a "permission denied" file.
stdout
to do exactly that. So, you will not see any output, even though your program ran.write_file.py
program and send its output to~root/log
bash: "Sorry, but you're not allowed to write to that file!" The shell is doing exactly what it should do. If it can't do what you asked it to do, it immediately informs you why there's a problem, giving you the opportunity to decide how to deal with it. For all the bash maintainers know, Very Bad Things could happen if you run that command and don't get to save the output. If it was important enough you designated a place to save it, it would be wrong to ASS|U|ME it was OK to run without saving stdout.