Reputation: 3
This is my first bash script ever, essentially it just shuts off my second monitor. But I've been having problems with it, as it keeps giving me errors when I run it.
#!/bin/bash
read -p "Do you want the 2nd monitor on or off? " ON_OFF
if [$ON_OFF == on]; then
xrandr --output DVI-I-3 --auto --right-of DVI-I-0
echo "done"
fi
if [$ON_OFF == off]; then
xrandr --output DVI-I-3 --off
echo "done"
fi
When I run it I get
monitor_control.sh: 11: [[off: not found
monitor_control.sh: 16: [[off: not found
Can anybody explain to me why it's not working?
Upvotes: 0
Views: 313
Reputation: 1979
You need to add space around [
and ]
, as they are separate commands in bash.
Moreover, either quotes need to be used around parameter expansions, or [[ ]]
needs to be used instead of [ ]
.
That is, you can either use:
if [[ $ON_OFF = on ]]
...or you can use:
if [ "$ON_OFF" = on ]
Otherwise you will get error if $ON_OFF
is empty.
Finally, it's better to use if ... then ... else ... fi
, like:
if [[ $ON_OFF = on ]]; then
xrandr --output DVI-I-3 --auto --right-of DVI-I-0
else
xrandr --output DVI-I-3 --off
fi
echo "done."
Upvotes: 4
Reputation: 17062
This should work.
#!/bin/bash
echo -n "Do you want the 2nd monitor on or off? "
read ON_OFF;
if [ $ON_OFF == "on" ]; then
xrandr --output DVI-I-3 --auto --right-of DVI-I-0
echo "done"
fi
if [ $ON_OFF == "off" ]; then
xrandr --output DVI-I-3 --off
echo "done"
fi
Upvotes: 0