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 batch runs a bunch of different build flavors and runs all night. If something occurs to me mid operation, I'd like to go in and add additional commands, or comment out un-executed ones.
If not possible, is there any shell or batch-mechanism that would allow me to do this?
Of course I've tried it, but it will be hours before I see if it worked or not, and I'm curious about what's happening or not happening behind the scenes.
I don't have csh installed, but
Run that, quickly edit the last line to read
Output is
Hrmph.
I guess edits to the shell scripts don't take effect until they're rerun.
Good question! Hope this simple script helps
It does seem under linux that changes made to an executing .sh are enacted by the executing script, if you can type fast enough!
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.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.
[edit] See also this answer, section 3 for workarounds.
It does affect, at least bash in my environment, but in very unpleasant way. See these codes. First
a.sh
:b.sh
:Do
In my case, the output is always:
This is unpredictable, thus dangerous. See this answer, section 3 for workarounds.
[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.
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:
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.