<分区>
在下面的脚本中,当 if
语句中的代码被执行时,用户将从 bash 中注销。这是为什么?
#!/bin/sh
if [ -z $1 ]; then
echo "1"
read w
exit 1
fi
if [ "$#" -gt "1" ]; then
echo "1"
read w
exit 2
fi
export PTSUSER=$1.
<some more code>
<分区>
在下面的脚本中,当 if
语句中的代码被执行时,用户将从 bash 中注销。这是为什么?
#!/bin/sh
if [ -z $1 ]; then
echo "1"
read w
exit 1
fi
if [ "$#" -gt "1" ]; then
echo "1"
read w
exit 2
fi
export PTSUSER=$1.
<some more code>
最佳答案
如果脚本已获取,并且命中了 if
block 中的任何一个,那么是的,当前用户的 bash shell 将退出,并且用户将被注销。这是因为获取脚本将使用当前用户的 bash shell 来执行脚本。
$ . ./script.sh
另一方面,如果它在新的 bash shell 实例中运行,则不会发生这种情况,一旦在 shell 中执行了任一退出命令(在任一 if block 中),bash 实例就会退出。
$ bash ./script.sh
关于linux - 导致用户注销的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12528605/