0

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 out 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 out

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%
3
  • Please add the result of running file sample to your question. (The file command tells us what type of file sample is, including some potentially interesting characteristics. You can read more about this command with man file.) Commented Jul 29, 2023 at 10:02
  • Show is what you did. If you did something wrong, then we need to see what you did. Don't recreate from memory. Do it again, and document it in the question (paste from what you did). Commented Jul 29, 2023 at 10:04
  • Thank you for your time. I have updated the question with your requested information at the bottom. I re-ran both sessions and added the file command. I couldn't copy+paste because I'm in a VM and haven't figured out how to make the windows host share the clipboard with the FreeBSD guest yet. Screenshotting the guest also does not appear to work. Commented Jul 29, 2023 at 17:51

1 Answer 1

0

I would say your text book has an error in the script. I changed the script like this, and it worked then:

% cat > sample
date
echo "Hello, world!"
# Press Ctrl-D (End Of File) here to stop entering data into file sample
% /bin/sh
% exec < sample
Thu Jul 31 18:41:38 PKT 2014
Hello, world!

Regarding you second question, I do not fully understand what exactly the question requires you to do, but I tried your and textbook provided solution. Both wrote results from executed command from data into out file. So, I would say, both are correct.

2
  • You tried ` $ exec < data > out` and got the output of the commands in data in the out file? Just being pedantic so I don't misunderstand. Thank you for your help. Commented Jul 29, 2023 at 17:52
  • 1
    Yes. I tried your solution and got output of command from data in file out.
    – nobody
    Commented Jul 30, 2023 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .