1

I fixed the code below so it works:

#!/bin/bash
out="$(cat /proc/acpi/bbswitch)"
if [[ "$out" == *OFF* ]];
then
    tee /proc/acpi/bbswitch <<<ON
    echo "Nvidia card activated."
else
    tee /proc/acpi/bbswitch <<<OFF
    echo "Nvidia card disabled."
fi

This is made for activating or disabling my optimus card. I get an error on line 4:

./.bb: line 4: [0000:01:00.0 OFF: command not found
OFF
Nvidia card disabled.

I can read from it that it tries to execute the $out variable. Why?

1
  • You've made several changes to the question. Does the error still occur? Commented Nov 12, 2012 at 22:16

1 Answer 1

4

You need to ensure that there is at least 1 space between the brackets [ / ] and the actual variables; i.e.: change your code from

if ["$out" == "$is"];

to:

if [ "$out" == "$is" ];

And it should work.

The reason is that [ is actually the "test" command in bash. Try on your prompt:

which [

and you should see something like:

/usr/bin/[

Also, man [ to read more about syntax

(Note, since arguments are delimited by spaces, there needs to be a space between your 2nd variable and ] as well. Test uses ] as the terminating sentinel)

Sign up to request clarification or add additional context in comments.

3 Comments

hmm.. I fixed it now, but there is a problem with the if statement. It never executes the ON part, only the OFF part.
I tried your command, Tristan, with sampson-chen's change and it works. Perhaps you are not getting the value you expect for one of your variables? Try adding this, set -x, right below your #!/bin/bash You can debug the output this way.
[[ 0000:01:00.0 OFF == \O\F\F ]] what is that supposed to mean, the \O\F\F

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.