3

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 5

7

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Might be worth noting that he needs to escape the string that he inserts. Better yet, I suppose it's better to use something like: echo 1/23|sed 's^/^2^'. Also, you don't need /g, as it's hopefully only one per line... :)
3

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

3

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

Removed it. It was from a snippit I found which looked for .pl AND .cgi files. Thanks for catching it.
Mine needed single quotes: find -name '*.pl' | xargs perl -pi -e 's{^#!/usr/bin/perl}{#!/usr/bin/env perl}'
0

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

Somehow I get an error: Illegal variable name. But this works: sed -i 's/^#\!.*$/#!\/bin\/ksh/' *.sh
0

On my machine only this works sed -i "s/^#\!.*/#\!\/usr\/bin\/perl/" *.sh Everyother throws an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.