Change the shell script while it works

Can you edit the shell of the script while it is running and affect the operation of the script?

I am curious to know about a particular case of csh script. I have this installment that runs a bunch of different build options and works all night. If something happens to me in the middle of the operation, I would like to go in and add additional commands or comment on incomplete ones.

If this is not possible, is there any shell or batch mechanism that would allow me to do this?

Of course, I tried, but there will be hours before I see if this works or not, and I wonder what happens or does not happen behind the scenes.

+77
linux shell csh
Aug 03 '10 at 15:49
source share
8 answers

Scripts do not work this way; the executing copy is independent of the source file you are editing. The next time the script is run, it will be based on the last saved version of the source file.

It might be wise to split this script into several files and run them separately. This will reduce runtime to failure. (i.e. split the package into one script to create scripts, run each of them separately to see which one is causing the problem).

-56
Aug 03 '10 at 15:54
source share

[edit] See also this answer, section 3 for workarounds.

It acts , at least bash in my environment, but is very nasty . See These Codes. First a.sh :

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

b.sh :

 #!/bin/sh echo "First echo" read y echo "Inserted" echo "$y" # echo "That 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 all.
 That all. 

It is unpredictable, so dangerous. See this answer, section 3 for workarounds.

[added] The exact behavior depends on one additional new line and, possibly, on your taste of Unix, file system, etc. If you just want to see some influences, just add "echo foo / bar" to b.sh before and / or after the read line.

+45
Jun 10 2018-11-11T00:
source share

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

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

This demonstrates that bash really interprets the "when you go" script. Indeed, editing a long-term script has unpredictable results, inserting random characters, etc. Why? Since bash reads from the last byte position, so editing changes the location of the current character that is being read.

Bash, in a word, is very, very dangerous because of this "function". svn and rsync when used with bash scripts are especially troubling because by default they "combine" the results ... in-place editing. Rsync has a mode that fixes this. svn and git not.

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

 #!/bin/bash source "$1" 

Now use #! / Bin / bashx in your scripts and always run them with "bashx" instead of bash. This fixes the problem - you can safely rsync your scripts.

Alternative (built-in) solution proposed / tested with @ AF7:

 { # your script } exit $? 

Paste brackets protect against editing, and output protects against adding. Of course, we would be much better if bash came with an option, for example, "-w" (the whole file) or something like that.

+34
Oct 17 '13 at 15:28
source share

Separate the script into functions, and each time you call the function, you are source from a separate file. You can then edit the files at any time, and your startup script will receive changes the next time it is received.

 foo() { source foo.sh } foo 
+17
Aug 03 '10 at 18:59
source share

I don't have csh, but

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

Run this, quickly edit the last line to read

 echo Change happened 

Exit

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

Hrmph.

I think that editing shell scripts does not take effect until they are restarted.

+1
Aug 03 '10 at 15:57
source share

If this is all in one script, then no, it will not work. However, if you configured it as a script driver for calling sub-scripts, then you can change the script before calling it or before it calls again if you loop, in which case I believe that these changes will be reflected in progress

+1
Aug 03 '10 at 16:10
source share

I hear no ... but what about some indirectness:

Batchrunner.sh

 Command1.sh Command2.sh 

Command1.sh

 runSomething 

Command2.sh

 runSomethingElse 

Then you should be able to edit the contents of each batch file before BatchRunner returns to it correctly?

OR

A cleaner version of BatchRunner will look in a single file, where it will run one line at a time in sequence. Then you should be able to edit this second file while the first one is working correctly?

0
Aug 03 '10 at 16:05
source share

usually, rarely to edit your script while it is running. All you have to do is check control over your operations. Use if / else statements to check conditions. If something does not work, then do it, otherwise do it. This is the way.

-5
Aug 03 '10 at 15:57
source share



All Articles