PDA

View Full Version : If statement in Bash



borgvall
October 8th, 2008, 12:45 PM
I'm trying to make a script which should set the correct color profile for the screen that is being used. It's not working even after several hours of trial and error. So please help me out with this code.



#!/bin/bash
grafik=xrandr | head -1
screen0="Screen 0: minimum 320 x 240, current 1920 x 1200, maximum #1920 x 1200"
screen1="Screen 1: minimum 320 x 240, current 1280 x 800, maximum 1280 x 800"
if [ "$grafik" = "$screen0" ];
then
xcalib /usr/share/color/icc/Monitor_2008-03-27_2_Benq_brightness30.icc
else [ "$grafik" = "$screen1" ];
xcalib /usr/share/color/icc/Monitor_2008-03-27_3_m1330_luminance195.icc
fi

geirha
October 8th, 2008, 12:57 PM
You want the first line of xrandr to be stored in grafik, you need to use the backticks for that:

grafik=`xrandr | head -1`
# alternatively you can use $() the same way
grafik=$(xrandr | head -1)

EDIT: Also, you probably only need to check if the first part is "Screen 0" or "Screen 1":


grafik=`xrandr | head -1`

case "$grafik" in
"Screen 0"*)
echo "Screen 0 stuff"
;;
"Screen 1"*)
echo "Screen 1 stuff"
;;
*)
echo no match
;;
esac

SeanHodges
October 8th, 2008, 12:58 PM
$grafik is getting set incorrectly.

Use back-ticks to pass the output of your command into $grafik:

grafik=`xrandr | head -1`


Edit: geirha beat me to it :)

borgvall
October 8th, 2008, 01:03 PM
Ah, I read some tutorials on this but thought that the backsticks just were ' but in another character type.

geirha
October 8th, 2008, 01:21 PM
I was too slow on editing my previous post, have a look at the example I added there. Your else is also wrong, instead of else [ "$grafik" = "$screen1" ]; you want to use
elif [ "$grafik" = "$screen1" ]; then with elif being short for else if.

borgvall
October 10th, 2008, 12:57 PM
I noticed that what counts as screen 0 varies, therefore "minimum 320 x 240, current 1920 x 1200, maximum #1920 x 1200" and "minimum 320 x 240, current 1280 x 800, maximum 1280 x 800" are the things which shows if the external or the internal screen should be used. This force me to make the script look for those lines and not only compare the exact line which "xrandr | head -1" outputs. So I guess I need help with another operator.

dwhitney67
October 10th, 2008, 01:11 PM
When implementing an if-statement, is not the semi-colon optional? Well, not if the "then" statement is on the same line. But if the "then" is on a separate line, the semi-colon should not be present. At least I think so; I could be wrong.

Semi-colon needed:


if [ some-condition ]; then
...
fi


Semi-colon not needed:


if [ some-condition ]
then
...
fi

borgvall
October 13th, 2008, 09:14 AM
Ok, but that isn't the problem Please see my previous post, I hope it's possible to understand it.

SeanHodges
October 13th, 2008, 01:24 PM
I noticed that what counts as screen 0 varies, therefore "minimum 320 x 240, current 1920 x 1200, maximum #1920 x 1200" and "minimum 320 x 240, current 1280 x 800, maximum 1280 x 800" are the things which shows if the external or the internal screen should be used. This force me to make the script look for those lines and not only compare the exact line which "xrandr | head -1" outputs. So I guess I need help with another operator.

Perhaps this?


screen0="Screen 0"
screen1="Screen 1"
if [[ "$grafik" =~ "$screen0" ]]
then
...
elif [[ "$grafik" =~ "$screen1" ]]
then
...
fi

Also, check out geirha's "switch case" post earlier.

akvino
October 13th, 2008, 04:24 PM
does [[ "$a" =~ "$b" ]] check for bitwise equality?

mssever
October 13th, 2008, 04:26 PM
does [[ "$a" =~ "$b" ]] check for bitwise equality?
No, =~ expects a regular expression on the right hand side and tests the left hand side for a match.

geirha
October 13th, 2008, 04:27 PM
does [[ "$a" =~ "$b" ]] check for bitwise equality?

It matches regular expressions. Sort of like doing echo "$a" | egrep "$b".

borgvall
October 15th, 2008, 09:37 PM
Perhaps this?


screen0="Screen 0"
screen1="Screen 1"
if [[ "$grafik" =~ "$screen0" ]]
then
...
elif [[ "$grafik" =~ "$screen1" ]]
then
...
fi

Also, check out geirha's "switch case" post earlier.

That was a good idea but this doesn't work from what I think because Screen 0 and Screen 1 changes when I unplug the external monitor. So again, what has to be compared is what's written after "Screen X:"

This is what I tried last time:


grafik=`xrandr | head -1`
screen0="minimum 320 x 240, current 1920 x 1200, maximum #1920 x 1200"
screen1="minimum 320 x 240, current 1280 x 800, maximum 1280 x 800"
if [[ "$grafik" =~ "$screen0" ]]
then
/home/victor/dokument/diverse/script/Benq.icc.sh
elif [[ "$grafik" =~ "$screen1" ]]
then
/home/victor/dokument/diverse/script/XPS.bright.icc.sh
fi

borgvall
October 16th, 2008, 10:21 AM
That was a good idea but this doesn't work from what I think because Screen 0 and Screen 1 changes when I unplug the external monitor. So again, what has to be compared is what's written after "Screen X:"

This is what I tried last time:


grafik=`xrandr | head -1`
screen0="minimum 320 x 240, current 1920 x 1200, maximum #1920 x 1200"
screen1="minimum 320 x 240, current 1280 x 800, maximum 1280 x 800"
if [[ "$grafik" =~ "$screen0" ]]
then
/home/victor/dokument/diverse/script/Benq.icc.sh
elif [[ "$grafik" =~ "$screen1" ]]
then
/home/victor/dokument/diverse/script/XPS.bright.icc.sh
fi


By the way that Benq.icc.sh and XPS.bright.icc.sh run xcalib and the icc file.

geirha
October 16th, 2008, 10:33 AM
That was a good idea but this doesn't work from what I think because Screen 0 and Screen 1 changes when I unplug the external monitor. So again, what has to be compared is what's written after "Screen X:"

This is what I tried last time:


grafik=`xrandr | head -1`
screen0="minimum 320 x 240, current 1920 x 1200, maximum #1920 x 1200"
screen1="minimum 320 x 240, current 1280 x 800, maximum 1280 x 800"
if [[ "$grafik" =~ "$screen0" ]]
then
/home/victor/dokument/diverse/script/Benq.icc.sh
elif [[ "$grafik" =~ "$screen1" ]]
then
/home/victor/dokument/diverse/script/XPS.bright.icc.sh
fi

It sounds like you just need to check the maximum value. If so, I'd use a case like this


case "`xrandr | head -1`" in
*"1920 x 1200") /home/victor/dokument/diverse/script/Benq.icc.sh ;;
*"1280 x 800") /home/victor/dokument/diverse/script/XPS.bright.icc.sh ;;
*)
echo "no matching screen" >&2
exit 1
;;
esac

borgvall
October 17th, 2008, 10:06 PM
It sounds like you just need to check the maximum value. If so, I'd use a case like this


case "`xrandr | head -1`" in
*"1920 x 1200") /home/victor/dokument/diverse/script/Benq.icc.sh ;;
*"1280 x 800") /home/victor/dokument/diverse/script/XPS.bright.icc.sh ;;
*)
echo "no matching screen" >&2
exit 1
;;
esac


Thank you Geirha!