after running program leave interactive shell to use

落爺英雄遲暮 提交于 2019-12-04 02:45:11
Gilles 'SO- stop being evil'

Approach 1: bash, zsh and a few other shells read a file whose name is in the ENV environment variable after the usual rc files and before the interactive commands or the script to run. However bash only does this if invoked as sh, and zsh only does this if invoked as sh or ksh, which is rather limiting.

temp_rc=$(mktemp)
cat <<'EOF' >"$temp_rc"
mycommand --option
rm -- "$0"
EOF
ENV=$temp_rc sh

Approach 2: make the shell read a different rc file, which sources the usual rc file and contains a call to the program you want to run. For example, for bash:

temp_rc=$(mktemp)
cat <<'EOF' >"$temp_rc"
mycommand --option
if [ -e ~/.bashrc ]; then . ~/.bashrc; fi
rm -- "$0"
EOF
bash --rcfile "$temp_rc"

For zsh, the file has to be called .zshrc, you can only specify a different directory.

temp_dir=$(mktemp -d)
cat <<'EOF' >"$temp_dir/.zshrc"
mycommand --option
if [ -e ~/.zshrc ]; then . ~/.zshrc; fi
rm -- $0; rmdir ${0:h}
EOF
ZDOTDIR=$temp_dir zsh

Why don't you simply start a new shell for interactive input?

#!/bin/sh
$@
exec zsh

I use this in a script to call gui programs from the shell, haven't tested it with zsh though

nohup $@ >/dev/null 2>/dev/null &
$ cat ~/bin/ish
#!/bin/zsh
bash -i <<EOF
$@ < /dev/tty
exec <> /dev/tty
EOF

$
$
$ ~/bin/ish vim
stty: standard input: Inappropriate ioctl for device

At this point vim is opened.

$ vim < /dev/tty
$ exec <> /dev/tty
$ 
$

shell is left for you to do other work. In my question the bash shell's STDIN was HEREDOC (<< EOF) so for so it not working for command who want to read from TTY. But after providing the command input from /dev/tty it start working.

I am not able to find how to correct warning

stty: standard input: Inappropriate ioctl for device
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!