How to check if a monitor is connected (e.g DP-1)?

Hello everyone, I wrote a script for my monitor’s resolution, and I set it to autostart.
But I want to an if statement so that it only applies these settings if DP-1 is connected.
Is this possible?
My script:

#!/bin/bash
MODE=$(optimus-manager --print-mode | cut -d " " -f 5)
CVT=$(cvt 1920 1080 | tail -1 | cut -d ' ' -f2-)
mName=$(echo $CVT | cut -d ' ' -f1)
xrandr --newmode $CVT

if [[ $MODE == "nvidia" ]]
then
	xrandr --addmode DP-1-1 "$mName"
    xrandr --output eDP-1-1 --primary --mode 1366x768 --pos 0x312 --rotate normal --output HDMI-1-1 --off --output DP-1-1 --mode $mName --pos 1366x0 --rotate normal	
else 
	xrandr --addmode DP-1 "$mName"
    xrandr --output eDP-1 --primary --mode 1366x768 --pos 0x312 --rotate normal --output HDMI-1 --off --output DP-1 --mode $mName --pos 1366x0 --rotate normal
fi

So it might look like:

#!/bin/bash
MODE=$(optimus-manager --print-mode | cut -d " " -f 5)
CVT=$(cvt 1920 1080 | tail -1 | cut -d ' ' -f2-)
mName=$(echo $CVT | cut -d ' ' -f1)
connected=$(Is VGA-1 connected?)
xrandr --newmode $CVT

if [[ $connected == "yes" ]]
then
    if [[ $MODE == "nvidia" ]]
    then
	    xrandr --addmode DP-1-1 "$mName"
        xrandr --output eDP-1-1 --primary --mode 1366x768 --pos 0x312 --rotate normal --output HDMI-1-1 --off --output DP-1-1 --mode $mName --pos 1366x0 --rotate normal	
    else 
	    xrandr --addmode DP-1 "$mName"
        xrandr --output eDP-1 --primary --mode 1366x768 --pos 0x312 --rotate normal --output HDMI-1 --off --output DP-1 --mode $mName --pos 1366x0 --rotate normal
    fi
fi

https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

That link doesn’t contain anything that tells me how to detect connected monitors…

I edited it to show what I mean.

Fixed it by modifying the script to this:

#!/bin/bash
MODE=$(optimus-manager --print-mode | cut -d " " -f 5)
CVT=$(cvt 1920 1080 | tail -1 | cut -d ' ' -f2-)
mName=$(echo $CVT | cut -d ' ' -f1)
connected=$(xrandr --listactivemonitors | grep ' DP-1')
xrandr --newmode $CVT

if [[ $connected != "" ]]
then
    if [[ $MODE == "nvidia" ]]
    then
	    xrandr --addmode DP-1-1 "$mName"
        xrandr --output eDP-1-1 --primary --mode 1366x768 --pos 0x312 --rotate normal --output HDMI-1-1 --off --output DP-1-1 --mode $mName --pos 1366x0 --rotate normal	
    else 
	    xrandr --addmode DP-1 "$mName"
        xrandr --output eDP-1 --primary --mode 1366x768 --pos 0x312 --rotate normal --output HDMI-1 --off --output DP-1 --mode $mName --pos 1366x0 --rotate normal
    fi
fi

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.