[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

backtick and %x{} not the same?

Andreas S

10/29/2006 5:24:00 PM

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=m...


26 Answers

Robert Klemme

10/29/2006 6:40:00 PM

0

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).

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

10/29/2006 6:56:00 PM

0




>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...


Logan Capaldo

10/29/2006 8:37:00 PM

0

On Mon, Oct 30, 2006 at 03:56:02AM +0900, Andreas S wrote:
>
>
>
> >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.
> -andre
>
> _________________________________________________________________
> Try the next generation of search with Windows Live Search today!
> http://imagine-windowslive.com/minisites/searchlaunch/?locale=en-us&source...
>

matt

10/29/2006 11:03:00 PM

0

Andreas S <andreas_s@hotmail.com> wrote:

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

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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Andreas S

10/30/2006 3:40:00 AM

0


>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=m...


Louis J Scoras

10/30/2006 12:05:00 PM

0

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.

Logan Capaldo

10/30/2006 3:00:00 PM

0

On Mon, Oct 30, 2006 at 12:40:16PM +0900, Andreas S wrote:
>
> >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
>
> 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=m...
>

Patriot Games

5/24/2007 7:42:00 PM

0

(article not available)

Patriot Games

5/24/2007 7:43:00 PM

0

(article not available)

Lawrence Glickman

5/24/2007 10:27:00 PM

0

(article not available)