我正在從CLI運行一個PHP腳本。在該PHP腳本中,我想使用system()
從命令行執行一些命令。也許這不是適合工作的工具,我可能只是將PHP腳本包裝在shell腳本中,但我想知道是否可以在PHP腳本中完全完成。如何從PHP exec調用python命令「workon」?
當我做這在終端正常工作:
[email protected]:~$ workon pootle
(pootle)[email protected]:~$
但是這並不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
爲空。
編輯
也許我與此有所進展。
[email protected]:~$ 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
}
'回聲$(其中workon)'(運行在shell終端,並參考路徑) –
@JoranBeasley輸出爲空。 – Mike
在這種情況下,我不認爲workon可以從終端訪問...'workon --help && echo $(which workon)' –