在Linux中更改目录并退出该目录中的程序

  • 本文关键字:程序 退出 Linux linux nim-lang
  • 更新时间 :
  • 英文 :


我有一个简单的程序,我从一个终端运行,它可以更改工作目录(通过setCurrentDir(并执行一些其他工作,在该文件夹中创建一些新文件。程序完成后,我希望终端提示保留在该文件夹中,而不是返回到原始目录。

据我所知,这是一个linux问题,而不是尼姆问题,但我想知道尼姆是否有任何变通方法。

如果不根据"如何设置父进程的工作目录"使用gdb对父进程进行黑客攻击,这应该是不可能的,事实上也是不可能的?

的确,不要这样做。

import std/[os,osproc,strformat]
proc getppid():int32{.importc,header:"unistd.h".}
let f = open("/tmp/gdb_cmds",fmWrite)
f.write(&"""p (int) chdir ("{commandLineParams()[0]}")
detach
quit
""")
f.close()
discard execCmd(&"sudo gdb -p {getppid()} -batch -x /tmp/gdb_cmds;rm /tmp/gdb_cmds")

用法:

/tmp$ nim c cd.nim
/tmp$ mkdir child
/tmp$ touch you_are_in_parent_dir
/tmp$ touch child/you_are_in_child_dir
/tmp$ ls
you_are_in_parent_dir
cd.nim
cd
child
/tmp$ ./cd child
/tmp$ ls #<---notice pwd is cached
you_are_in_child_dir
/tmp$

最新更新