3 ms·
At first I was going to say that handling this properly would require shell to have magic powers. After trying to come up with a proof of why it's impossible,
by marvy 11y ago
At first I was going to say that handling this properly would require shell to have magic powers. After trying to come up with a proof of why it's impossible, I think I found a simple solution. There are two problems the author was complaining about. The first one is:
cat paper.* > paper.all
This has the potential to use up the whole disk. The fix here is easy: do globbing before redirection. But this does not solve the other problem:
cat a b > a
This has a simple workaround which is even easier to type: cat a >> b. But the trap remains. How could the shell fix it? By pretending the user wrote this instead:
cat a b > temp; mv temp a
Let's look at the trade offs. We do a bit of extra work: one rename operation. This should be pretty cheap, and solves this problem as well as paper.all.
The real trouble I think is coming up with a good name and location for tmpfile that is guaranteed not to mess up the operation of the command we're running. If we put it in the current directory, we get the wrong answer from ls. If we put it in /tmp, it will mess things up when we're working in /tmp. However, after doing a few minutes of research, it appears that this problem is already solvable on Linux: by using the O_TMPFILE flag, there is no need to give the file a name at all, until after the command finishes running.
- voyou 11y agoSometimes I start a long-running process, redirecting it's output, and periodically look at the output file while this process is running. Your proposed solution wouldn't allow for that.
- marvy 11y agoIt would be possible, but not convenient: /proc/pid/fd/1. Ick. Ok, maybe the cure is worse than the disease. I'm out of clever ideas for now.
- hackmiester 11y agoWhat if 'a' is a device?
- marvy 11y agoOuch. Don't do that for devices. Or named pipes I guess. This idea has more problems than I thought at first.