在bash脚本中使用源时出错:获得一个'未找到源“ [英] Getting a 'source: not found' error when using source in a bash script
问题描述
我想写(我以为会是)一个简单的bash脚本,将:
I'm trying to write (what I thought would be) a simple bash script that will:
- 运行的virtualenv在$ 1 创建一个新的环境
- 激活虚拟环境
- 做一些更多的东西(Django的安装,添加django-admin.py到的virtualenv的路径等)
- 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.)
第1步的作品不错,但我似乎无法激活virtualenv中。对于那些不熟悉的virtualenv,它创建激活虚拟环境中的激活
文件。从CLI中,使用源运行
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
source $env_name/bin/activate
其中$ env_name,很明显,是虚拟的env安装在dir的名称
Where $env_name, obviously, is the name of the dir that the virtual env is installed in.
在我的脚本,创建虚拟环境后,我存储路径激活脚本是这样的:
In my script, after creating the virtual environment, I store the path to the activate script like this:
activate="`pwd`/$ENV_NAME/bin/activate"
但是,当我打电话源$激活
,我得到这样的:
/home/clawlor/bin/scripts/djangoenv: 20: source: not found
我知道 $激活
包含正确的路径激活脚本,其实我甚至测试一个文件是存在的之前,我称之为源
。但源
本身似乎无法找到它。我也尝试运行的所有步骤在CLI,在那里一切正常手动。
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.
在我的研究,我发现<一个href=\"http://www.doughellmann.com/articles/CompletelyDifferent-2008-05-virtualenvwrapper/Listing1.html\">this脚本,这是类似我想要什么,但也做了很多,我不需要别的东西,比如存储所有虚拟环境在〜/ .virtualenv目录(或无论是在$ WORKON_HOME )。但在我看来,他是创造路径激活
,并调用源$激活
在基本同样的方式我。
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.
下面是它的脚本的全部:
Here is the script in it's entirety:
#!/bin/sh
PYTHON_PATH=~/bin/python-2.6.1/bin/python
if [ $# = 1 ]
then
ENV_NAME="$1"
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
免责声明:我的bash脚本富是pretty弱。我在CLI还算舒服,但也有可能是这是不工作的一些愚蠢至极的原因。
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脚本,通过名字来称呼它:
If you're writing a bash script, call it by name:
#!/bin/bash
/ bin / sh的是不能保证的是bash。这在几年前引起了每吨在Ubuntu破碎的脚本(IIRC)。
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
源内置的作品只是在bash罚款;但你可能也只是使用点像诺曼建议。
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.
这篇关于在bash脚本中使用源时出错:获得一个'未找到源“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!