Emacs workflow to edit Bash scripts while they run

后端 未结 4 776
夕颜
夕颜 2020-12-06 18:15

Bash can give unexpected results if one edits a script while the script is running. But it would often be very convenient to be able to edit a temporary copy of a script tha

4条回答
  •  天涯浪人
    2020-12-06 19:01

    [edit] The best workaround is done in shell script side, not in Emacs. In short, _put all in a brace, and put "exit" before the closing brace. Read the linked answer well to avoid pitfalls.

    In this reply, I supplement Gilles's answer with theory.

    The reason deletion is safe but modification is not is that in Unix, a deleted file gets inaccessible from the file system, but the processes that had opened the file (here, /bin/bash) can still read it, as explained here. It's not that bash does something special, like buffering the entire file, but it simply reads. (Even after the deletion, you can recover the script while it's running, as explained here. In bash, the script seems to be always opened as 255, /proc//fd/255.)

    [edit] My old answer included Emacs solution and co, as per OP's request. But after years of experience, I'm sure that the above pointer is the best, far better than any other attempts.)

提交回复
热议问题