On 03 Aug 08, at 23:15, Steve Cronin wrote:
OK for the sake of the group I will parade my ignorance so that perhaps others can learn too...

First I do understand your point that using AppleScript is using an unnecessary high-level tool when a lower-level tool will do! Thanks for keeping me on-point there!! Good old-school unix thinking - always a good thing.

Do note Michael Ash's email noting that exec'ing a GUI app is generally to be avoided. Just because it's the most straightforward approach (to me, at least) doesn't mean it's correct. :)

I have 4 questions
1) Given that it is higher-level than necessary, I would still appreciate understanding why it doesn't work. I revamped the '&' and added it to the end of the osaxscript command instead of passing as argument -> no change..

The & is only interpreted by shells, as part of a command line entered into them (either on standard input, or through a shell script). Passing an ampersand as an argument acts as though you entered "&" (with quotes) as an argument - at best, it might be treated as a file called &. Passing this as an argument to a shell is no exception, either. It's only when it's part of a command being interpreted by the shell that it gains any special meaning.

2) In the script you cited, I don't understand the 'if(fork() || fork())' conditional test. Can you clarify this statement?

Keep in mind that fork() is called once and returns twice - once in the parent process, where it returns the PID of the child process, and once in the child process, where it returns 0.

As such, if(fork() || fork()) exit(0); behaves as follows:

FIRST PROCESS:
  * calls the first fork(), creating SECOND PROCESS
  * ... gets PID of SECOND PROCESS back, which evaluates as true
  * short-circuits the second fork() and runs exit(0)

SECOND PROCESS:
  * ... gets 0 back from first fork()
  * doesn't fall through, and calls the second fork()
* ... gets pid of THIRD PROCESS back, which again evaluates as true, etc

THIRD PROCESS:
  * ... gets 0 back from second fork()
* both fork()s returned 0 on this path, so it continues merrily on its way!

The crux of this trick is the way it uses boolean short-circuiting: if the first clause of a boolean OR statement (x || y) is true, the second one isn't even evaluated, as the answer will be true either way. Similarly, if the first clause of an AND statement is false, the second one isn't evaluated.


3) Why are you using 'execl' and not 'exec'?

There's actually no straight exec() function - there's just a bunch of variants like execv, execl, execlp, and a few others. They all funnel into the execve() syscall, so I picked execl because it's easier to work with than many of the others. :) (All of the members of the execv* family require you to construct an array of arguments, whereas the execl* family lets you simply pass multiple arguments to the function.)

4) When you say "...launch this with a single argument..." I' not sure of exactly what you mean. Launch how?

I tried creating a text file with your script as the contents (Restart.unixscript)

I don't believe I said anything of the sort. The "script" I gave you was a short C program that'd act as a helper - it definitely won't run as a shell script. (No, not even in csh... heh.) You'd have to compile it, then run it as a task with a single argument.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to