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?
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:
…or you can use:
Otherwise you will get error if
$ON_OFFis empty.Finally, it’s better to use
if ... then ... else ... fi, like: