Backtick and %x{} not the same?

I can't any information about this, so I dare myself to post here. I have shell script I want to call from ruby, say testme.sh, where it contains

echo "It's me"

Calling it with %x{testme.sh} gives me the expected result, but calling it with backtick `testme.sh` results in 'command not found: testme.sh'.

The backtick works if I put double quotes around it, ie `"testme.sh"`, or if I use shebang in the shell script
#!/bin/sh
echo "It's me"

Why is this? By the way `testme.sh` works in perl which made me expect it to work in ruby too (I know, I know, I shouldn't expect that).

Thanks in advance

-andre

···

_________________________________________________________________
All-in-one security and maintenance for your PC. Get a free 90-day trial! http://clk.atdmt.com/MSN/go/msnnkwlo0050000002msn/direct/01/?href=http://www.windowsonecare.com/?sc_cid=msn_hotmail

IMHO without #! all bets are off. AFAIK every shell tries to execute a script itself if it cannot be exec'ed as a binary. This can lead to problems if a csh tries to execute a sh script or vice versa. So basically you should *always* use the shebang line to make clear which interpreter is supposed to execute a script - even if "it works without" and you do it only for documentation reasons. If adding the shebang line fixes it I'd just do that and not bother any more. My 0.02EUR

Kind regards

    robert

···

Andreas S <andreas_s@hotmail.com> wrote:

I can't any information about this, so I dare myself to post here. I
have shell script I want to call from ruby, say testme.sh, where it
contains
echo "It's me"

Calling it with %x{testme.sh} gives me the expected result, but
calling it with backtick `testme.sh` results in 'command not found:
testme.sh'.
The backtick works if I put double quotes around it, ie
`"testme.sh"`, or if I use shebang in the shell script
#!/bin/sh
echo "It's me"

Why is this? By the way `testme.sh` works in perl which made me
expect it to work in ruby too (I know, I know, I shouldn't expect
that).

Isn't it better to use an absolute pathname, rather than use a bare
filename and making assumptions what $PATH is going to be? In other
languages this is certainly a consideration, so I fancy the rule might
apply to Ruby. m.

···

Andreas S <andreas_s@hotmail.com> wrote:

backtick `testme.sh` results in 'command not found: testme.sh'.

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

From: "Robert Klemme" <shortcutter@googlemail.com>

So basically you should *always* use the shebang line to make clear which interpreter is supposed to execute a script - even if "it works without" and you do it only for documentation reasons.

Just seconds after I posted I found I made a fool of myself. %x{testme.sh} doesn't work as well.

You're point is well taken and I must agree with you. However, I'm also curious about what's going on under the hood. What does ruby do when I use double quote in backtick why it runs the command while without double quote it doesn't? (Sometimes I made my life harder by not knowing where to stop and keep moving along)

-andre

···

_________________________________________________________________
Try the next generation of search with Windows Live Search today! http://imagine-windowslive.com/minisites/searchlaunch/?locale=en-us&source=hmtagline

From: matt@tidbits.com (matt neuburg)

Isn't it better to use an absolute pathname, rather than use a bare
filename and making assumptions what $PATH is going to be?

I tried that and it doesn't work either. Apparently the issue wasn't search path. Logan explained what has happened there (although I'm not sure what he meant by ruby thinks, "I can handle this myself". How?)

I was expecting system call would result exactly the same as if the command was executed in the shell itself. If there's a problem with the executed command, I would've thought I'd be able to replicate it from the shell. 'command not found' was a rather puzzling error message for me.

I don't know ruby in and out, but I'm sure there is a good reason ruby does this.

-andre

···

_________________________________________________________________
All-in-one security and maintenance for your PC. Get a free 90-day trial! http://clk.atdmt.com/MSN/go/msnnkwlo0050000002msn/direct/01/?href=http://www.windowsonecare.com/?sc_cid=msn_hotmail

>From: "Robert Klemme" <shortcutter@googlemail.com>
>
>So basically you should *always* use the shebang line to make clear which
>interpreter is supposed to execute a script - even if "it works without"
>and you do it only for documentation reasons.
>
Just seconds after I posted I found I made a fool of myself. %x{testme.sh}
doesn't work as well.

You're point is well taken and I must agree with you. However, I'm also
curious about what's going on under the hood. What does ruby do when I use
double quote in backtick why it runs the command while without double quote
it doesn't? (Sometimes I made my life harder by not knowing where to stop
and keep moving along)

`simple` # Ruby thinks, I can handle this myself
`"simple"` # Some extra chars in there huh, I'd better start a shell and
let it parse this craziness

The side-effect being of course that the shell decides to run it as a
shell script, or whatever.

···

On Mon, Oct 30, 2006 at 03:56:02AM +0900, Andreas S wrote:

-andre

_________________________________________________________________
Try the next generation of search with Windows Live Search today!
http://imagine-windowslive.com/minisites/searchlaunch/?locale=en-us&source=hmtagline

You can always be more explicit, of course, and help it find the command:

    `sh testme.sh`

or

    $sh = ['/bin/bash','/bin/sh'].find {|x| File.executable? x}
    `$sh testme.sh`

···

--
Lou.

>From: matt@tidbits.com (matt neuburg)
>
>Isn't it better to use an absolute pathname, rather than use a bare
>filename and making assumptions what $PATH is going to be?

I tried that and it doesn't work either. Apparently the issue wasn't search
path. Logan explained what has happened there (although I'm not sure what
he meant by ruby thinks, "I can handle this myself". How?)

It does a straight fork+exec instead of invoking a shell interpreter

···

On Mon, Oct 30, 2006 at 12:40:16PM +0900, Andreas S wrote:

I was expecting system call would result exactly the same as if the command
was executed in the shell itself. If there's a problem with the executed
command, I would've thought I'd be able to replicate it from the shell.
'command not found' was a rather puzzling error message for me.

I don't know ruby in and out, but I'm sure there is a good reason ruby does
this.

-andre

_________________________________________________________________
All-in-one security and maintenance for your PC. Get a free 90-day trial!
http://clk.atdmt.com/MSN/go/msnnkwlo0050000002msn/direct/01/?href=http://www.windowsonecare.com/?sc_cid=msn_hotmail