Skip to main content
added 202 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Since lsblk allows for JSON output, it seems natural to use jq with it to figure out whether the given argument is a disk or not:

#!/bin/sh

if lsblk -J | jq -e --arg name "$1" '.blockdevices[] | select(.type == "disk" and .name == $name)' >/dev/null
then
    printf '"%s" is a disk\n' "$1"
else
    printf '"%s" is not a disk\n' "$1"
fi

This correctly handles the cases where the first argument to the script is empty, missing, or nonsense.

The -e option to jq makes the utility exit with an exit status dependent on the last value evaluated. It will be non-zero if the last expression evaluated (the select()) is null, for example.

Since lsblk allows for JSON output, it seems natural to use jq with it to figure out whether the given argument is a disk or not:

#!/bin/sh

if lsblk -J | jq -e --arg name "$1" '.blockdevices[] | select(.type == "disk" and .name == $name)' >/dev/null
then
    printf '"%s" is a disk\n' "$1"
else
    printf '"%s" is not a disk\n' "$1"
fi

This correctly handles the cases where the first argument to the script is empty, missing, or nonsense.

Since lsblk allows for JSON output, it seems natural to use jq with it to figure out whether the given argument is a disk or not:

#!/bin/sh

if lsblk -J | jq -e --arg name "$1" '.blockdevices[] | select(.type == "disk" and .name == $name)' >/dev/null
then
    printf '"%s" is a disk\n' "$1"
else
    printf '"%s" is not a disk\n' "$1"
fi

This correctly handles the cases where the first argument to the script is empty, missing or nonsense.

The -e option to jq makes the utility exit with an exit status dependent on the last value evaluated. It will be non-zero if the last expression evaluated (the select()) is null, for example.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Since lsblk allows for JSON output, it seems natural to use jq with it to figure out whether the given argument is a disk or not:

#!/bin/sh

if lsblk -J | jq -e --arg name "$1" '.blockdevices[] | select(.type == "disk" and .name == $name)' >/dev/null
then
    printf '"%s" is a disk\n' "$1"
else
    printf '"%s" is not a disk\n' "$1"
fi

This correctly handles the cases where the first argument to the script is empty, missing, or nonsense.