Skip to main content
Made code POSIX.
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54

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:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match those mount or umount commands which use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use sourcesed to remove the 2nd line, to prevent recursion.
  5. Use exec sh -s -- "$@" to run only that code.., along with any command line arguments.
  6. And Nothing past the exitexec line is run.

Test beforehand by changing sourceexec to cat # and examining the output. If it looks good, put sourceexec back in.

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) ; exit

How it (theoretically) works:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match those mount or umount commands which use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use source to run only that code...
  5. And exit.

Test beforehand by changing source to cat and examining the output. If it looks good, put source back in.

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 kludge might work... copy this code to the 2nd line of the script:

mount | cut -d' ' -f3 | sed 's/.*/^u\?mount [^[:space:]]\* &$/' | 
  grep -v -f - $0 | sed 2d | exec sh -s -- "$@"

How it (theoretically) works:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match those mount or umount commands which use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use sed to remove the 2nd line, to prevent recursion.
  5. Use exec sh -s -- "$@" to run only that code, along with any command line arguments. Nothing past the exec line is run.

Test beforehand by changing exec to cat # and examining the output. If it looks good, put exec back in.

Tweak.
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54

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) ; exit

How it (theoretically) works:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match those mount or umount commands thatwhich use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use source to run only that code...
  5. And exit.

Test beforehand by changing source to cat and examining the output. If it looks good, put source back in.

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 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) ; exit

How it (theoretically) works:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match mount or umount commands that use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use source to run only that code...
  5. And exit.

Test beforehand by changing source to cat and examining the output. If it looks good, put source back in.

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) ; exit

How it (theoretically) works:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match those mount or umount commands which use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use source to run only that code...
  5. And exit.

Test beforehand by changing source to cat and examining the output. If it looks good, put source back in.

Different approach.
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54

Avoid those problems by only continuing if eachRevised 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 is successfulor umount:

mount a x &&  
mount b y && 
setup_thing
mount c z &&  
do_something
umount z
cleanup_thing
umount y
umount x

Or if do_something should happen in any eventThis kludge might work... copy this code to the 2nd line of the script:

source <(mount a| xcut ;-d' mount' b-f3 y| ;
         sed 's/.*/^u\?mount c[^[:space:]]\* z
do_something&$/' | 
umount z y x      grep -v -f - $0) ; exit

How it (theoretically) works:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match mount or umount commands that use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use source to run only that code...
  5. And exit.

Test beforehand by changing source to cat and examining the output. If it looks good, put source back in.

Avoid those problems by only continuing if each mount is successful:

mount a x && mount b y && mount c z && do_something
umount z y x

Or if do_something should happen in any event:

mount a x ; mount b y ; mount c z
do_something
umount z y x

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 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) ; exit

How it (theoretically) works:

  1. Use mount and cut to make a list of pre-existing mountpoints, to be left alone.
  2. Use sed to create a list of grep patterns that match mount or umount commands that use any of those mountpoints.
  3. Use grep -v to search the current script for any lines that don't match the previous list of grep patterns. Leaving all the code that's fit to run.
  4. Use source to run only that code...
  5. And exit.

Test beforehand by changing source to cat and examining the output. If it looks good, put source back in.

Typo.
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54
Loading
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54
Loading