在 Ubuntu 20.04LTS 上:
- 創建一個虛擬用戶。
- 創建一個簡短的腳本,將匯出的變數附加到新用戶的
.bashrc,然后獲取.bashrc該變數并嘗試使用該變數。 - 以新用戶身份運行腳本。
為什么沒有設定變數?
> adduser --gecos ',,,' --disabled-password foouser
[...output...]
> cat > /tmp/bootstrap.sh <<EOF
echo 'export FOO=foo' >> /home/foouser/.bashrc
. /home/foouser/.bashrc
echo 'sourced foouser bashrc'
set | grep FOO
EOF
> chmod a x /tmp/bootstrap.sh
> su - foouser -c /tmp/bootstrap.sh
[...output does not include FOO...]
> deluser --remove-home foouser
uj5u.com熱心網友回復:
您的問題是明確.bashrc配置為僅針對互動式外殼運行。看頂部:/home/foouser/.bashrc
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
如果您將 a 添加set -x到/tmp/bootstrap.sh腳本中,您將在執行時看到以下內容:
echo 'export FOO=foo'
. /home/foouser/.bashrc
case $- in
return
echo 'sourced foouser bashrc'
sourced foouser bashrc
set
grep FOO
在那里你可以看到它命中return了 case 陳述句中的命令。
-i您可以使用以下選項強制互動式 shell :
root@ed3085a447ad:/# su - foouser -c 'bash -i -c /tmp/bootstrap.sh'
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
sourced foouser bashrc
FOO=foo
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/472126.html
