Edit shell script while it's running

2019-01-08 13:10发布

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.

标签: linux shell csh
10条回答
Lonely孤独者°
2楼-- · 2019-01-08 13:39

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-08 13:39

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!

查看更多
闹够了就滚
4楼-- · 2019-01-08 13:47

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
查看更多
放我归山
5楼-- · 2019-01-08 13:48

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.

查看更多
狗以群分
6楼-- · 2019-01-08 13:49

[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:

#!/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.

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.

查看更多
Summer. ? 凉城
7楼-- · 2019-01-08 13:55

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.

查看更多
登录 后发表回答