Monday, 23 November 2009

Using sed to change perl interpreter path in scripts

Was installing OTRS on windows (unfortunately), even tho I downloaded the zip file it still used /usr/bin/perl as path and this was causing problems so wanted quick way to change #!/usr/bin/perl at beginning of each perl script into #!perl (assuming perl in is your windows path)

SED is well known and incredibly useful tool for doing such a job, easiest way to download is to install cygwin

Following test on a file will output to stdout and prove regular expression works


sed 's/^#!\/usr\/bin\/perl/#!perl/g'

after that you can use the "-i" switch to do a live edit of all files with a ".pl" extension
sed -i 's/^#!\/usr\/bin\/perl/#!perl/g' *.pl

Tried initially using "*" glob but turns out sed then tries to process directories and gives error, not all perl files have the .pl extenstion so modified slightly to use the find command to process only files and pass them to sed command.

find -type f -print -exec sed -i 's/^#!\/usr\/bin\/perl/#!perl/g' {} \;

On windows you might be best changing #!perl to be full path to your perl executabe, e.g. #!C:\/Perl\/Bin\/Perl.exe


No comments: