通过 SSH 运行源脚本名称时退出脚本

发布于 2024-08-30 04:55:41 字数 114 浏览 2 评论 0原文

我有一个包含许多选项的脚本,其中一个选项集应该更改目录,然后退出脚本,但是通过 ssh 运行源代码以使其在父级中更改,然后退出 SSH 是否有另一种方法这样做以使其不退出?我的脚本位于 /usr/sbin 目录中。

I have a script with a number of options in it one of the option sets is supposed to change the directory and then exit the script however running over ssh with the source to get it to change in the parent it exits SSH is there another way to do this so that it does not exit? my script is in the /usr/sbin directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2024-09-06 04:55:41

您可以尝试让脚本运行子 shell,而不是使用任何方法来“更改父级中的[目录]”(假设您让子级打印出 cd 命令并让父级执行类似eval "$(script --print-cd)")。因此,不要添加 --print-cd 选项,而是添加一个 --subshel​​l 选项来启动 $SHELL 的新实例。

d=/path/to/some/dir
#...
cd "$d"
#...
if test -n "$opt_print_cd"; then
    sq_d="$(printf %s "$d" | sed -e "s/'/'\\\\''/g")"
    printf "cd '%s'\n" "$sq_d"
elif test -n "$opt_subshell"; then
    exec "$SHELL"
fi

如果您无法编辑脚本本身,您可以制作一个包装器(假设您有权在“服务器”上创建新的持久文件):

#!/bin/sh
script='/path/to/script'
print_cd=
for a; do test "$a" = --print-cd && print_cd=yes && break; done
if test -n "$print_cd"; then 
    eval "$("$script" ${1+"$@"})" # use cd instead of eval if the script prints a bare dir path
    exec "$SHELL"
else
    exec $script" ${1+"$@"}
fi

You might try having the script run a subshell instead of whatever method it is using to “change [the directory] in the parent” (presumably you have the child print out a cd command and have the parent do something like eval "$(script --print-cd)"). So instead of (e.g.) a --print-cd option, add a --subshell option that starts a new instance of $SHELL.

d=/path/to/some/dir
#...
cd "$d"
#...
if test -n "$opt_print_cd"; then
    sq_d="$(printf %s "$d" | sed -e "s/'/'\\\\''/g")"
    printf "cd '%s'\n" "$sq_d"
elif test -n "$opt_subshell"; then
    exec "$SHELL"
fi

If you can not edit the script itself, you can make a wrapper (assuming you have permission to create new, persistent files on the ‘server’):

#!/bin/sh
script='/path/to/script'
print_cd=
for a; do test "$a" = --print-cd && print_cd=yes && break; done
if test -n "$print_cd"; then 
    eval "$("$script" ${1+"$@"})" # use cd instead of eval if the script prints a bare dir path
    exec "$SHELL"
else
    exec $script" ${1+"$@"}
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文