Skip to main content
added 246 characters in body
Source Link
l0b0
  • 9k
  • 21
  • 36

Some suggestions:

  • #!/usr/bin/env bash is a more portable shebang line.
  • function prepare_volumes_for_exit() can be simplified to prepare_volumes_for_exit().
  • Using sudo within a script is generally discouraged, since it makes it easy to do really bad things without explicit user consent. Instead, remove the sudos and tell the user to run the whole script as root.
  • format_string is an anti-pattern. First, because quotes are actually valid characters in paths, but more importantly because it is a strong indication that you're expecting the user to wrongly input broken strings.
  • ! [ $# -eq 2 ] can be simplified as [ $# -ne 2 ].
  • while read -r drive_number volume_location vc_mapper mount_location
    …
    done <<< $(veracrypt -t -l)
    
    can be improved as
    while read -r -u9 drive_number volume_location vc_mapper mount_location
    …
    done 9< <(veracrypt -t -l)
    
    This way, no word splitting happens on the input, and commands within the while loop which happen to read from stdin won't break the loop.
  • exit 0 is redundant.
  • By convention, scripts should be silent if there is nothing important to report, and any error messages should go to standard error rather than standard output.

Some suggestions:

  • #!/usr/bin/env bash is a more portable shebang line.
  • function prepare_volumes_for_exit() can be simplified to prepare_volumes_for_exit().
  • Using sudo within a script is generally discouraged, since it makes it easy to do really bad things without explicit user consent. Instead, remove the sudos and tell the user to run the whole script as root.
  • while read -r drive_number volume_location vc_mapper mount_location
    …
    done <<< $(veracrypt -t -l)
    
    can be improved as
    while read -r -u9 drive_number volume_location vc_mapper mount_location
    …
    done 9< <(veracrypt -t -l)
    
    This way, no word splitting happens on the input, and commands within the while loop which happen to read from stdin won't break the loop.

Some suggestions:

  • #!/usr/bin/env bash is a more portable shebang line.
  • function prepare_volumes_for_exit() can be simplified to prepare_volumes_for_exit().
  • Using sudo within a script is generally discouraged, since it makes it easy to do really bad things without explicit user consent. Instead, remove the sudos and tell the user to run the whole script as root.
  • format_string is an anti-pattern. First, because quotes are actually valid characters in paths, but more importantly because it is a strong indication that you're expecting the user to wrongly input broken strings.
  • ! [ $# -eq 2 ] can be simplified as [ $# -ne 2 ].
  • while read -r drive_number volume_location vc_mapper mount_location
    …
    done <<< $(veracrypt -t -l)
    
    can be improved as
    while read -r -u9 drive_number volume_location vc_mapper mount_location
    …
    done 9< <(veracrypt -t -l)
    
    This way, no word splitting happens on the input, and commands within the while loop which happen to read from stdin won't break the loop.
  • exit 0 is redundant.
  • By convention, scripts should be silent if there is nothing important to report, and any error messages should go to standard error rather than standard output.
Source Link
l0b0
  • 9k
  • 21
  • 36

Some suggestions:

  • #!/usr/bin/env bash is a more portable shebang line.
  • function prepare_volumes_for_exit() can be simplified to prepare_volumes_for_exit().
  • Using sudo within a script is generally discouraged, since it makes it easy to do really bad things without explicit user consent. Instead, remove the sudos and tell the user to run the whole script as root.
  • while read -r drive_number volume_location vc_mapper mount_location
    …
    done <<< $(veracrypt -t -l)
    
    can be improved as
    while read -r -u9 drive_number volume_location vc_mapper mount_location
    …
    done 9< <(veracrypt -t -l)
    
    This way, no word splitting happens on the input, and commands within the while loop which happen to read from stdin won't break the loop.