Forum: Ruby bang is not work!!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cda3d26c2566666a0f507073b1389628?d=identicon&s=25 Kyung won Cheon (kdream95)
on 2009-02-18 01:59
OS is CentOs 4.7

$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]

$ cat ruby_test
#!/usr/local/bin/ruby

puts 'hello'

$ ls -l
-rwxr-xr-x  1 gmc gmc      94  2ì›” 17 19:11 ruby_test

$ ruby_test
-bash: ruby_test: command not found

################
# Why ???
# Help Me!!
################
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-02-18 02:08
(Received via mailing list)
Kyung won Cheon wrote:
> $ ls -l
> -rwxr-xr-x  1 gmc gmc      94  2ì›” 17 19:11 ruby_test
>
> $ ruby_test
> -bash: ruby_test: command not found
>
> ################
> # Why ???
> # Help Me!!
> ################

What's the output of

$ which ruby

What happens with:

$ ./ruby_test

and

$ ruby ruby-test
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2009-02-18 02:45
(Received via mailing list)
Kyung won Cheon wrote:

> $ ls -l
> -rwxr-xr-x  1 gmc gmc      94  2ì›” 17 19:11 ruby_test
>
> $ ruby_test
> -bash: ruby_test: command not found

That's normal.  Instead of typing "script_name", type "./script_name".
Unless you alias or move ruby_test to somewhere in your exec path, that
is (and you probably don't want that).
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-18 03:59
(Received via mailing list)
Hi,

Am Mittwoch, 18. Feb 2009, 09:59:29 +0900 schrieb Kyung won Cheon:
> OS is CentOs 4.7
>
> $ cat ruby_test
> #!/usr/local/bin/ruby
> [...]
> $ ruby_test
> -bash: ruby_test: command not found

This is a security feature of your OS/distribution and not about
Ruby.

Suppose you say something like

  $ export PATH=.:"$PATH"

and the intruder would place a script named "ls" in the current
directory...

Bertram
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-18 07:18
Bertram Scharpf wrote:
>
> This is a security feature of your OS/distribution and not about
> Ruby.
>
> Suppose you say something like
>
>   $ export PATH=.:"$PATH"
>
> and the intruder would place a script named "ls" in the current
> directory...
>

..and?
69f776698d1a63c70af26bfaea085b17?d=identicon&s=25 Choi, Junegunn (Guest)
on 2009-02-18 08:44
(Received via mailing list)
And, you may end up executing the possibly malicious script instead of
the original ls when you carelessly type 'ls'. Regarding the she-bang
line, I prefer to write '#!/usr/bin/env ruby' rather than to hard-code
the path of a ruby executable.

2009/2/18 7stud -- <bbxx789_05ss@yahoo.com>:
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-18 09:34
Choi, Junegunn wrote:
> And, you may end up executing the possibly malicious script instead of
> the original ls when you carelessly type 'ls'.
>

So your not supposed to just type ls at a prompt?
62002cee15efcf4628cd7efc19425a07?d=identicon&s=25 Tim Becker (Guest)
on 2009-02-18 09:43
(Received via mailing list)
> So your not supposed to just type ls at a prompt?
>

You shouldn't have '.' (the current) directory in the PATH where the
shell
looks for programs to execute, because someone could replace the "real"
program with one in the current directory.

To the original problem:

a.) either ruby is not in /usr/local/bin
-> use `#!/usr/bin/env ruby` instead

b.) '.' is not in the PATH
-> don't change PATH, call your script using `./ruby_test`

c.) you're script is not executable
-> call `chmod +x ruby_test`

or any combination of the above.

   -tim
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2009-02-18 19:55
(Received via mailing list)
7stud -- wrote:

> Choi, Junegunn wrote:
>> And, you may end up executing the possibly malicious script instead
>> of the original ls when you carelessly type 'ls'.
>>
>
> So your not supposed to just type ls at a prompt?

He was saying that without this protection you might not want to.  In
usual cases you do, because this protection exists.  It's one of
several reasons why you need to set the path, set an alias or type in
the full path, if it's not a system command.  It's a good thing.
This topic is locked and can not be replied to.