I can capture the disk devices of my Linux machine with the following command:
lsblk -lnb | numfmt --to=iec --field=4 | grep disk | awk '{print $1}'
sda
sdb
sdc
sdd
In my bash script I used the line below to test if the script's argument matches one of the disks on my machine:
if [[ ` lsblk -lnb | numfmt --to=iec --field=4 | grep disk | awk '{print $1}' | grep -c $arg ` != 0 ]]
then
echo “argument is a disk”
.
.
.
fi
I run the script as
bash /tmp/verify_disks <some arg>
But in case $arg
is by mistake as arg=”—help”
, or in case $arg
is null, I get the following exception:
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
So what is the right approach to check if $arg
includes one of the disks in my Linux machines without exception?