在 bash 脚本中使用源时出现“源:未找到”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/670191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a 'source: not found' error when using source in a bash script
提问by Chris Lawlor
I'm trying to write (what I thought would be) a simple bash script that will:
我正在尝试编写(我认为是)一个简单的 bash 脚本,它将:
- run virtualenv to create a new environment at $1
- activate the virtual environment
- do some more stuff (install django, add django-admin.py to the virtualenv's path, etc.)
- 运行 virtualenv 以 $1 的价格创建一个新环境
- 激活虚拟环境
- 做更多的事情(安装 django,将 django-admin.py 添加到 virtualenv 的路径等)
Step 1 works quite well, but I can't seem to activate the virtualenv. For those not familiar with virtualenv, it creates an activate
file that activates the virtual environment. From the CLI, you run it using source
第 1 步效果很好,但我似乎无法激活 virtualenv。对于那些不熟悉 virtualenv 的人,它会创建一个activate
文件来激活虚拟环境。在 CLI 中,您可以使用source
source $env_name/bin/activate
Where $env_name, obviously, is the name of the dir that the virtual env is installed in.
显然,$env_name 是安装虚拟环境的目录的名称。
In my script, after creating the virtual environment, I store the path to the activate script like this:
在我的脚本中,创建虚拟环境后,我存储了激活脚本的路径,如下所示:
activate="`pwd`/$ENV_NAME/bin/activate"
But when I call source "$activate"
, I get this:
但是当我打电话时source "$activate"
,我得到了这个:
/home/clawlor/bin/scripts/djangoenv: 20: source: not found
I know that $activate
contains the correct path to the activate script, in fact I even test that a file is there before I call source
. But source
itself can't seem to find it. I've also tried running all of the steps manually in the CLI, where everything works fine.
我知道它$activate
包含激活脚本的正确路径,实际上我什至在调用source
. 但是source
自己好像找不到。我还尝试在 CLI 中手动运行所有步骤,一切正常。
In my research I found this script, which is similar to what I want but is also doing a lot of other things that I don't need, like storing all of the virtual environments in a ~/.virtualenv directory (or whatever is in $WORKON_HOME). But it seems to me that he is creating the path to activate
, and calling source "$activate"
in basically the same way I am.
在我的研究中,我发现了这个脚本,它与我想要的类似,但也做了很多我不需要的其他事情,比如将所有虚拟环境存储在 ~/.virtualenv 目录中(或任何在$WORKON_HOME)。但在我看来,他正在创造通往 的道路activate
,并source "$activate"
以与我基本相同的方式呼唤。
Here is the script in it's entirety:
这是脚本的全部内容:
#!/bin/sh
PYTHON_PATH=~/bin/python-2.6.1/bin/python
if [ $# = 1 ]
then
ENV_NAME=""
virtualenv -p $PYTHON_PATH --no-site-packages $ENV_NAME
activate="`pwd`/$ENV_NAME/bin/activate"
if [ ! -f "$activate" ]
then
echo "ERROR: activate not found at $activate"
return 1
fi
source "$activate"
else
echo 'Usage: djangoenv ENV_NAME'
fi
DISCLAIMER: My bash script-fu is pretty weak. I'm fairly comfortable at the CLI, but there may well be some extremely stupid reason this isn't working.
免责声明:我的 bash script-fu 非常弱。我对 CLI 相当熟悉,但很可能有一些非常愚蠢的原因这不起作用。
回答by guns
If you're writing a bash script, call it by name:
如果您正在编写 bash 脚本,请按名称调用它:
#!/bin/bash
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
/bin/sh 不保证是 bash。几年前,这在 Ubuntu (IIRC) 中造成了大量损坏的脚本。
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.
内置的源代码在 bash 中运行良好;但你也可以像诺曼建议的那样使用点。
回答by Norman Ramsey
In the POSIX standard, which /bin/sh
is supposed to respect, the command is .
(a single dot), not source
. The source
command is a csh
-ism that has been pulled into bash
.
在/bin/sh
应该尊重的 POSIX 标准中,命令是.
(一个点),而不是source
. 该source
命令是一个csh
-ism 已被拉入bash
.
Try
尝试
. $env_name/bin/activate
Or if you must have non-POSIX bash
-isms in your code, use #!/bin/bash
.
或者,如果您bash
的代码中必须有非 POSIX -ism,请使用#!/bin/bash
.
回答by Madhu
In Ubuntu if you execute the script with sh scriptname.sh
you get this problem.
在 Ubuntu 中,如果您执行脚本,sh scriptname.sh
则会遇到此问题。
Try executing the script with ./scriptname.sh
instead.
尝试使用./scriptname.sh
代替执行脚本。