Edit shell script while it's running

前端 未结 11 1511
余生分开走
余生分开走 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:30

    If this is all in a single script, then no it will not work. However, if you set it up as a driver script calling sub-scripts, then you might be able to change a sub-script before it's called, or before it's called again if you're looping, and in that case I believe those changes would be reflected in the execution.

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

    usually, it uncommon to edit your script while its running. All you have to do is to put in control check for your operations. Use if/else statements to check for conditions. If something fail, then do this, else do that. That's the way to go.

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

    Scripts don't work that way; the executing copy is independent from the source file that you are editing. Next time the script is run, it will be based on the most recently saved version of the source file.

    It might be wise to break out this script into multiple files, and run them individually. This will reduce the execution time to failure. (ie, split the batch into one build flavor scripts, running each one individually to see which one is causing the trouble).

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

    An interesting side note - if you are running a Python script it does not change. (This is probably blatantly obvious to anyone who understands how shell runs Python scripts, but thought it might be a useful reminder for someone looking for this functionality.)

    I created:

    #!/usr/bin/env python3
    import time
    print('Starts')
    time.sleep(10)
    print('Finishes unchanged')
    

    Then in another shell, while this is sleeping, edit the last line. When this completes it displays the unaltered line, presumably because it is running a .pyc? Same happens on Ubuntu and macOS.

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

    I'm hearing no... but what about with some indirection:

    BatchRunner.sh

    Command1.sh
    Command2.sh
    

    Command1.sh

    runSomething
    

    Command2.sh

    runSomethingElse
    

    Then you should be able to edit the contents of each command file before BatchRunner gets to it right?

    OR

    A cleaner version would have BatchRunner look to a single file where it would consecutively run one line at a time. Then you should be able to edit this second file while the first is running right?

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

    It does affect, at least bash in my environment, but in very unpleasant way. See these codes. First a.sh:

    #!/bin/sh
    
    echo "First echo"
    read y
    
    echo "$y"
    
    echo "That's all."
    

    b.sh:

    #!/bin/sh
    
    echo "First echo"
    read y
    
    echo "Inserted"
    
    echo "$y"
    
    # echo "That's all."
    

    Do

    $ cp a.sh run.sh
    $ ./run.sh
    $ # open another terminal
    $ cp b.sh run.sh  # while 'read' is in effect
    $ # Then type "hello."
    

    In my case, the output is always:

    hello
    hello
    That's all.
    That's all.

    (Of course it's far better to automate it, but the above example is readable.)

    [edit] This is unpredictable, thus dangerous. The best workaround is , as described here put all in a brace, and before the closing brace, put "exit". Read the linked answer well to avoid pitfalls.

    [added] The exact behavior depends on one extra newline, and perhaps also on your Unix flavor, filesystem, etc. If you simply want to see some influences, simply add "echo foo/bar" to b.sh before and/or after the "read" line.

    0 讨论(0)
提交回复
热议问题