http://qs321.pair.com?node_id=743174


in reply to Automatically Rewrite Shebang Line on Multiple Files

This is very easy if you've got cygwin to give you a working shell, find and xargs. For example, to change from /usr/bin/perl to /usr/local/bin/perl I'd do something like (untested):

find -name '*.pl' -o -name '*.cgi' | xargs perl -pi -e "s{^#!/usr/bin/perl}{#!/usr/local/bin/perl}"

The -pi command-line switch tells Perl to do in-place editing and the -e specifies the code to on each line. If your filenames might have spaces in them you can add -print0 to the find call and -0 to the xargs call before perl.

If you want to do it all in Perl can use File::Find or something similar to find the files for you.

-sam

Replies are listed 'Best First'.
Re^2: Automatically Rewrite Shebang Line on Multiple Files
by ikegami (Patriarch) on Feb 11, 2009 at 22:08 UTC
    for %q in (*.cgi *.pl) do ( perl -pi.bak -e"s{^#!/usr/bin/perl}{#!/usr/local/bin/perl}" "%q" )

    Change for to for /r for a recursive solution.

    Update: Simplified solution

      Your solution with a for loop has the word perl within it. Is this a script in a language other than perl, or is there some significance to the use of "perl -pi.bak"? I'm not familiar with cygwin - the last time I tried to install it something didn't work right.

      Regards,
      Scott

        Is this a script in a language other than perl

        It's a Windows shell (cmd) command. If you need it in a batch file, you need to escape the %. Change %q to %%q.

        I'm not familiar with cygwin

        Cygwin is a OS emulator, not a shell or a scripting language. When people say Cygwin, they usually refer to bash and/or GNU tools. samtregar showed that a solution can be obtained using bash and GNU tools. I showed that such tools weren't required.

        is there some significance to the use of "perl -pi.bak"

        It invokes perl. -p and -i are documented in perlrun.

Re^2: Automatically Rewrite Shebang Line on Multiple Files
by linuxer (Curate) on Feb 11, 2009 at 21:53 UTC

    Hi, possibly just a typo, the shebang starts with #!, not with !#.

      Thanks - there's probably other errors. I usually have to play with a one-liner like this before it actually works.

      -sam