您无法从screen
会话中启动Shell脚本,因为它将继承旧环境。但是,您可以使用fifo将新的环境变量添加到旧的屏幕会话中。您可以在开始图形会话时填充该fifo。
#!/bin/bash
FIFO=/tmp/your_variables
[ -e $FIFO ] && cat $FIFO > /dev/null || mkfifo $FIFO
# save number of variables that follow
NVARS=2
echo $NVARS > $FIFO
echo ENV1=sth1 > $FIFO
echo ENV2=sth2 > $FIFO
登录时在后台启动该脚本(仅当从中读取所有变量时,该脚本才会终止)。
现在您可以阅读fifo了,例如,将此功能添加到您的 .bashrc
update_session() {
FIFO=/tmp/your_variables
NVAR=$(cat $FIFO)
for i in $(seq $NVAR); do
export $(cat $FIFO)
done
#delete the pipe, or it will not work next time
rm $FIFO
}
这样您就可以在旧screen
会话中
update_session
setenv [var [string]] Set the environment variable var to value string. If only var is specified, the user will be prompted to enter a value. If no parameters are specified, the user will be prompted for both variable and value. The environment is inherited by all subsequently forked shells.