Revised to handle letting pre-existing mountpoints alone. Assuming the code is in this format, where the lines that might cause trouble have one command per line, which is either mount or umount:
mount a x mount b y setup_thing mount c z do_something umount z cleanup_thing umount y umount x This bash kludge might work... copy this code to the 2nd line of the script:
source <(mount | cut -d' ' -f3 | sed 's/.*/^u\?mount [^[:space:]]\* &$/' | grep -v -f - $0) ;| exitsed 2d | exec sh -s -- "$@" How it (theoretically) works:
- Use
mountandcutto make a list of pre-existing mountpoints, to be left alone. - Use
sedto create a list ofgreppatterns that match thosemountorumountcommands which use any of those mountpoints. - Use
grep -vto search the current script for any lines that don't match the previous list ofgreppatterns. Leaving all the code that's fit to run. - Use
sourcesedto remove the 2nd line, to prevent recursion. - Use
exec sh -s -- "$@"to run only that code.., along with any command line arguments. - And Nothing past the
exitexecline is run.
Test beforehand by changing sourceexec to cat # and examining the output. If it looks good, put sourceexec back in.