linux - 导致用户注销的 Bash 脚本

标签 linux bash

<分区>

在下面的脚本中,当 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/

相关文章:

python - 将输入传递给 shell 脚本调用的 python 脚本

python - 用 fabric 模拟 KeyboardInterrupt

linux - 如何在不使用可加载内核模块(LKM)的情况下在/proc中添加文件

c++ - 如何在 Linux 上使用智能指针(例如 auto_ptr r shared_ptr)在 C++ 中生成链接列表数据结构?

mysql - 无法连接到新的mysql;密码问题?

linux - 如何使用 termios 将删除键分配给 Linux 中的另一个字符?

linux - 如何从音频文件中获取振幅列表?

linux - 如果存在 git 命令,则使 bash 命令回退到 git 命令

linux - 另一个命令中的 Bash 命令变量

bash - 在 bash 中,如何打印列表的前 n 个元素?