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

Andreas S [email protected] wrote:

#!/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

From: “Robert K.” [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

Andreas S [email protected] 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.

From: [email protected] (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

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

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.

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`

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

From: [email protected] (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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs