robespierre wrote:
this is a classic "led down the garden path" situation, which you do need to have some programming ability to notice.
you concluded that "It's something stupid related to pipes and processes"; in other words, that the statement "x=1" was not affecting the value of x, because it (surprisingly) executes in a different process. this is an unwarranted assumption, since it might not have affected x simply by never executing at all.
It might be an unwarranted assumption, except for the fact that I tested this pretty carefully years ago, and then again recently.
robespierre wrote:
Code:
Select all
x=0
echo onetime | while read line; do
touch quux
done
ls quux
ls: quux: No such file or directory
for those who "do a lot of shell scripting" and are led astray by such basic mistakes, the greater danger may not be their choice of shell, but letting them near computers to begin with.
Sorry, but your script results are wrong using all the shells I'm talking about. All of them create the file quux, and then list it successfully, as you can see here:
Code:
Select all
$ cat > quux.sh
echo onetime | while read line; do
touch quux
done
ls quux
$ dash quux.sh
quux
$ ksh93 quux.sh
quux
$ mksh quux.sh
quux
$ bash quux.sh
quux
So you posted bogus results, and then sneered that I shouldn't even be allowed near a computer? Okay... if you want an even simpler example:
Code:
Select all
$ cat > inloop.sh
echo onetime | while read line; do echo inloop; done
$ for shell in dash ksh93 mksh bash; do $shell inloop.sh; done
inloop
inloop
inloop
inloop
The shell loops work just fine.
robespierre wrote:
Code:
Select all
echo foo bar | read line; echo $line
is a newline, because line is empty.
Yeah, that's why the example scripts I gave never piped to "read", but rather to "while read", like this:
Code:
Select all
$ echo foo bar | while read line; do echo $line; done
foo bar
So you didn't bother to notice the difference, yet you're the one with the "real programming ability" who is able to spot these things. Yeah, okay...
One last script to illustrate the incompatibility:
Code:
Select all
$ cat > inloop2.sh
x=1
echo onetime | while read line; do
x=2
echo "inloop: $x"
done
echo "endloop: $x"
$ ksh93 inloop2.sh
inloop: 2
endloop: 2
$ mksh inloop2.sh
inloop: 2
endloop: 1
I'm not some big shell scripting guru, but I'm also not a bumbling idiot who just makes this stuff up because I just lack "real programming ability." I ran into this particular problem because real shell scripts I was writing for HP-UX servers would fail in Cygwin because pdksh and mksh are not faithful ksh88 clones. This sort of incompatibility is dangerous because there is no indication of it other than getting the wrong (old) variable values. Other people have run into it as well, usually when they try to migrate their ksh88 scripts to Linux, and then run into all sorts of errors.