Struggling with if statement [solved]

Forum rules
Before you post please read how to get help
Post Reply
User avatar
AndyMH
Level 9
Level 9
Posts: 2668
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Struggling with if statement [solved]

Post by AndyMH » Wed Oct 09, 2019 11:54 am

Simple bash script:

Code: Select all

monitor=$(xrandr | grep -o "HDMI-2 connected")
if [ ${#monitor} > 0 ]; then
    echo "hello"
#    xrandr --newmode "2560x1080" 185.58 2560 2624 2688 2784 1080 1083 1093 1111 -hsync -vsync
#    xrandr --addmode HDMI-2 "2560x1080"
#    xrandr --output HDMI-2 --mode "2560x1080"
fi
Find out if there is a monitor on HMDI-2, if not then do nothing, trouble is the statements inside the if clause are always executed. The problem is with if [ ${#monitor} > 0 ] which is supposed to test the length of the monitor variable and appears to be always returning TRUE.

Bit of testing:

Code: Select all

andy@T432 ~ $ myvar="hello"
andy@T432 ~ $ echo $myvar
hello
andy@T432 ~ $ echo ${#myvar}  
5
andy@T432 ~ $ test ${#myvar} > 0 <<this is what I have and it doesn't work
andy@T432 ~ $ echo $?
0
andy@T432 ~ $ test #myvar -eq 5 <<this works but leaving the {} out causes a syntax error in bash in the script
andy@T432 ~ $ echo $?
1
Obviously I've got the syntax wrong but how do I fix it?
Last edited by AndyMH on Mon Oct 14, 2019 2:27 pm, edited 2 times in total.
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

User avatar
AndyMH
Level 9
Level 9
Posts: 2668
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Struggling with if statement

Post by AndyMH » Wed Oct 09, 2019 12:01 pm

Fixed, a bit more googling:

Code: Select all

if [[ "$monitor" > 0 ]]; then
but don't ask me to explain why :?
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

rene
Level 12
Level 12
Posts: 4415
Joined: Sun Mar 27, 2016 6:58 pm

Re: Struggling with if statement

Post by rene » Wed Oct 09, 2019 12:40 pm

AndyMH wrote:
Wed Oct 09, 2019 11:54 am
trouble is the statements inside the if clause are always executed
Quite:

Code: Select all

rene@hp8k:~$ unset foo && rm -f 0
rene@hp8k:~$ if [ ${#foo} > 0 ]; then echo foo; fi
foo
rene@hp8k:~$ ls -l 0
-rw-rw-r-- 1 rene rene 0 okt  9 18:33 0
That is, the correct binary operator is not > but -gt. However, a better answer is that you shouldn't really parse output in the first place; should (assuming you don't need $monitor for anything else, of course) just use the exit code of grep directly:

Code: Select all

if xrandr | grep -q "HDMI-2 connected"; then
	....
fi

User avatar
AndyMH
Level 9
Level 9
Posts: 2668
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Struggling with if statement

Post by AndyMH » Wed Oct 09, 2019 2:04 pm

rene, thanks for that, far more elegant than my solution.
if xrandr | grep -q "HDMI-2 connected"; then
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

gm10
Level 20
Level 20
Posts: 10117
Joined: Thu Jun 21, 2018 5:11 pm

Re: Struggling with if statement

Post by gm10 » Wed Oct 09, 2019 3:24 pm

For an alternative that works without any tools:

Code: Select all

[ $(</sys/class/drm/card0-HDMI-A-2/status) == "connected" ] && echo "connected"
(the path may need to be adjusted depending on your hardware)

Just thought I'd mention it, nothing wrong with using xrandr though.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

User avatar
AndyMH
Level 9
Level 9
Posts: 2668
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Struggling with if statement

Post by AndyMH » Wed Oct 09, 2019 3:37 pm

the path may need to be adjusted depending on your hardware
No it didn't, that worked as well, but read this after implementing rene's suggestion.

Each time I have to write a bash script, because I don't do it very often, I feel I'm starting from scratch and trip up over the syntax :( Even with my "Bash Pocket Reference" it's quicker to ask here. Thanks to both of you :D
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

Post Reply

Return to “Scripts & Bash”