Weird things in csh (and kernel?)

Patrick Barron pdb at sei.cmu.edu
Sat Feb 27 17:41:09 AEST 1988


In article <1193 at ark.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
>Has anyone noticed the following oddities?
>1)
>	% a.out < a.out
>	a.out: Text file busy.
>	%
>Why shouldn't a process be able to read its text file?

   I can't reproduce this under Ultrix 1.2.

>2)
>	% cat ~/.cshrc
>	echo echo hello
>	% cp /bin/echo .
>	% ./echo > echo
>	hello: Command not found.
>	% cat echo
>	echo hello
>	hello
>	%
>
>What kinda weird thing is going on here?
>Shouldn't there be an error message "Text file busy."
>in this case?

This is actually pretty simple.  When you redirect output to "echo",
the file gets overwritten *before* "./echo" is executed.  So, it ends
up trying to execute an empty file.  Since it doesn't have a valid
magic number, and the execute bits are set (copying /bin/echo left
them set, and overwriting the file didn't change that), it's assumed
to be a shell script.  Your .cshrc file gets executed, echoing
"echo hello" using the C-shell's built-in echo.  That goes into
the file "./echo", and the shell then starts to execute commands from
"./echo".  (Why isn't the Bourne shell used to do this?  The file does
not start with "#!/bin/csh")  That file now contains "echo hello", so
the shell's "echo" echoes "hello".  Remember, standard output is still
the "./echo" file, so that gets tacked on to the end.  The shell then
reads the next command, which is "hello", and it complains "Command not
found."

--Pat.



More information about the Comp.unix.wizards mailing list