简体   繁体   English

我可以假设安装了bash

[英]Can I assume bash is installed

I have a cross platform product that gets deployed to several Linux distros and I'm busy changing one of its startup scripts. 我有一个跨平台产品,可以部署到几个Linux发行版,我正在忙着更改其中一个启动脚本。 Currently the script uses /bin/sh as interpreter. 目前该脚本使用/ bin / sh作为解释器。 My question is. 我的问题是。 How safe am I if I change this to /bin/bash, will there be some cases that bash is not installed or something. 如果我将其更改为/ bin / bash,我会有多安全,是否会出现某些未安装bash或其他情况的情况。 Why does all the scripts in /etc/init.d use /bin/sh. 为什么/etc/init.d中的所有脚本都使用/ bin / sh。 Are there some stuff that will break when using bash? 使用bash时会有一些东西会破坏吗?

Can I assume bash is installed 我可以假设安装了bash

If portability is a goal, you would be doing yourself a disservice to assume bash was installed. 如果可移植性是一个目标,那么假设安装了bash ,你自己就会受到伤害。 That is not to say that bash won't be installed 99% of the time, but it's the 1% that gets ya. 这并不是说99%的时间不会安装bash ,而是获得ya的1%。

How safe am I if I change this to /bin/bash, will there be some cases that bash is not installed or something 如果我将其更改为/ bin / bash,我会有多安全,是否会出现某些未安装bash或其他情况的情况

bash is a POSIX Compliant shell, so unless you need/want to use bash-only syntax, I would not just change the interpreter line to #!/bin/bash on a whim; bash是一个POSIX兼容的 shell,所以除非你需要/想要使用bash-only语法,否则我不会只是随心所欲地将解释器行更改为#!/bin/bash ; it buys you nothing with a script that works with #!/bin/sh 它使用与#!/bin/sh一起使用的脚本购买任何东西

Why does all the scripts in /etc/init.d use /bin/sh 为什么/etc/init.d中的所有脚本都使用/ bin / sh

Because they don't want the system to go down if the admin decides he wants to save hard drive space by deleting bash because his favorite shell is zsh 因为如果管理员决定通过删除bash来保存硬盘空间,因为他们最喜欢的shell是zsh他们不希望系统关闭

Are there some stuff that will break when using bash? 使用bash时会有一些东西会破坏吗?

It's more the other way around. 反过来也是如此。 If you have a script with bash-only (non-POSIX) syntax, for example [[ ]] or process substitution <( ) , this will break if you change the interpreter from #!/bin/bash to #!/bin/sh 如果你有一个只有bash-only(非POSIX)语法的脚本,例如[[ ]]或进程替换<( ) ,如果你将解释器从#!/bin/bash改为#!/bin/sh ,这将会中断#!/bin/sh

It depends on how "cross" platform you application is. 这取决于您应用程序的“交叉”平台。

In case it runs on "major" linux distributions, feel free to assume bash, but in case of smaller dists, or customized installs bash is not a given. 如果它在“主要”Linux发行版上运行,请随意假设bash,但如果是较小的dists,或自定义安装bash不是给定的。 Indeed if you move away from linux i would be even more careful about assuming bash (and for that matter the /bin/bash location is not a given either). 事实上,如果你离开linux,我会更加谨慎地假设bash(并且就此而言,/ bin / bash位置也不是给定的)。

The reason why /etc/init.d scripts always use /bin/sh is because it is a given on most platforms. /etc/init.d脚本总是使用/ bin / sh的原因是因为它在大多数平台上都是给定的。

I don't think stuff will break if you use bash, bash implements the same "features" as sh, but not the other way around. 如果你使用bash,我不认为东西会破坏,bash实现与sh相同的“功能”,但不是相反。 So in short if you don't need bash specific features, then use /bin/sh 因此,如果您不需要bash特定功能,那么请使用/ bin / sh

To assume that bash is installed falls in the same category as 假设安装的bash属于同一类别

  • All the world's a VAX 全世界都是VAX
  • All the CPUs are i386 所有的CPU都是i386
  • All the free Unixen are Linux 所有免费的Unixen都是Linux
  • All the IDEs are Eclipse 所有的IDE都是Eclipse
  • All the compilers are gcc 所有的编译器都是gcc

If you keep portability (at least to the POSIX shell) in mind, you will save yourself a lot of work for the future. 如果你保持可移植性(至少对POSIX shell),你将为自己节省大量未来的工作。 The day will come that you find yourself in a different company, with a different OS and a different shell. 你会发现自己身处另一家公司,拥有不同的操作系统和不同的外壳。 Then your knowledge about shell portability will pay large dividends. 那么你对shell可移植性的了解将会带来很大的好处。

The mantra to tell yourself and others is this: "Assumption is the mother of all screw-ups." 告诉自己和他人的口头禅是:“假设是所有搞砸的母亲。” :-) :-)

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

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