Can someone please tell me how to find out the pathname of the KornShell (ksh) on my machine and then change the interpreter line in all shell scripts (.sh) in the current directory that show a different pathname for ksh?
5 Answers
This will give you the path to Korn shell:
which ksh
And this will replace shebang in all shell scripts:
sed -i 's/#!\/bin\/bash/#!insert escaped ksh path here/' *.sh
1 Comment
echo 1/23|sed 's^/^2^'. Also, you don't need /g, as it's hopefully only one per line... :)To find out where ksh lives:
whence -a ksh
To change all shell scripts in the current directory to use ksh:
sed -i '1{/^#!\/[^ ]*\/\(ba\|\)sh\( *\)/s||#!/bin/ksh\2|}' *.sh
This will match for sh or bash and preserve any spaces (and arguments) that appear afterwards. It only acts on the first line so it won't touch similar lines that may appear later in the file.
Substitute the actual location of your ksh executable or use /usr/bin/env ksh.
Use sed -i .bak if you want to backup the changed files.
Comments
Put this in the first line of each file (shebang line):
#!/usr/bin/env ksh
If you need to replace you can use sed/awk:
find -name '*.sh' | xargs perl -pi -e "s{^#!/usr/bin/sh}{#!/usr/bin/env ksh}"
2 Comments
find -name '*.pl' | xargs perl -pi -e 's{^#!/usr/bin/perl}{#!/usr/bin/env perl}'This command will locate the korn shell executable in your UNIX environment:
which ksh
If no output is returned, then the korn shell is missing. (At least not found in your environment PATH.) You can get it from the KornShell site or install it via your software package manager system.
In order to replace the interpreter line in all shell scripts (*.sh), you could run something like this:
sed -i "s/^#\!.*$/#\!\/bin\/ksh/" *.sh
The -i option is to edit files in place. (Warning: Test this command without the -i first, to avoid file modifications.)
The quoted argument is the pattern to match all lines starting with "#!" and replace them with "#!/bin/ksh". (Note that some special characters need to be escaped with "\" before.) You may need to customize this argument if it isn't exactly the replacement you're looking for.
1 Comment
sed -i 's/^#\!.*$/#!\/bin\/ksh/' *.sh