backtick and %x{} not the same?

A

Andreas S

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/msnnkwl...://www.windowsonecare.com/?sc_cid=msn_hotmail
 
R

Robert Klemme

Andreas S said:
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
 
A

Andreas S

From: "Robert Klemme" <[email protected]>

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
 
L

Logan Capaldo

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

matt neuburg

Andreas S said:
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.
 
A

Andreas S

From: (e-mail address removed) (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/msnnkwl...://www.windowsonecare.com/?sc_cid=msn_hotmail
 
L

Louis J Scoras

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`
 
L

Logan Capaldo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top