相同的shell脚本在不同的Linux发行版上具有不同的行为

时间:2021-10-11 17:28:53

I simply wrote a shell script to make me compile my code easily. My directory tree looks like:

我只是编写了一个shell脚本,以便我轻松编译代码。我的目录树看起来像:

TOPDIR
|
+--- DIR[1]
.
.
.
+--- DIR[n]
|
+--- DIR[n+1]
      |
      +--- makefile

I used to use following command to compile my code:

我曾经使用以下命令编译我的代码:

$cd DIR[n+1]
$make

because I want to go back to TOPDIR after compile the code, therefore, I type:

因为我想在编译代码后回到TOPDIR,因此,我输入:

$cd -

I want to make it in one shell script file, so I write:

我想在一个shell脚本文件中创建它,所以我写道:

#! /bin/sh
cd $HOME/$WORKDIR/$MAKEDIR
make
cd -

It works well, after this, I got new code from our vendor and it changed something in make process. The directory tree looks almost same as previous but it uses a shell script to start the make process:

它运作良好,在此之后,我从我们的供应商处获得了新代码,并且它在make过程中发生了变化。目录树看起来与之前几乎相同,但它使用shell脚本来启动make进程:

TOPDIR
|
+--- DIR[1]
|
+--- DIR[2]
.
.
.
+--- DIR[N]
      |
      +--- DIR[n]
      |     |
      |     +---build_1.sh
      +---build_2.sh

In build_1.sh, it just contain:

在build_1.sh中,它只包含:

#! /bin/sh
source ../build_2.sh

therefore, I first use:

因此,我首先使用:

$cd DIR[n]
$./build_1.sh

after compile, I use:

编译后,我使用:

$cd -

but I write my command in script:

但是我用脚本写了我的命令:

#! /bin/sh
cd DIR[n]
./build_1.sh
cd -

it shows me:

它告诉我:

./build_1.sh: 2: ./build_1.sh: source: not found

I use same script on openSUSE; it has no problem. But on ubuntu-13.04, its pop up above error. I tested the source function in ubuntu-13.04, no problem for use.

我在openSUSE上使用相同的脚本;它没有问题。但是在ubuntu-13.04上,它弹出错误。我在ubuntu-13.04中测试了源函数,没问题。

Any comment on this issue?

对此问题有何评论?

1 个解决方案

#1


1  

In Ubuntu, the default shell /bin/sh is a symbolic link to /bin/dash - bash and dash have enough functionality in common that most people never realize the difference, but they are not identical (dash is meant to be lighter).

在Ubuntu中,默认的shell / bin / sh是/ bin / dash的符号链接 - bash和dash具有足够的共同功能,大多数人从未意识到差异,但它们并不完全相同(破折号意味着更轻)。

The Debian Almquist shell (dash) is a Unix shell, much smaller than Bash, but it is still aiming at POSIX-compliance. It requires less disk space, but it is also less feature-rich. - Wikipedia

Debian Almquist shell(破折号)是一个Unix shell,比Bash小得多,但它仍然瞄准POSIX兼容性。它需要的磁盘空间更少,但功能也更少。 - *

You should be safe while sticking to POSIX syntax, but if your script is using features that are only available in bash, be sure to change the shebang from /bin/sh to /bin/bash to avoid trouble with systems where /bin/sh is not bash.

在坚持使用POSIX语法时应该是安全的,但是如果你的脚本使用的是仅在bash中可用的功能,请务必将/ bin / sh中的shebang更改为/ bin / bash以避免系统出现问题/ bin / sh不是bash。

So my advice is: never assume bash is the default shell, stick to syntax from the POSIX standard if you can or explicitly point to /bin/bash at the shebang.

所以我的建议是:永远不要假设bash是默认的shell,如果你可以或者明确指向shebang的/ bin / bash,那么坚持使用POSIX标准的语法。

#1


1  

In Ubuntu, the default shell /bin/sh is a symbolic link to /bin/dash - bash and dash have enough functionality in common that most people never realize the difference, but they are not identical (dash is meant to be lighter).

在Ubuntu中,默认的shell / bin / sh是/ bin / dash的符号链接 - bash和dash具有足够的共同功能,大多数人从未意识到差异,但它们并不完全相同(破折号意味着更轻)。

The Debian Almquist shell (dash) is a Unix shell, much smaller than Bash, but it is still aiming at POSIX-compliance. It requires less disk space, but it is also less feature-rich. - Wikipedia

Debian Almquist shell(破折号)是一个Unix shell,比Bash小得多,但它仍然瞄准POSIX兼容性。它需要的磁盘空间更少,但功能也更少。 - *

You should be safe while sticking to POSIX syntax, but if your script is using features that are only available in bash, be sure to change the shebang from /bin/sh to /bin/bash to avoid trouble with systems where /bin/sh is not bash.

在坚持使用POSIX语法时应该是安全的,但是如果你的脚本使用的是仅在bash中可用的功能,请务必将/ bin / sh中的shebang更改为/ bin / bash以避免系统出现问题/ bin / sh不是bash。

So my advice is: never assume bash is the default shell, stick to syntax from the POSIX standard if you can or explicitly point to /bin/bash at the shebang.

所以我的建议是:永远不要假设bash是默认的shell,如果你可以或者明确指向shebang的/ bin / bash,那么坚持使用POSIX标准的语法。