简体   繁体   中英

UNIX, change the interpreter line in all shell scripts

有人可以告诉我如何在我的机器上找到KornShell(ksh)的路径名,然后更改当前目录中显示ksh的不同路径名的所有shell脚本(.sh)中的解释器行吗?

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 

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}" find -name '*.sh' | xargs perl -pi -e "s{^#!/usr/bin/sh}{#!/usr/bin/env ksh}"

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.

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.

在我的机器上只有这个工作sed -i "s/^#\\!.*/#\\!\\/usr\\/bin\\/perl/" *.sh每个人都会抛出一个错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM