I want to do this
if nvidia-gpu available; then
do something
else
do something-else
fi
How can i achieve this using bash script?
I want to do this
if nvidia-gpu available; then
do something
else
do something-else
fi
How can i achieve this using bash script?
It depends a little on your system and the tools installed. If, e. g., you can use lshw
, this would possibly be enough:
if [[ $(lshw -C display | grep vendor) =~ Nvidia ]]; then
do something
else
do something-else
fi
Explanation:
lshw
lists your hardware devices-C display
limits this to display charachteristics onlygrep vendor
filters out the line containing vendor specification=~ Nvidia
checks if that line matches the expression "Nvidia"If you can't use lshw
you might be able to achieve something similar using lspci
or reading values from the /proc
file system.
It might be possible, that for this to work you need to have working Nvidia drivers installed for the graphics card. But I assume, your use case wouldn't make sense without them anyway. :)
If lspci
is available/acceptable.
#!/usr/bin/env bash
# Some distro requires that the absolute path is given when invoking lspci
# e.g. /sbin/lspci if the user is not root.
gpu=$(lspci | grep -i '.* vga .* nvidia .*')
shopt -s nocasematch
if [[ $gpu == *' nvidia '* ]]; then
printf 'Nvidia GPU is present: %s\n' "$gpu"
echo do_this
else
printf 'Nvidia GPU is not present: %s\n' "$gpu"
echo do_that
fi
Things get more complicated when dealing with dual GPU's like Optimus and the likes.
lspci | grep -i vga
Output
01:00.0 VGA compatible controller: NVIDIA Corporation GP107M [GeForce GTX 1050 3 GB Max-Q] (rev a1)
05:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Picasso (rev c2)
One work around is to count how many GPU's is/are available.
lspci | grep -ci vga
Output
2
Just add another test if there are more than one VGA output.
total=$(lspci | grep -ci vga)
if ((total > 1)); then
echo "$total"
echo do_this
fi
I'm Using GNU grep
so, not sure if that flag will work on non GNU grep.
When it is loaded with modprobe
or insmod
then you can do something like...
(lsmod | grep -q nameofyourdriver) && echo 'already there' || echo 'load it'