简体   繁体   English

如何在不修改FreeBSD,OpenBSD和Linux的情况下获取bash脚本?

[英]How do I get a bash script working on FreeBSD, OpenBSD and Linux without modifying it?

Sorry, the headline might be a bit irritating, but I didn't know anything better. 对不起,标题可能有点刺激,但我不知道更好。 Anyway, I want a bash script to work on FreeBSD, OpenBSD and Linux without modifying it, but bash isn't located at the same place in Linux and BSD. 无论如何,我想要一个bash脚本可以在FreeBSD,OpenBSD和Linux上运行而无需修改它,但是bash并不位于Linux和BSD的同一个地方。

So, if I write #!/bin/bash then it won't work on BSD, because the bash shell is located in /usr/local/bin/bash there. 所以,如果我写#!/bin/bash然后它将无法在BSD上运行,因为bash shell位于/usr/local/bin/bash中。 Is there any solution to get this script working on both? 是否有任何解决方案可以让这个脚本同时工作?

Or do I really need to ship two scripts with different paths...? 或者我真的需要发送两个不同路径的脚本......?

在shebang中使用env#!/usr/bin/env bash )应该使脚本OS不可知。

I like the answer about using #!/usr/bin/env bash It is an interesting and excellent answer, but that would only work if bash is in the path. 我喜欢使用#!/usr/bin/env bash答案这是一个有趣且优秀的答案,但只有当bash在路径中时才会有效。

Another option might be to use #!/bin/sh which is the most universally compatible shell location. 另一种选择可能是使用#!/bin/sh ,它是最普遍兼容的shell位置。 Then, have the script do something in sh , such as check where bash is installed (if bash is even installed). 然后,让脚本在sh执行某些操作,例如检查bash的安装位置(如果安装了bash )。 Another option might be to have bash exist to both locations. 另一个选择可能是两个位置都存在bash Making another installation may sound like overkill, but this goal could be accomplished as simply as creating a hard link so that bash actually exists in both locations. 进行另一次安装可能听起来有点过分,但这个目标可以像创建硬链接一样完成,以便bash实际存在于两个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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