LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   a little puzzle (https://www.linuxquestions.org/questions/slackware-14/a-little-puzzle-861821/)

Ramurd 02-10-2011 08:13 AM

a little puzzle
 
I was tinkering a little bit and I noticed something strange. I admit that I don't know the answer to this riddle (yet), but more than willing to let you shine your brilliant lights:

When I am in a terminal and I type

Code:

while true
do
ps -ef | wc -l
sleep 1
done

The result is what one'd think: the total of all processes +1 (header) (let's say 180)

Now where I got tinkering:
Code:

while true
do
$0 -c ps -ef | wc -l
sleep 1
done

The result here is 4

So the big question is: why can't ps "see" all processes when called within a separate shell?

Exercises to be done:
what does it do within a script that calls a new shell (she-banged)
Is this behaviour the same when doing this little joke as root.

But first, can anyone solve this little riddle?

brixtoncalling 02-10-2011 08:28 AM

The arguments -ef are not being passed to ps probably. Put "ps -ef" instead and it'll work. I'd guess that -ef are getting sent to bash...

Ramurd 02-10-2011 08:44 AM

You're correct, was searching myself in a totally different scope thing (thinking that the newly called shell was the reason for this behaviour.

Fun though ;-)

Code:

while true
do
$0 -c "ps -ef" | wc -l
sleep 1
done

does report the 180 processes again. Good one to remember when calling a shell this way to put quotes around the parameters. Often I do some tinkering like this with no real goal in mind. Just a curiosity of "what'd happen if I do something like this?". Makes for some nice learning moments.

brixtoncalling 02-10-2011 09:04 AM

hey I was more surprised that you were calling a new shell with $0. I always forget about the magic $ variables in bash!

Ramurd 02-10-2011 09:10 AM

I was actually looking at what'd happen with the $0 variable when I was calling it like this: in the do-while-loop. As predicted: it remained the shell's shell.

If you want it even more funny, I think this is possible in a script:

#!`which $0`

Up to you to predict what'll happen ;-) (actually, it's something worth tinkering about: as it'll bea script calling itself... FUN!)

brixtoncalling 02-10-2011 09:39 AM

Quote:

Originally Posted by Ramurd (Post 4254178)
I was actually looking at what'd happen with the $0 variable when I was calling it like this: in the do-while-loop. As predicted: it remained the shell's shell.

If you want it even more funny, I think this is possible in a script:

#!`which $0`

Up to you to predict what'll happen ;-) (actually, it's something worth tinkering about: as it'll bea script calling itself... FUN!)

I've no idea if that hash-bang will work, but won't actually be a script calling itself I'd say ... but I'm going to careful about testing any of your scripts on my machine because there's clearly some highly experimental stuff going on over there in Rotterdam;)

dive 02-10-2011 10:56 AM

Looks like a forkbomb, but I won't test it to find out ;)

sinic 02-10-2011 12:21 PM

This won't work; the characters following the shebang are interpreted by the kernel, not by the shell. That's also the reason why you always have to specify the whole path to an executable up there.


All times are GMT -5. The time now is 06:02 PM.