我想通过 shell 运行作为参数给出的任何程序
希望该 shell 保留为交互式 shell 以便稍后使用。
#!/bin/bash
bash -i <<EOF
$@
exec <> /dev/tty
EOF
但它不适用于 zsh
#!/bin/bash
zsh -i <<EOF
$@
exec <> /dev/tty
EOF
以及是否有人知道更多改进的方法
请告诉我。
方法 1:bash、zsh 和其他一些 shell 读取名称位于ENV
环境变量位于通常的 rc 文件之后、交互式命令或要运行的脚本之前。然而,bash 仅在作为 sh 调用时才执行此操作,而 zsh 仅在作为 sh 或 ksh 调用时执行此操作,这是相当有限的。
temp_rc=$(mktemp)
cat <<'EOF' >"$temp_rc"
mycommand --option
rm -- "$0"
EOF
ENV=$temp_rc sh
方法 2:使 shell 读取不同的 rc 文件,该文件获取通常的 rc 文件并包含对要运行的程序的调用。例如,对于 bash:
temp_rc=$(mktemp)
cat <<'EOF' >"$temp_rc"
mycommand --option
if [ -e ~/.bashrc ]; then . ~/.bashrc; fi
rm -- "$0"
EOF
bash --rcfile "$temp_rc"
对于 zsh,必须调用该文件.zshrc
,您只能指定不同的目录。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)