我有一个使用sh
shell 的脚本。我在使用该source
命令的行中出现错误。它似乎source
不包含在我的sh
外壳中。
如果我明确尝试source
从 shell 运行,我会得到:
sh: 1: source: not found
我应该以某种方式安装“源”吗?我有错误的版本sh
吗?
/bin/sh
通常是其他一些试图模仿 The Shell 的 shell。许多发行版都使用/bin/bash
for sh
,它支持source
. 但是,在 Ubuntu 上,/bin/dash
使用的是不支持source
. 大多数 shell 使用.
而不是source
. 如果您无法编辑脚本,请尝试更改运行它的 shell。
在 Bourne shell(sh) 中,使用 . 获取文件的命令
. filename
在某些操作系统/环境(至少 Mac OS、Travis-CI、Ubuntu)中,这必须是:
. ./filename
$ls -l `which sh`
/bin/sh -> dash
$sudo dpkg-reconfigure dash #Select "no" when you're asked
[...]
$ls -l `which sh`
/bin/sh -> bash
然后就OK了
source
内置是一个bashism 。把它简单地写成这样.
。
例如
. $FILE
# OR you may need to use a relative path (such as in an `npm` script):
. ./$FILE
发生此问题是因为 jenkins Execute Shell 通过其 /bin/sh 运行脚本
因此,/bin/sh 不知道“来源”
您只需要在 jenkins 的 Execute Shell 顶部添加以下行
#!/bin/bash
我在尝试从 #Jenkins 执行 shell调用源命令时遇到了这个错误。
source profile.txt
或者
source profile.properties
替换源命令是使用,
. ./profile.txt
或者
. ./profile.properties
注意:两个点之间有一个空格(.)
该source
命令内置在某些 shell 中。如果您有脚本,则应在第一行指定要使用的 shell,例如:
#!/bin/bash
我在 Ubuntu 上的 gnu Makefile 中找到了(其中 /bin/sh -> bash)
我需要使用 . 命令,以及使用 ./ 前缀指定目标脚本(参见下面的示例)
source 在这种情况下不起作用,不知道为什么,因为它应该调用 /bin/bash..
我的 SHELL 环境变量也设置为 /bin/bash
test:
$(shell . ./my_script)
请注意,此示例不包含制表符;必须为堆栈交换格式化。
source是 bash 内置命令,因此要执行 source 命令,您可以以 Root 身份登录。
sudo -s
source ./filename.sh
在 Ubuntu 上,我没有使用 sh scriptname.sh 来运行文件,而是使用 . scriptname.sh 并且成功了!我的文件的第一行包含:
#!/bin/bash
使用此命令运行脚本
.name_of_script.sh
这可能会对您有所帮助,我收到此错误是因为我试图.profile
用命令重新加载我的. .profile
并且它有一个语法错误
Bourne shell (sh) 使用 PATH 定位到source <file>
. 如果您尝试获取的文件不在您的路径中,则会收到错误“找不到文件”。
尝试:
source ./<filename>