[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

changing the shebang of ruby files best way ?

unbewusst.sein

6/27/2008 8:22:00 PM

I've a lot of ruby files (grabed from net) having a wrong shebang for my
setup.

actual shebang is :
#!/usr/bin/ruby -w

and i need to change it to :
#! /usr/bin/env ruby -w

that way i'll get the ruby in my PATH and not a fixed ruby.

what i plan to do :

detect if the shebang isn't correct and if true :
move the file to a temp directory
create a new with "good" shebang having path/name of the previous


remove the tmp dir

but i wonder, because i know i have to change the first line only, if i
could change the uncorrect file "in place" even if my shebang line is
longer (4 chars) than the preceeding one ????
--
Une Bévue
17 Answers

Greg Donald

6/27/2008 8:47:00 PM

0

On Fri, Jun 27, 2008 at 3:22 PM, Une B=E9vue
<unbewusst.sein@weltanschauung.com.invalid> wrote:
> I've a lot of ruby files (grabed from net) having a wrong shebang for my
> setup.
>
> actual shebang is :
> #!/usr/bin/ruby -w
>
> and i need to change it to :
> #! /usr/bin/env ruby -w


for file in *.rb; do
cp $file $file.tmp
sed -e "s/#!\/usr\/bin\/ruby\ -w$/#!\/usr\/bin\/env\ ruby\ -w/g"
$file.tmp >$file
rm $file.tmp
done



--=20
Greg Donald
http://des...

unbewusst.sein

6/27/2008 9:11:00 PM

0

Greg Donald <gdonald@gmail.com> wrote:

> for file in *.rb; do
> cp $file $file.tmp
> sed -e "s/#!\/usr\/bin\/ruby\ -w$/#!\/usr\/bin\/env\ ruby\ -w/g"
> $file.tmp >$file
> rm $file.tmp
> done

fine, thanks, i'll switch from ruby to zsh ;-)
--
Une Bévue

Igal Koshevoy

6/27/2008 9:56:00 PM

0

Greg Donald wrote:
> for file in *.rb; do
> cp $file $file.tmp
> sed -e "s/#!\/usr\/bin\/ruby\ -w$/#!\/usr\/bin\/env\ ruby\ -w/g"
> $file.tmp >$file
> rm $file.tmp
> done
>
Or as sh and Ruby:

for file in *.rb; do
ruby -p -i.bak -e '$_.sub!(%r{(#!)(/usr/bin/ruby)( -w)?},
"\\1/usr/bin/env ruby -w")' "$file"
done

Or using find and Ruby to recurse the entire tree:

find . -type f -name '*.rb' -exec ruby -p -i.bak -e
'$_.sub!(%r{(#!)(/usr/bin/ruby)( -w)?}, "\\1/usr/bin/env ruby -w")' {} \;

The "-i.bak" tells Ruby to do in-place replacements and backup originals
to that file extension.

-igal

efriedNOspam

6/28/2008 4:02:00 AM

0

In article <1ij7ltk.jdqdo9zynmjxN%unbewusst.sein@weltanschauung.com.invalid>, unbewusst.sein@weltanschauung.com.invalid (=?ISO-8859-1?Q?Une_B=E9v?=
=?ISO-8859-1?Q?ue?=) wrote:
>I've a lot of ruby files (grabed from net) having a wrong shebang for my
>setup.
>
>actual shebang is :
>#!/usr/bin/ruby -w
>
>and i need to change it to :
>#! /usr/bin/env ruby -w
>
>that way i'll get the ruby in my PATH and not a fixed ruby.
>
>what i plan to do :
>
>detect if the shebang isn't correct and if true :
> move the file to a temp directory
> create a new with "good" shebang having path/name of the previous
>
>
>remove the tmp dir
>
>but i wonder, because i know i have to change the first line only, if i
>could change the uncorrect file "in place" even if my shebang line is
>longer (4 chars) than the preceeding one ????

Just create a symbolic link to /usr/bin and leave the shebang

Ryan Davis

6/28/2008 7:41:00 PM

0


On Jun 27, 2008, at 13:22 , Une B=E9vue wrote:

> I've a lot of ruby files (grabed from net) having a wrong shebang =20
> for my
> setup.
>
> actual shebang is :
> #!/usr/bin/ruby -w
>
> and i need to change it to :
> #! /usr/bin/env ruby -w

the problem is that /usr/bin/env absolutely sucks on linux. -w is not =20=

only NOT passed as an argument to "ruby", it is considered part of =20
ruby (eg "ruby -w") and horks the exec.

If you're tweaking shebang lines JUST FOR YOU, this is fine... but I =20
get a bunch of tickets filed against my software that it doesn't work =20=

for them (mitigated by installing via rubygems). I try to tell them to =20=

file a bug against their linux distro, but they don't seem to agree =20
with me most of the time. :)


unbewusst.sein

6/29/2008 10:56:00 AM

0

e <efriedNOspam@yahoo.com> wrote:

>
> Just create a symbolic link to /usr/bin and leave the shebang

there is another ruby there...
the default ruby (Apple installed) is under /usr, the used ruby is under
/opt, i'm using the latest...
--
Une Bévue

Greg Donald

6/29/2008 5:09:00 PM

0

On Sat, Jun 28, 2008 at 2:41 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
> the problem is that /usr/bin/env absolutely sucks on linux. -w is not only
> NOT passed as an argument to "ruby", it is considered part of ruby (eg "ruby
> -w") and horks the exec.

You can set $VERBOSE, $-w, or $-v to true to get the same effect as ruby -w.


--
Greg Donald
http://des...

Garance A Drosehn

6/30/2008 4:00:00 AM

0

On Sat, Jun 28, 2008 at 3:41 PM, Ryan Davis <ryand-ruby@zenspider.com>
wrote:

>
> On Jun 27, 2008, at 13:22 , Une B=E9vue wrote:
>
> actual shebang is :
>> #!/usr/bin/ruby -w
>>
>> and i need to change it to :
>> #! /usr/bin/env ruby -w
>>
>
> the problem is that /usr/bin/env absolutely sucks on linux. -w is not onl=
y
> NOT passed as an argument to "ruby", it is considered part of ruby (eg "r=
uby
> -w") and horks the exec.
>

FWIW, the problem here is not 'env', it's the way that '#!' lines are
parsed. The 'env' will do the correct thing if it is given separate
parameters, but the kernel-level exec processor does not parse out all the
individual fields in a '#!'-line. It parses "the executable" and
"everything else", and then passes "everything else" as a single parameter
to "the executable". And at this point, it would be much too incompatible =
a
change to have the exec processor parse it in any other way. There's even
some logic to the way that parsing is done, although I don't remember it at
the moment.

The 'env' command in FreeBSD provides a way around this, by adding some mor=
e
options to 'env' so it can parse out the individual options, but I don't
know if any other OS's will notice that and pick up the options. (note:
I'm the guy who added those options to 'env' in FreeBSD...)

--=20
Garance Alistair Drosehn =3D drosihn@gmail.com
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA

Ryan Davis

6/30/2008 6:55:00 PM

0


On Jun 29, 2008, at 10:09 , Greg Donald wrote:

> On Sat, Jun 28, 2008 at 2:41 PM, Ryan Davis <ryand-
> ruby@zenspider.com> wrote:
>> the problem is that /usr/bin/env absolutely sucks on linux. -w is
>> not only
>> NOT passed as an argument to "ruby", it is considered part of ruby
>> (eg "ruby
>> -w") and horks the exec.
>
> You can set $VERBOSE, $-w, or $-v to true to get the same effect as
> ruby -w.

what about -s? or a myriad of other options...


Ryan Davis

6/30/2008 7:15:00 PM

0


On Jun 29, 2008, at 20:59 , Garance A Drosehn wrote:

> The 'env' command in FreeBSD provides a way around this, by adding
> some more
> options to 'env' so it can parse out the individual options, but I
> don't
> know if any other OS's will notice that and pick up the options.
> (note:
> I'm the guy who added those options to 'env' in FreeBSD...)

I've been told that FreeBSD is going to start acting like linux in
this regard... is that true?