Here's an impossibility result: The behavior of a script may depend on information that it wrote to a file earlier. If you don't actually allow it to write to the file (but make the script believe it did write to the file), then you may influence the script's behavior in a way that wouldn't happen if you ran it "for real".
For instance:
#!/bin/bash
write_bit_to_file () {
# write a random bit to a file
echo $((RANDOM % 2)) >> file.txt
}
get_bit_from_file () {
# read the random bit from the file
tail -1 file.txt
}
# MAIN SCRIPT
#############
# ... do some stuff, save some info for later ...
write_bit_to_file
# ... do more stuff, retrieve info from file ...
if (( $(get_bit_from_file) )); then
# access foo.txt and do something
echo "I'm going to access foo.txt"
else
# access bar.txt and do something
echo "I'm going to access bar.txt"
fi
This is obviously a very artificial script, but I hope you get the point: if the script does not actually write to file.txt
, but believes it does, you will either get an error (e.g., if file.txt
does not exist) or unexpected behavior (e.g., if file.txt
does exist, but contains other information than expected, because write_bit_to_file
did not actually write to the file). If you ran the script for real, however, it would behave as expected (e.g., randomly access either foo.txt
or bar.txt
).
What may be possible is to write a monitor that executes your script, observes the files it writes to, makes a backup of these files before that happens, and at the end of the script restores those files to their original state. But that would be pretty nasty ;)
Edit: In this answer, muru suggests a really good way to implement something akin to this monitor, and even better, since it never affects your actual root filesystem and preserves modification of files done by scripts to be re-used later. That's the way to go! :-)
chroot(8)
andstrace(1)
in conjunction with a union mount with a file system like overlayfs or aufs? – David Foerster Nov 12 '14 at 16:15