Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit shell script while it's running

Tags:

linux

shell

csh

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.

like image 960
ack Avatar asked Aug 03 '10 15:08

ack


People also ask

Can I edit a shell script while it's running?

The shell is not like other scripting languages. The shell reads and executes commands one by one, so the simple answer is "yes, editing a running script can affect it", but it's important to understand the details.

Can a Bash script modify itself?

One of the requirements I have is that the script must be self contained; no other files are to accompany the script and there are to be no environment variables. This would require the script to be able to edit itself.

How do I change a Bash script?

Bash shell read commands such as ls, date and others typed into a terminal and then run them. That is the primary function of bash shell. To change your shell to bash use the chsh command.


2 Answers

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.

like image 110
teika kazura Avatar answered Oct 20 '22 18:10

teika kazura


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.

like image 27
Erik Aronesty Avatar answered Oct 20 '22 16:10

Erik Aronesty