LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 02-10-2011, 08:13 AM   #1
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 538

Rep: Reputation: 74
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?
 
Old 02-10-2011, 08:28 AM   #2
brixtoncalling
Member
 
Registered: Jul 2008
Location: British Columbia
Distribution: Slackware current
Posts: 403

Rep: Reputation: 67
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...
 
1 members found this post helpful.
Old 02-10-2011, 08:44 AM   #3
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 538

Original Poster
Rep: Reputation: 74
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.
 
Old 02-10-2011, 09:04 AM   #4
brixtoncalling
Member
 
Registered: Jul 2008
Location: British Columbia
Distribution: Slackware current
Posts: 403

Rep: Reputation: 67
hey I was more surprised that you were calling a new shell with $0. I always forget about the magic $ variables in bash!
 
Old 02-10-2011, 09:10 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 538

Original Poster
Rep: Reputation: 74
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!)
 
Old 02-10-2011, 09:39 AM   #6
brixtoncalling
Member
 
Registered: Jul 2008
Location: British Columbia
Distribution: Slackware current
Posts: 403

Rep: Reputation: 67
Quote:
Originally Posted by Ramurd View Post
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
 
Old 02-10-2011, 10:56 AM   #7
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,199

Rep: Reputation: 290Reputation: 290Reputation: 290
Looks like a forkbomb, but I won't test it to find out
 
Old 02-10-2011, 12:21 PM   #8
sinic
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 4
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming puzzle smeezekitty General 6 11-23-2009 01:23 AM
egrep puzzle dazdaz Solaris / OpenSolaris 4 01-22-2008 04:15 AM
A puzzle Gins Linux - General 10 02-24-2007 07:23 AM
GTK puzzle Chuck23 Fedora 9 08-24-2004 03:04 AM
Simple C Puzzle sansk Programming 20 05-22-2004 04:09 PM


All times are GMT -5. The time now is 03:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration