post-receive hook 中的 PATH 不包含 bashrc 中设置的 PATH [英] PATH in post-receive hook doesn't contain PATH as set in bashrc
问题描述
如何在 Ubuntu 上设置 PATH,同时在接收后脚本中设置我设置的变量?目前我正在通过 ~/.bashrc
文件这样做:
How can I set the PATH on Ubuntu in a way where its variables that I set are also set in a post-receive script? Currently I'm doing it via the ~/.bashrc
file like this:
export PATH="$PATH:/opt/mssql-tools/bin"
但是如果我从钩子中打印它,则在 PATH
中看不到任何变化.因此,如果我尝试在钩子中执行有问题的命令,我会得到
but can't see any change in the PATH
if I print it from the hook. Therefore, if I try to execute the command in question in the hook, I get
remote: FileNotFoundError: [Errno 2] No such file or directory: 'sqlcmd': 'sqlcmd'
所以我现在看到的唯一解决方案是在 post-receive 钩子本身中再次定义它,如下所示:
So the only solution I see right now is defining it again in the post-receive hook itself, like this:
export PATH="$PATH:/opt/mssql-tools/bin"
有没有更好的方法?
谢谢!
推荐答案
首先,一点文件设置:
$ mkdir /tmp/dir1 /tmp/dir2
$ date > /tmp/dir1/foo
$ date > /tmp/dir2/bar
现在,考虑一个简单的脚本:
Now, consider a simple script:
$ chmod 755 foo.sh; cat foo.sh
#!/bin/sh
# intentionally set an inadequate PATH:
export PATH=""
# script to 'ls' two dirs, show that output, and show the diff of the two.
ls /tmp/dir1 > temp1
ls /tmp/dir2 > temp2
echo /tmp/dir1:
cat temp1
echo /tmp/dir2:
cat temp2
diff temp1 temp2
该脚本在语法上格式良好,但让我们看看会发生什么:
The script is well-formed syntactically, but let's see what happens:
$ ./foo.sh
./foo.sh: ls: not found
./foo.sh: ls: not found
/tmp/dir1:
./foo.sh: cat: not found
/tmp/dir2:
./foo.sh: cat: not found
./foo.sh: diff: not found
该路径不足以让脚本解释器找到脚本想要运行的可执行文件.三个单独的可执行文件无法加载:ls
、cat
和 diff
.所以让我们帮助它一点.由于 ls
通常驻留在 /bin
目录中,我们将 PATH
编辑为:
The path isn't sufficient for the script interpreter to find the executables the script wants to run. Three separate executables fail to load: ls
, cat
, and diff
. So let's help it a little. Since ls
typically resides in the /bin
directory, let's edit PATH
to become:
export PATH="/bin"
然后再试一次:
$ ./foo.sh
/tmp/dir1:
foo
/tmp/dir2:
bar
./foo.sh: diff: not found
好吧,ls
现在运行正常.这就是进步.由于 cat
也存在于/bin 中,所以在路径中添加/bin 一石二鸟.但是仍然没有找到 diff
,因为 diff
位于/usr/bin 中.因此,让我们将其添加到 PATH 中:
Well, ls
runs okay now. That's progress. And since cat
also lives in /bin, adding /bin to the path killed two birds with one stone. But diff
still isn't being found, because diff
lives in /usr/bin. So let's add that to the PATH:
export PATH="/bin:/usr/bin"
然后再试一次:
$ ./foo.sh
/tmp/dir1:
foo
/tmp/dir2:
bar
1c1
< foo
---
> bar
瞧!不再有错误,因为 PATH
变量包含允许脚本解释器定位脚本调用的可执行文件所需的一切.
Voila! No more errors, because the PATH
variable contains everything needed to allow the script interpreter to locate the executables that are called by the script.
另一种方法是告诉 PATH
对接并指定您自己的可执行文件路径.无论出于何种原因,当您可能不信任或不想要标准"可执行文件时,此方法有时会很方便.以这种方式构建脚本时,我更喜欢对要引用的可执行文件使用变量,这样如果 ^H^H 位置发生变化时,我只需更改变量即可,而不必搜索整个脚本该可执行文件的调用.
The other way is to tell PATH
to butt out and specify your own path to executables. This method is sometimes handy when you might not trust or desire the "standard" executables, for whatever reason. When structuring a script in this fashion, I prefer to use variables for the executables I want to reference, so that if^H^Hwhen the location changes, I can just change the variables and don't have to search the entire script for all the invocations of that executable.
$ chmod 755 bar.sh; cat bar.sh
#!/bin/sh
# intentionally set an inadequate PATH:
export PATH=""
# ls lives in /bin:
LS="/bin/ls"
# so does cat:
CAT="/bin/cat"
# but diff lives in /usr/bin:
DIFF="/usr/bin/diff"
# script to 'ls' two dirs, show that output, and show the diff of the two.
$LS /tmp/dir1 > temp1
$LS /tmp/dir2 > temp2
echo /tmp/dir1:
$CAT temp1
echo /tmp/dir2:
$CAT temp2
$DIFF temp1 temp2
和输出:
$ ./bar.sh
/tmp/dir1:
foo
/tmp/dir2:
bar
1c1
< foo
---
> bar
您可以混合和匹配这些方法,方法是指定包含大多数内容的 PATH
,并为其他内容指定绝对路径,但是您的问题出现了,因为您没有这样做.
You can mix and match these approaches, by specifying a PATH
that includes most things, and specifying absolute paths for the others, but your problem is arising because you have not done that.
您要么需要在钩子脚本中指定一个完整且足够的 PATH
,和/或指定驻留在任何 PATH
变量当前使用的钩子脚本.
You either need to specify a full and adequate PATH
in your hook script, and/or specify absolute paths to the remaining executables (if any) that reside outside whatever PATH
variable your hook script currently uses.
这篇关于post-receive hook 中的 PATH 不包含 bashrc 中设置的 PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!