identify M.2 SSD from BASH Script
Solution 1:
A directory test is sufficient:
(the path /sys/class/nvme
is created by the module nvme
. However, this does not say anything about the operational state of the drive(s) or even if a drive exists, though).
#!/bin/bash
if [[ -d /sys/class/nvme ]]; then
apt-get -y install nvme-cli
fi
Or, if you prefer using globbing to check for the existence of a file(s), a for
-loop will have the advantage of avoiding a subshell:
shopt -s nullglob
for _ in /dev/nvme*; do
apt-get -y install nvme-cli; break
done
Also, if you can, you should avoid globbing as an argument, where there are alternatives, e.g., in this case, the use of echo
or printf
is preferable (which is, in reality, a loop construct).
shopt -s nullglob
if [[ -n $(echo /dev/nvme*) ]]; then
apt-get -y install nvme-cli
fi
Solution 2:
Check to see if any nvme device files exist:
if ls /dev/nvme*
then
echo "Install NVME-CLI"
fi