Skip to content

Commit 902679b

Browse files
committed
Convert use of 'which' to 'hash' and 'command -v'
1 parent 0c768c8 commit 902679b

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

format-udf.sh

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ set -euf -o pipefail
3232
SUDO=''
3333
if [[ $(id -u) -ne 0 ]]; then
3434
# verify that 'sudo' is present before assuming we can use it
35-
if [[ ! -x $(which sudo 2>/dev/null) ]]; then
35+
if ! hash sudo 2>/dev/null; then
3636
echo "[-] Dependencies unmet. Please verify that 'sudo' is installed, executable, and in the PATH." >&2
3737
echo "Alternatively, you may also re-run this script as root." >&2
3838
exit 1
@@ -256,7 +256,7 @@ while getopts ":b:fp:w:vh" opt; do
256256
exit 1
257257
fi
258258
if [[ "$WIPE_METHOD" = "scrub" ]]; then
259-
if [[ ! -x $(which scrub 2>/dev/null) ]]; then
259+
if ! hash scrub 2>/dev/null; then
260260
echo "[-] Dependencies unmet. Please verify that the following are installed, executable, and in the PATH: scrub" >&2
261261
exit 1
262262
fi
@@ -348,7 +348,7 @@ if [[ "$PARENT_DEVICE" != "$DEVICE" ]]; then
348348
echo "You are attempting to format a single partition (as opposed to entire device)."
349349
echo "For maximal compatibility, the recommendation is to format the entire device."
350350
echo "If you continue, the resultant UDF partition will not be recognized on macOS."
351-
351+
352352
if [[ -z $FORCE ]]; then
353353
read -rp "Type 'yes' if you would like to continue anyway: " YES_CASE
354354
YES=$(echo "$YES_CASE" | tr '[:upper:]' '[:lower:]')
@@ -364,18 +364,18 @@ fi
364364
###############################################################################
365365

366366
echo "[+] Testing dependencies..."
367-
if [[ ! -x $(which cat 2>/dev/null) ]] ||
368-
[[ ! -x $(which grep 2>/dev/null) ]] ||
369-
[[ ! -x $(which mount 2>/dev/null) ]] ||
370-
[[ ! -x $(which test 2>/dev/null) ]] ||
371-
[[ ! -x $(which true 2>/dev/null) ]] ||
372-
[[ ! -x $(which false 2>/dev/null) ]] ||
373-
[[ ! -x $(which awk 2>/dev/null) ]] ||
374-
[[ ! -x $(which printf 2>/dev/null) ]] ||
375-
[[ ! -x $(which sed 2>/dev/null) ]] ||
376-
[[ ! -x $(which tr 2>/dev/null) ]] ||
377-
[[ ! -x $(which dd 2>/dev/null) ]] ||
378-
[[ ! -x $(which xxd 2>/dev/null) ]]; then
367+
if ! hash cat 2>/dev/null ||
368+
! hash grep 2>/dev/null ||
369+
! hash mount 2>/dev/null ||
370+
! hash test 2>/dev/null ||
371+
! hash true 2>/dev/null ||
372+
! hash false 2>/dev/null ||
373+
! hash awk 2>/dev/null ||
374+
! hash printf 2>/dev/null ||
375+
! hash sed 2>/dev/null ||
376+
! hash tr 2>/dev/null ||
377+
! hash dd 2>/dev/null ||
378+
! hash xxd 2>/dev/null; then
379379
echo "[-] Dependencies unmet. Please verify that the following are installed, executable, and in the PATH: cat, grep, mount, test, true, false, awk, printf, sed, tr, dd, xxd" >&2
380380
exit 1
381381
fi
@@ -384,9 +384,9 @@ fi
384384
# ensure have required drive info tool
385385
echo -n "[+] Looking for drive info tool..."
386386
# `true` is so that a failure here doesn't cause entire script to exit prematurely
387-
TOOL_BLOCKDEV=$($SUDO which blockdev 2>/dev/null) || true
387+
TOOL_BLOCKDEV=$(command -v blockdev 2>/dev/null) || true
388388
# `true` is so that a failure here doesn't cause entire script to exit prematurely
389-
TOOL_IOREG=$(which ioreg 2>/dev/null) || true
389+
TOOL_IOREG=$(command -v ioreg 2>/dev/null) || true
390390
if [[ -x "$TOOL_BLOCKDEV" ]]; then
391391
TOOL_DRIVE_INFO=$TOOL_BLOCKDEV
392392
elif [[ -x "$TOOL_IOREG" ]]; then
@@ -402,9 +402,9 @@ echo " using $TOOL_DRIVE_INFO"
402402
# ensure have required drive listing tool
403403
echo -n "[+] Looking for drive listing tool..."
404404
# `true` is so that a failure here doesn't cause entire script to exit prematurely
405-
TOOL_BLOCKDEV=$($SUDO which blockdev 2>/dev/null) || true
405+
TOOL_BLOCKDEV=$(command -v blockdev 2>/dev/null) || true
406406
# `true` is so that a failure here doesn't cause entire script to exit prematurely
407-
TOOL_DISKUTIL=$(which diskutil 2>/dev/null) || true
407+
TOOL_DISKUTIL=$(command -v diskutil 2>/dev/null) || true
408408
if [[ -x "$TOOL_BLOCKDEV" ]]; then
409409
TOOL_DRIVE_LISTING=$TOOL_BLOCKDEV
410410
elif [[ -x "$TOOL_DISKUTIL" ]]; then
@@ -420,7 +420,7 @@ echo " using $TOOL_DRIVE_LISTING"
420420
# ensure have required drive summary tool
421421
echo -n "[+] Looking for drive summary tool..."
422422
# `true` is so that a failure here doesn't cause entire script to exit prematurely
423-
TOOL_BLKID=$($SUDO which blkid 2>/dev/null) || true
423+
TOOL_BLKID=$(command -v blkid 2>/dev/null) || true
424424
if [[ -x "$TOOL_BLKID" ]]; then
425425
TOOL_DRIVE_SUMMARY=$TOOL_BLKID
426426
echo " using $TOOL_DRIVE_SUMMARY"
@@ -433,9 +433,9 @@ fi
433433
# ensure have required unmount tool
434434
echo -n "[+] Looking for unmount tool..."
435435
# `true` is so that a failure here doesn't cause entire script to exit prematurely
436-
TOOL_UMOUNT=$($SUDO which umount 2>/dev/null) || true
436+
TOOL_UMOUNT=$(command -v umount 2>/dev/null) || true
437437
# `true` is so that a failure here doesn't cause entire script to exit prematurely
438-
TOOL_DISKUTIL=$($SUDO which diskutil 2>/dev/null) || true
438+
TOOL_DISKUTIL=$(command -v diskutil 2>/dev/null) || true
439439
# prefer 'diskutil' if available, as it's required on macOS (even if 'umount' is present)
440440
if [[ -x "$TOOL_DISKUTIL" ]]; then
441441
TOOL_UNMOUNT=$TOOL_DISKUTIL
@@ -452,9 +452,9 @@ echo " using $TOOL_UNMOUNT"
452452
# ensure have required UDF tool
453453
echo -n "[+] Looking for UDF tool..."
454454
# `true` is so that a failure here doesn't cause entire script to exit prematurely
455-
TOOL_MKUDFFS=$($SUDO which mkudffs 2>/dev/null) || true
455+
TOOL_MKUDFFS=$(command -v mkudffs 2>/dev/null) || true
456456
# `true` is so that a failure here doesn't cause entire script to exit prematurely
457-
TOOL_NEWFS_UDF=$($SUDO which newfs_udf 2>/dev/null) || true
457+
TOOL_NEWFS_UDF=$(command -v newfs_udf 2>/dev/null) || true
458458
if [[ -x "$TOOL_MKUDFFS" ]]; then
459459
TOOL_UDF=$TOOL_MKUDFFS
460460
elif [[ -x "$TOOL_NEWFS_UDF" ]]; then
@@ -525,14 +525,14 @@ if [[ -n $PHYSICAL_BLOCK_SIZE ]]; then
525525
echo "of $LOGICAL_BLOCK_SIZE bytes and physical block size of $PHYSICAL_BLOCK_SIZE bytes."
526526
if [[ $LOGICAL_BLOCK_SIZE -eq 512 ]] && [[ $PHYSICAL_BLOCK_SIZE -eq 4096 ]]; then
527527
echo "This device is an '512 emulation' (512e) drive."
528-
elif [[ $LOGICAL_BLOCK_SIZE -eq 4096 ]] && [[ $PHYSICAL_BLOCK_SIZE -eq 4096 ]]; then
528+
elif [[ $LOGICAL_BLOCK_SIZE -eq 4096 ]] && [[ $PHYSICAL_BLOCK_SIZE -eq 4096 ]]; then
529529
echo "This device is an '4K native' (4Kn) drive."
530530
fi
531531
echo "As such, this drive will not be as compatible across operating systems as a standard"
532532
echo "drive having a logical block size of 512 bytes and a physical block size of 512 bytes."
533533
echo "For example, this drive will not be usable for read or write on Windows XP."
534534
echo "Please see the format-udf README for more information/limitations."
535-
535+
536536
if [[ -z $FORCE ]]; then
537537
read -rp "Type 'yes' if you would like to continue anyway: " YES_CASE
538538
YES=$(echo "$YES_CASE" | tr '[:upper:]' '[:lower:]')
@@ -593,7 +593,7 @@ if [[ $((TOTAL_SIZE/LOGICAL_BLOCK_SIZE)) -ge $(((2**32)-1)) ]]; then
593593
echo "and the remainder of the drive will not be used."
594594
echo "The maximum UDF file system capacity on this device is $((LOGICAL_BLOCK_SIZE/256)) TiB."
595595
echo "Please see the format-udf README for more information."
596-
596+
597597
if [[ -z $FORCE ]]; then
598598
read -rp "Type 'yes' if you would like to continue anyway: " YES_CASE
599599
YES=$(echo "$YES_CASE" | tr '[:upper:]' '[:lower:]')
@@ -622,7 +622,7 @@ fi
622622

623623
if [[ -z $FORCE ]]; then
624624
echo "The above-listed device (and partitions, if any) will be completely erased."
625-
625+
626626
read -rp "Type 'yes' if this is what you intend: " YES_CASE
627627
YES=$(echo "$YES_CASE" | tr '[:upper:]' '[:lower:]')
628628
if [[ $YES != "yes" ]]; then

0 commit comments

Comments
 (0)