无需注销即可切换到父 shell

Avi*_*ran 29 shell

假设我从本地帐户开始:

avindra@host:~>
Run Code Online (Sandbox Code Playgroud)

然后我切换到root:

host:~ #
Run Code Online (Sandbox Code Playgroud)

然后我切换到oracle:

[ oracle@host:~]
Run Code Online (Sandbox Code Playgroud)

有没有办法让我回到 root shell(父 shell),而无需退出 oracle shell?

这会很方便,因为 oracle 帐户没有sudo 权限。oracle的一个典型场景是我最终在/some/really/deeply/nested/目录下,各种特殊的环境变量都是按照特定的方式设置的。

问题来了:我需要回到 root 来访问一些系统文件。是的,我可以退出 oracle 以返回 root,但代价是丢失我当前的工作目录和环境。

有没有办法使用已知的约定“切换”到父 shell?

Bra*_*ley 41

您可以CTRL-Z使用以下kill命令模拟一个(通常用于临时后台进程):

[tsa20@xxx01:/home/tsa20/software]$ kill -19 $$

[1]+  Stopped                 sudo -iu tsa20
[root@xxx01 ~]# fg
sudo -iu tsa20
[tsa20@xxx01:/home/tsa20/software]$
Run Code Online (Sandbox Code Playgroud)

bash只是陷阱CTRL-Z组合键。kill -19发送SIGSTP到实际上是同一件事的进程。

  • 请注意,不能保证“19”将是 SIGTSTP 的数量。使用 `kill -s TSTP` 或 `kill -s STOP`(TSTP 可以被捕获或忽略,STOP 不能)。 (5认同)
  • @Mikel、`zsh` 或`csh`、`tcsh` 或`bash`,或Bourne shell 或`ksh`...(在`ksh` 中,_suspend 是`'kill -s STOP $$ '`_(注意 $$ 周围缺少引号的错误))。 (3认同)
  • `bash` _not_ 捕获 CTRL-Z,当你按下 CTRL-Z 时,你的终端模拟器发送一个 `^Z` 字符 (0x1a) 到 pty 设备的主端。pty 驱动程序的 _line 规则_然后将 SIGTSTP 发送到终端的前台进程组。在那之前,根本不涉及`bash`。SIGTSTP 可能会导致进程组领导挂起,然后_bash` 对其执行的`wait()` 将返回。 (2认同)
  • @nyuszika7h,`$$` 可能是只读的,但 `IFS` 不是。试试`IFS=0123456789; 例如,在 `ksh` 中挂起`。 (2认同)