bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug #68131] Shell script misbehaves when edited


From: Greg Wooledge
Subject: Re: [bug #68131] Shell script misbehaves when edited
Date: Mon, 9 Mar 2026 07:13:11 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Mon, Mar 09, 2026 at 08:36:02 +0100, Félix Hauri via Bug reports for the GNU 
Bourne Again SHell wrote:
> Le Sun, Mar 08, 2026 at 06:14:32PM -0400, Greg Wooledge a écrit :
> > On Sun, Mar 08, 2026 at 15:54:06 -0600, Stan Marsh wrote:
> > > >When a shell script is edited while running, it appears the current 
> > > >invocation takes the update right away.
> > It would be the work of a few minutes to come up with an example.
> > A lot longer to come up with a *good* example.
> 
> Running this:
> 
>     #!/bin/bash
> 
>     echo "This is a test script"
> 
>     script="$(sed \$s/last/second-to-last/ <$0)"
>     echo "$script" >$0
> 
>     echo 'echo This line was added while running' >>$0
> 
>     echo This is the last line.
> 
> could produce something like:
> 
>     This is a test script
>     This is the last line.
>     ./script.sh: line 11: ast: command not found
>     This line was added while running

The example I thought of was:

====================================================
#!/bin/bash
set -e

# Cool Script v1.3.2 by cooldude@hotmail.com

echo Cool
sleep 60
# rm -rf /something/important
# heh heh heh
echo Script
====================================================

Imagine this is running, and then version 1.3.3 is released, with
the only changes being the version number and the author's email
address, which is now cooldude@gmail.com.

The running instance has just read and is now executing the sleep command.
That puts the file position at the # character on the commented-out
rm command.

The new script is installed by overwriting the original.  The new version
is 2 bytes shorter, so the file position is now 2 bytes further in,
which puts it at the "rm".

So, after the update, the sleep command eventually finishes, and then
the running instance executes the rm command, which either succeeds
(bad), or fails (less bad).  If it fails, then the set -e kicks in,
and the final echo is never executed, so either way you lose.

As I said before, it would take longer to come up with a *good*
example, and this isn't a very good one.  But it's simple enough
to understand, and illustrates the "problem".



reply via email to

[Prev in Thread] Current Thread [Next in Thread]