From d10c45c232375c4c6ff9b60c1e17b92fc558668b Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Fri, 4 Jan 2019 23:59:29 +0200 Subject: [PATCH 1/2] Fix https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ... --- patch.sh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/patch.sh b/patch.sh index b5e23d8..183a106 100755 --- a/patch.sh +++ b/patch.sh @@ -89,25 +89,25 @@ if [[ ( ! "$patch" ) || ( ! "$object" ) ]]; then fi if [[ $rollback_flag ]]; then - if [[ -f $backup_path/"$object".$driver_version ]]; then - cp -p $backup_path/"$object".$driver_version \ - $driver_dir/"$object".$driver_version + if [[ -f "$backup_path/$object.$driver_version" ]]; then + cp -p "$backup_path/$object.$driver_version" \ + "$driver_dir/$object.$driver_version" echo "Restore from backup $object.$driver_version" else echo "Backup not found. Try to patch first." exit 1; fi else - if [[ ! -f $backup_path/"$object".$driver_version ]]; then - echo "Attention! Backup not found. Copy current libnvidia-encode to backup." - mkdir -p $backup_path - cp -p $driver_dir/"$object".$driver_version \ - $backup_path/"$object".$driver_version + if [[ ! -f "$backup_path/$object.$driver_version" ]]; then + echo "Attention! Backup not found. Copy current $object to backup." + mkdir -p "$backup_path" + cp -p "$driver_dir/$object.$driver_version" \ + "$backup_path/$object.$driver_version" fi - sha1sum $backup_path/"$object".$driver_version - sed "$patch" $backup_path/"$object".$driver_version > \ - $driver_dir/"$object".$driver_version - sha1sum $driver_dir/"$object".$driver_version + sha1sum "$backup_path/$object.$driver_version" + sed "$patch" "$backup_path/$object.$driver_version" > \ + "$driver_dir/$object.$driver_version" + sha1sum "$driver_dir/$object.$driver_version" ldconfig echo "Patched!" fi From 5182417990f3b8289b8bd2138779541193814b29 Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Sat, 5 Jan 2019 00:36:48 +0200 Subject: [PATCH 2/2] strict mode of bash operation: pipefail, stop on errors, stop on undefined variables fixes https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... fixes nvidia-smi exit code checking bug. without pipefail check always successful because of `head` command. --- patch.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/patch.sh b/patch.sh index 183a106..44350a9 100755 --- a/patch.sh +++ b/patch.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -euo pipefail + backup_path="/opt/nvidia/libnvidia-encode-backup" silent_flag='' rollback_flag='' @@ -68,18 +70,15 @@ declare -A object_list=( ["415.25"]='libnvcuvid.so' ) -driver_version=$(/usr/bin/nvidia-smi --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) -if [[ ! $? -eq 0 ]]; then + +if ! driver_version=$(/usr/bin/nvidia-smi --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) ; then echo 'Something went wrong. Check nvidia driver' exit 1; fi echo "Detected nvidia driver version: $driver_version" -patch="${patch_list[$driver_version]}" -object="${object_list[$driver_version]}" - -if [[ ( ! "$patch" ) || ( ! "$object" ) ]]; then +if [[ ! -v "patch_list[$driver_version]" || ! -v "object_list[$driver_version]" ]]; then echo "Patch for this ($driver_version) nvidia driver not found." 1>&2 echo "Available patches for: " 1>&2 for drv in "${!patch_list[@]}"; do @@ -88,6 +87,9 @@ if [[ ( ! "$patch" ) || ( ! "$object" ) ]]; then exit 1; fi +patch="${patch_list[$driver_version]}" +object="${object_list[$driver_version]}" + if [[ $rollback_flag ]]; then if [[ -f "$backup_path/$object.$driver_version" ]]; then cp -p "$backup_path/$object.$driver_version" \