在研究《管理Python虛擬環(huán)境的腳本》時,我們使用了source指令而沒有使用sh或者bash來執(zhí)行腳本,就是因為source指令可以讓腳本在當(dāng)前bash(sh)中執(zhí)行;而sh或者bash則會新啟動一個bash來執(zhí)行。
我們可以通過下面這個腳本做測試
# test.sh
# 用一個數(shù)組保存進(jìn)程ID和進(jìn)程名
processInfo=()
# 查找父進(jìn)程的進(jìn)程號
findParentID() {
if [ $1 = $2 ]; then
# 如果父進(jìn)程號等于目標(biāo)進(jìn)程號,說明已經(jīng)找到了父進(jìn)程
# 打印所有進(jìn)程信息
echo "processInfo: ${processInfo[@]}"
return
else
# 獲取當(dāng)前進(jìn)程的父進(jìn)程號
parentID=$(ps -o ppid= $1)
# 獲取父進(jìn)程的名字
parentName=$(ps -o comm= $parentID)
# 將父進(jìn)程號和父進(jìn)程名保存到數(shù)組中
processInfo+=($parentID $parentName)
findParentID $parentID $2
fi
}
currentName=$(ps -o comm= $$)
processInfo+=($$ $currentName)
findParentID $$ $1
bash
bash test.sh $$
processInfo: 45322 bash 40883 bash
當(dāng)前bash的進(jìn)程ID是40883,新啟動的bash的進(jìn)程ID是45322。
source
source test.sh $$
processInfo: 40883 bash
可以見得沒有啟動新的bash程序。
source還可以讓自動注冊腳本中的函數(shù)。
比如上面指令讓腳本中的findParentID方法可以直接被使用。
findParentID $$ $$
processInfo: 40883 bash
相似的應(yīng)用在Python虛擬環(huán)境中也有體現(xiàn)。
比如我們啟動一個虛擬環(huán)境,使用下面的命令
source .env/bin/activate
而退出虛擬環(huán)境的方法deactivate則注冊在.env/bin/activate文件中文章來源:http://www.zghlxwxcb.cn/news/detail-809744.html
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
# reset old environment variables
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
PATH="${_OLD_VIRTUAL_PATH:-}"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r 2> /dev/null
fi
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
PS1="${_OLD_VIRTUAL_PS1:-}"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
unset VIRTUAL_ENV_PROMPT
if [ ! "${1:-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV="/home/fangliang/numpy-example/.env"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
PS1="(.env) ${PS1:-}"
export PS1
VIRTUAL_ENV_PROMPT="(.env) "
export VIRTUAL_ENV_PROMPT
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r 2> /dev/null
fi
如果我們使用bash來執(zhí)行,則因為虛擬環(huán)境會在新啟動的bash中存在,并會快速退出?;氐轿覀冊瓉淼腷ash中時,已經(jīng)不是虛擬環(huán)境了。相應(yīng)的deactivate方法也沒注冊到環(huán)境中。
所以如果我們希望腳本對當(dāng)前bash有所影響,就要使用source去執(zhí)行腳本;如果不希望影響當(dāng)前bash,則可以使用bash或者sh去執(zhí)行。
需要注意的是,bash并不等價于sh。sh(Bourne Shell)是1978年由史蒂夫·伯恩編寫的shell;bash(Bourne-Again Shell)是1987年由布萊恩·福克斯為GNU計劃編寫的Unix shell。主要目標(biāo)是與POSIX標(biāo)準(zhǔn)保持一致,同時兼顧對sh的兼容,是各種Linux發(fā)行版標(biāo)準(zhǔn)配置的Shell。比如上面test.sh使用bash可以正確執(zhí)行,而sh執(zhí)行就會報錯。文章來源地址http://www.zghlxwxcb.cn/news/detail-809744.html
到了這里,關(guān)于在當(dāng)前bash(sh)中執(zhí)行腳本和注冊函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!