2

This question already has an answer here:

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

marked as duplicate by Sergiy Kolodyazhnyy command-line May 15 '17 at 1:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
  • 1
    Let us continue this discussion in chat. – Byte Commander May 13 '17 at 17:21
1

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?

3

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.