I'm working through a textbook on UNIX system administration. Chapter 3 of "Unix Third Edition" by a Syed Mansoor.
Chapter 13 has the following example of exec usage which is straightforward enough and does what I expect.
% /bin/csh % exec date Thu Jul 31 18:16:01 PKT 2014
However, later in the chapter, it has this example:
% cat sample date echo "Hello, world!" % /bin/sh % exec < sample Thu Jul 31 18:41:38 PKT 2014 Hello, world!
However when I try this, this is what I get:
$ exec < sample $ $
That's not a typo, that's what my output looks like. I don't understand why there is a discrepancy here. I am running this on PC-BSD. The explanation in the chapter is that this command redirects the input of the current shell from stdin to be from the file instead. So it should run those commands and print their output to stdout, which is still the shell. I have also tried
exec < sample > /dev/tty
as a sanity check (I think), which I understand to mean direct file to input of current shell and output to the default terminal, which is the one I'm using. However, I get the same result.
Finally, the following question appears at the end of the section, and although the answers are available on his github, I don't quite understand why my attempt didn't work. I do understand why the solution works, I think. The question:
Write a command for changing stdin of your shell to a file called data and stdout to a file called out, both in your present working directory. If the data file contains the following lines, what happens after the commands are executed?
echo –n "The time now is: " date echo –n "The users presently logged on are: " who
My attempted solution was:
$ exec < data > out
which I understand to mean, direct the file
data to input of the shell and direct output of the shell to the file
The solution is
( exec > out ; exec ./data )
which I understand as a grouped command, which executes in a subshell, which first directs the output of that subshell to the file
out, and which then replaces that subshell with the executable file
data who's output has been redirected to the file
So my questions are:
- Why doesn't the second example work on my machine when it works in the textbook? Using an ISO of the same system, although the author's version of PC-BSD is likely 9 years older. I have only used this VM exclusively for this textbook.
- Why doesn't my solution to the exercise work?
- Do I understand the author's solution correctly?
Thank you for your consideration.
P.S. I did
chmod u+x every executable in these examples.
Edit as requested by commenters Here is my attempt at the textbook example, from start to finish. I can't copy + paste exactly because I'm in a virtual machine, but I have typed input and output out exactly character by character and tripled-checked I have done so without error.
[tony@pcbsd-8682] ~/ch12% cat > sample date echo "Hello, world!" [tony@pcbsd-8682] ~/ch12% /bin/sh $ exec < sample $ [tony@pcbsd-8682] ~/ch12% file sample sample: ASCII text
For the exercise:
[tony@pcbsd-8682] ~/ch12/13.4d-redux% cat data echo -n "The time now is: " date echo -n "The users presently logged on are: " who [tony@pcbsd-8682] ~/ch12/13.4d-redux% file data date: ASCII text [tony@pcbsd-8682] ~/ch12/13.4d-redux% /bin/sh $ exec < data > out $ [tony@pcbsd-8682] ~/ch12/13.4d-redux%