4

I have this script, which changes NVIDIA-SETTINGS vibrance, when certain application/process is launched (in my case Counter-Strike:Global Offensive game)

Script:

#!/bin/bash

on="1023"
off="0"
dv="0"


# RESET
sleep 10
log "RESET"
nvidia-settings -a "[gpu:0]/DigitalVibrance[DFP-0]=$off"

while true; do #daemon mode
dv=`nvidia-settings -q "[gpu:0]/DigitalVibrance[DFP-0]" -t`
  if pgrep -l csgo | grep csgo_linux
  then
#  log "Process csgo_linux found"
    if [ $dv -eq $off ]; then
      nvidia-settings -a "[gpu:0]/DigitalVibrance[DFP-0]=$on"
    fi
  else
# No process found
    if [ $dv -eq $on ]; then
      nvidia-settings -a "[gpu:0]/DigitalVibrance[DFP-0]=$off"
    fi
  fi

  if [ $dv -eq $on ]; then
  sleep 5
  else
  sleep 1
  fi

done

What is wrong with this script, why it gives me these errors?

622 csgo_linux64
/home/matas/Desktop/vib-gui.sh: line 18: [: -eq: unary operator expected
/home/matas/Desktop/vib-gui.sh: line 28: [: -eq: unary operator expected
622 csgo_linux64
/home/matas/Desktop/vib-gui.sh: line 18: [: -eq: unary operator expected
/home/matas/Desktop/vib-gui.sh: line 28: [: -eq: unary operator expected
622 csgo_linux64
/home/matas/Desktop/vib-gui.sh: line 18: [: -eq: unary operator expected
/home/matas/Desktop/vib-gui.sh: line 28: [: -eq: unary operator expected
622 csgo_linux64
/home/matas/Desktop/vib-gui.sh: line 18: [: -eq: unary operator expected
/home/matas/Desktop/vib-gui.sh: line 28: [: -eq: unary operator expected

EDIT:

#!/bin/bash

on="1023"
off="0"
dv="0"


# RESET
sleep 10
nvidia-settings -a "[gpu:0]/DigitalVibrance[DFP-0]=$off"

while true; do #daemon mode
dv=`nvidia-settings -q "[gpu:0]/DigitalVibrance[DFP-0]" -t`
  if pgrep -l csgo | grep csgo_linux
  then
#  log "Process csgo_linux found"
    if [ "$dv -eq $off" ]; then
      nvidia-settings -a "[gpu:0]/DigitalVibrance[DFP-0]=$on"
    fi
  else
# No process found
    if [ "$dv" -eq "$on" ]; then
      nvidia-settings -a "[gpu:0]/DigitalVibrance[DFP-0]=$off"
    fi
  fi

  if [ "$dv -eq $on" ]; then
  sleep 5
  else
  sleep 1
  fi

done
  • Change this `$dv -eq $on` to `$dv = $on` and try again – George Udosen May 13 '17 at 16:45
  • 1
    Try quoting your variables inside the test brackets everywhere: `[ "$dv" -eq "$on" ]` – Byte Commander May 13 '17 at 16:45
  • `"$dv" -eq "$on"` to `"$dv" = "$on"`, yes forgot the quotes... – George Udosen May 13 '17 at 16:49
  • 1
    Also if `[ $dv -eq $on ]` says "-eq: unary operator expected", this means that you first variable, `$dv` is empty. Check the output of the command which you are assigning to it. – Byte Commander May 13 '17 at 16:52
  • Did something, and this appears: 14441 csgo_linux64 is it good or bad? –  May 13 '17 at 16:57
  • Quated, and removed -eq, now have this: –  May 13 '17 at 17:00
  • I guess that is the output of `pgrep -l csgo | grep csgo_linux`, searching for processes with "csgo_linux" in their name. The 14441 is the process id then. If you get such output and not any error messages, I suppose the script is working. – Byte Commander May 13 '17 at 17:00
  • /home/matas/Desktop/vib-gui.sh: line 23: [: : unary operator expected 16105 csgo_linux64 –  May 13 '17 at 17:00
  • yes grep outputs the right code that is in script terminal –  May 13 '17 at 17:01
  • Can you please [edit] your question and update the script block with your current version? – Byte Commander May 13 '17 at 17:02
  • I edited it, but now console outputs that 10 line with 'log reset' - command not found, cleared that line, console - no errors, but any effects too. –  May 13 '17 at 17:05
  • You wrote `"$dv -eq $off"` in line 17, but it must be `"$dv" -eq "$off"` – Byte Commander May 13 '17 at 17:15
  • - Byte Commander, when i write it what you say it gives error, but diffrent. –  May 13 '17 at 17:20
  • 1
    Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/58723/discussion-between-byte-commander-and-matas-esu). – Byte Commander May 13 '17 at 17:21

2 Answers2

7

To fix this specific problem

There are other issues with your script presumably dealt with in Byte Commander's answer, but for those coming in from Google, to fix this specific problem, always surround variables with double quotes. For example [ "$dv" -eq "$on" ].

Explanation

A unary operator only has one argument. A binary operator has two arguments.

For example, -eq is a binary operator, because it has two arguments and figures out whether they're equal.

When the shell sees [ 3 -eq 3 ], everything is fine, because -eq takes two arguments, and it has been given two arguments, the 3s. What if one of those was blank, though? It would be either [ -eq 3 ] or [ 3 -eq ]. Those are missing one of the arguments, so the shell gets to thinking you meant to use an operator that only has one argument, a unary operator.

Your variables can be blank, causing just this problem. To avoid the issue, surround all uses of a variable with double quotes.

Chai T. Rex
  • 5,126
  • 1
  • 24
  • 48
0

I rewrote the script for you, as discussed in chat:

#!/bin/bash

# set log_enabled="true" for status output, else log_enabled="false"
log_enabled="true"

on="1023"
off="0"
dv="0"


log () {
  if $log_enabled
    then 
      echo "$(date +%H:%M:%S) - $1"
  fi
}


log "waiting 10 seconds..."
sleep 10

log "resetting DigitalVibrance to $off (off)"
nvidia-settings -a "DigitalVibrance=$off" > /dev/null

log "beginning to watch for csgo_linux processes"
while true
do
  dv=$(nvidia-settings -q "DigitalVibrance" -t)
  log "current DigitalVibrance setting: $dv"

  if pgrep "csgo_linux" > /dev/null
    then  # if CS:GO is running
      if [ "$dv" -eq "$off" ]
        then  # if DigitalVibrance is currently off
          log "setting DigitalVibrance to $on (on)"
          nvidia-settings -a "DigitalVibrance=$on" > /dev/null
      fi

    else  # if CS:GO is not running
      if [ "$dv" -eq "$on" ]
        then  # if DigitalVibrance is currently on
          log "setting DigitalVibrance to $off (off)"
          nvidia-settings -a "DigitalVibrance=$off" > /dev/null
      fi
  fi

  if [ "$dv" -eq "$on" ]
    then
      sleep 5
    else
      sleep 1
  fi

done

This should work fine, except for the case if the command nvidia-settings -q "DigitalVibrance" -t has empty output instead of returning the current settings value as number.

It is more nicely formatted, fixes some errors we had in the original script, correctly quotes the variables in the if tests and uses actually working commands to get and set the nvidia settings value, as we found out the ones in the original script did not do anything on your system. I also added an optional logging function to show some status output in the console, which you can disable by replacing the line log_enabled="true" with log_enabled="false".


About how to start this script automatically on login, you can read How do I start applications automatically on login? or How to add a script to Startup Applications from the command line?

Byte Commander
  • 105,631
  • 46
  • 284
  • 425