Pregunta

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

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:

$cd -

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

#! /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:

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

In build_1.sh, it just contain:

#! /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.

Any comment on this issue?

¿Fue útil?

Solución

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).

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

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.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top