【发布时间】:2017-07-22 20:34:29
【问题描述】:
我正在从 CLI 运行 PHP 脚本。在那个 PHP 脚本中,我想使用system() 从命令行执行一些命令。也许这不是适合这项工作的工具,我可能只是将 PHP 脚本包装在 shell 脚本中,但我想知道是否可以完全在 PHP 脚本中完成。
当我在终端中执行此操作时,它工作正常:
user@host:~$ workon pootle
(pootle)user@host:~$
但是这在 PHP 中不起作用(作为同一用户执行)。
#!/usr/bin/env php
<?php
system('workon pootle'); // sh: 1: workon: not found
system('/bin/bash workon pootle'); // /bin/bash: workon: No such file or directory
system('/bin/sh workon pootle'); // /bin/sh: 0: Can't open workon
我注意到最后两个的输出与终端执行时的输出完全相同,但第一个不同。我认为它可能是一个别名,但它似乎不是。 alias | grep -i workon 没有显示输出。
我还比较了从命令行返回的所有环境变量env 和 PHP system('env'),除了_ 之外的所有环境变量都完全相同,包括SHELL=/bin/bash。
终端执行的which workon的输出为空。
编辑
也许我在这方面有所收获。
user@host:~$ type workon
workon is a function
workon ()
{
typeset env_name="$1";
if [ "$env_name" = "" ]; then
lsvirtualenv -b;
return 1;
fi;
virtualenvwrapper_verify_workon_home || return 1;
virtualenvwrapper_verify_workon_environment $env_name || return 1;
activate="$WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate";
if [ ! -f "$activate" ]; then
echo "ERROR: Environment '$WORKON_HOME/$env_name' does not contain an activate script." 1>&2;
return 1;
fi;
type deactivate > /dev/null 2>&1;
if [ $? -eq 0 ]; then
deactivate;
unset -f deactivate > /dev/null 2>&1;
fi;
virtualenvwrapper_run_hook "pre_activate" "$env_name";
source "$activate";
virtualenvwrapper_original_deactivate=`typeset -f deactivate | sed 's/deactivate/virtualenv_deactivate/g'`;
eval "$virtualenvwrapper_original_deactivate";
unset -f deactivate > /dev/null 2>&1;
eval 'deactivate () {
# Call the local hook before the global so we can undo
# any settings made by the local postactivate first.
virtualenvwrapper_run_hook "pre_deactivate"
env_postdeactivate_hook="$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR/postdeactivate"
old_env=$(basename "$VIRTUAL_ENV")
# Call the original function.
virtualenv_deactivate $1
virtualenvwrapper_run_hook "post_deactivate" "$old_env"
if [ ! "$1" = "nondestructive" ]
then
# Remove this function
unset -f virtualenv_deactivate >/dev/null 2>&1
unset -f deactivate >/dev/null 2>&1
fi
}';
virtualenvwrapper_run_hook "post_activate";
return 0
}
【问题讨论】:
-
echo $(which workon)(从 shell 终端运行并参考路径) -
@JoranBeasley 输出为空。
-
在这种情况下,我也不相信可以从终端访问 workon ...
workon --help && echo $(which workon) -
但确实如此。查看问题。
-
可以试试
workon的绝对路径吗?
标签: php bash shell virtualenv virtualenvwrapper