Edit shell script while it's running

前端 未结 11 1512
余生分开走
余生分开走 2020-11-27 03:18

Can you edit a shell script while it\'s running and have the changes affect the running script?

I\'m curious about the specific case of a csh script I have that batc

相关标签:
11条回答
  • 2020-11-27 03:48

    I don't have csh installed, but

    #!/bin/sh
    echo Waiting...
    sleep 60
    echo Change didn't happen
    

    Run that, quickly edit the last line to read

    echo Change happened
    

    Output is

    Waiting...
    /home/dave/tmp/change.sh: 4: Syntax error: Unterminated quoted string
    

    Hrmph.

    I guess edits to the shell scripts don't take effect until they're rerun.

    0 讨论(0)
  • 2020-11-27 03:49

    Try this... create a file called bash-is-odd.sh:

    #!/bin/bash
    echo "echo yes i do odd things" >> bash-is-odd.sh
    

    That demonstrates that bash is, indeed, interpreting the script "as you go". Indeed, editing a long-running script has unpredictable results, inserting random characters etc. Why? Because bash reads from the last byte position, so editing shifts the location of the current character being read.

    Bash is, in a word, very, very unsafe because of this "feature". svn and rsync when used with bash scripts are particularly troubling, because by default they "merge" the results... editing in place. rsync has a mode that fixes this. svn and git do not.

    I present a solution. Create a file called /bin/bashx:

    #!/bin/bash
    source "$1"
    

    Now use #!/bin/bashx on your scripts and always run them with bashx instead of bash. This fixes the issue - you can safely rsync your scripts.

    Alternative (in-line) solution proposed/tested by @AF7:

    {
       # your script
    } 
    exit $?
    

    Curly braces protect against edits, and exit protects against appends. Of course, we'd all be much better off if bash came with an option, like -w (whole file), or something that did this.

    0 讨论(0)
  • 2020-11-27 03:50

    Good question! Hope this simple script helps

    #!/bin/sh
    echo "Waiting..."
    echo "echo \"Success! Edits to a .sh while it executes do affect the executing script! I added this line to myself during execution\"  " >> ${0}
    sleep 5
    echo "When I was run, this was the last line"
    

    It does seem under linux that changes made to an executing .sh are enacted by the executing script, if you can type fast enough!

    0 讨论(0)
  • 2020-11-27 03:50

    Use Zsh instead for your scripting.

    AFAICT, Zsh does not exhibit this frustrating behavior.

    0 讨论(0)
  • 2020-11-27 03:52

    Break your script into functions, and each time a function is called you source it from a separate file. Then you could edit the files at any time and your running script will pick up the changes next time it gets sourced.

    foo() {
      source foo.sh
    }
    foo
    
    0 讨论(0)
提交回复
热议问题