I am kinda confused with this, am not sure what is happening
i have a script say test.sh
----------
cat myfile | while read line
do
exit 2
done
echo "out of loop"
-----------
as it is evident, the exit should cause the script to terminate ,hence
producing no output for the script.
now when i run the script as "./test.sh", the behaviour is as
expected.
but when i run it with nohup as "nohup ./test.sh &", the "out of loop"
statement appears in the output, indicating that the exit just gets it
outside the loop but does not exit the program.
any ideas on this ?
How odd. None of the shells I've tried have exited the shell from
inside the loop.
What's happening is that the `while' loop is being run in a subprocess;
so `exit' is exiting that subshell. You can verify this by printing
`$?' in the final `echo'. The POSIX spec allows this behaviour (IEEE
1003.1 Shell and Utilities 2.12):
: Additionally, each command of a multi-command pipeline is in a
: subshell environment; as an extension, however, any or all commands in
: a pipeline may be executed in the current environment.
> any ideas on this ?
My guess is that you're using different shells in the two cases.
-- [mdw]
1. dawood-ss-macbook:junk dawood$ nohup ./try.sh &
2. [1] 438
3. dawood-ss-macbook:junk dawood$ appending output to nohup.out
on line 2, it displays job id([1]) and pid (438). you get prompt
immediately as on line 3. now shell is waiting to get next input. if you
want prompt again hit return. :)
The OP could rewrite the loop as :
while read line
do
exit 2
done <myfile
That would not only get rid of the pipe, but also avoid creating
an unnecessary cat process.
--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
"No meetings were held, nor secret handshakes created, to allow a Seria-
list Elite to disenfranchise the (tonal) non conformist. None had to be."
-- Wendy Carlos
Thanks all for your comments.
Well the concept behind nohup is pretty much clear, but i am still not
able to get through with the problem
I check that i have the same shell (type) in/out of the while loop.
tried removing cat myfile | with the > operator, but that didn't help
either
works the same way as cat when using nohup
Is there a more elegant/different way of reading a file line by line
which would problably do it in this case ?
You could always switch to awk. But exit inside a while loop is
supposed to work. What's the output of this ?
cat <<\EOF >/tmp/truc
#!/bin/sh
echo "before (\$0 = '$0', \$\$ = '$$')"
ps | grep "$$"
while read line
do
echo "during (\$0 = '$0', \$\$ = '$$')"
ps | grep "$$"
exit 2
done </etc/resolv.conf
echo "after (\$0 = '$0', \$\$ = '$$')"
ps | grep "$$"
EOF
chmod +x /tmp/truc
nohup /tmp/truc &
echo '$!' = $!
>>> while read line
>>> do
>>> exit 2
>>> done <myfile
>>
>> tried removing cat myfile | with the > operator, but that didn't help
> But exit inside a while loop is supposed to work. [...]
There is an exception for traditional Bourne shells: redirecting
loops and other control structures also results in a subshell.
You can work around this by putting the structure into a function,
or with a global redirection,
exec 5<&0 <myfile
while read line [...]
done
exec <&5 5<&\-
--
http://code.dogmap.org./lintsh/
http://www.in-ulm.de/~mascheck/bourne/common.html
What do you want to do with each line? If the answer is to modify it
and write it to a file or aggregate it or otherwise to manipulation on
the text, then use sed/awk/perl/... but if the answer is to find a
file named based on that line, or do something else related to file or
process manipulation then stick to a shell script.
Ed.