Skip to content

Instantly share code, notes, and snippets.

@xiaoxi-ij478
Created October 26, 2023 14:58
Show Gist options
  • Save xiaoxi-ij478/5fe41544a4a57006ac4fa6a12c39bc64 to your computer and use it in GitHub Desktop.
Save xiaoxi-ij478/5fe41544a4a57006ac4fa6a12c39bc64 to your computer and use it in GitHub Desktop.
Update NVIDIA driver on any linux box
#!/bin/bash
LC_ALL=C.UTF-8 LANG=C.UTF-8 checkonly=false
check_terminal()
{
tty -s || exec xterm "$0"
}
check_root()
{
[ "$(id -u)" != 0 ] && exec sudo "$0"
}
check_x()
{
pidof -q Xorg || return
echo "Stop X & switch to virtual terminal before updating!"
echo "If you just want to check if there're any updates,"
echo "press c, or anything else to exit."
read -r -N1 -p"Please select one: "
case $REPLY in
c) checkonly="true"; echo;;
*) exit 1;;
esac
}
check_version()
{
echo "Checking..."
hiv=$(
curl -fsSL \
https://www.nvidia.cn/drivers/unix/linux-amd64-display-archive |
grep -FA1 \
'driverResults.aspx' |
grep -Eo "[0-9]{3}\.[0-9]{2,3}(\.[0-9]{2})?" |
sort -V |
tail -n1
)
hili="https://cn.download.nvidia.cn/XFree86/Linux-x86_64/$hiv/NVIDIA-Linux-x86_64-$hiv.run"
}
install_update()
{
nv=$(
modinfo nvidia |
grep "^version:" |
cut -d: -f2
)
if printf "%s\n%s" $hiv $nv | sort -CV
then
echo "No update available."
read -r -n1 -p"Press any key to quit..."
exit 0
fi
echo "A new update for Nvidia Driver is available: $hiv!"
if ! $checkonly
then
echo "Do you want to update it? [y/n]"
while true
do
read -r -N1
case $REPLY in
[Yy]) break;;
[Nn]) exit 0;;
*) echo "Wrong reply! Input again!";;
esac
done
echo "Downloading & Running..."
wget -O/tmp/nvidia.run "$hili"
bash /tmp/nvidia.run
fi
read -n1 -p"Press any key to quit..."
exit 0
}
check_terminal
check_root
check_x
check_version
install_update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment