我有一个脚本,使用sh shell。我在使用source命令的行中得到一个错误。它似乎源不包括在我的sh shell。

如果我显式地尝试从shell运行source,我得到:

sh: 1: source: not found

我是否应该安装“source”?我的sh版本是否错误?


当前回答

source是bash内置命令,因此要执行source命令,您可以以Root用户登录。

sudo - s 源。/ filename.sh

其他回答

源代码是内置的bashism。简单地写成。代替。

e.g.

. $FILE

# OR you may need to use a relative path (such as in an `npm` script):

. ./$FILE

https://wiki.ubuntu.com/DashAsBinSh#source

在Ubuntu上,我没有使用sh scriptname.sh来运行文件,而是使用。Scriptname.sh,它工作了! 我的文件的第一行包含: # !/bin/bash

使用该命令运行脚本

.name_of_script.sh
$ls -l `which sh`
/bin/sh -> dash

$sudo dpkg-reconfigure dash #Select "no" when you're asked
[...]

$ls -l `which sh`
/bin/sh -> bash

那就好了

/bin/sh通常是试图模仿shell的其他shell。许多发行版使用/bin/bash作为sh,它支持source。但是在Ubuntu上,使用的是不支持source的/bin/dash。大多数shell使用。而不是来源。如果无法编辑脚本,请尝试更改运行脚本的shell。

我在Ubuntu上的gnu Makefile中找到了(其中/bin/sh -> bash)

我需要用。命令,并使用./前缀指定目标脚本(参见下面的示例)

源没有工作在这种情况下,不确定为什么,因为它应该调用/bin/ bash_. .

我的SHELL环境变量也设置为/bin/bash

test:
    $(shell . ./my_script)

注意,这个示例不包括制表符;必须格式化堆栈交换。