need help with a command line please[SOLVED]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

need help with a command line please[SOLVED]

Post by Logansfury »

Hello everyone,

I am almost done with my Display conky widget. I need some help getting info from xrandr output. The first line relating to port 2 will be either

HDMI-A-1 connected 2560x1440+2560+0

or HDMI-A-1 disconnected

I need a command one-liner to check if HDMI-A-1 says connected or disconnected. If disconnected print "No monitor detected", if connected, print $3 but strip the "+2560+0" from the end.

The closest my online resource can come up with is:

Code: Select all

${exec xrandr --query | grep 'HDMI-A-1 connected' >/dev/null && xrandr --query | grep 'HDMI-A-1 connected' | awk '{print $3}' || echo "No monitor detected" | sed 's/+2560+0//'}
Can anyone please come up with a working script?

Thank you,

Logan
Last edited by Logansfury on Wed Mar 20, 2024 5:00 pm, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: need help with a command line please

Post by xenopeek »

Code: Select all

res=$(xrandr | sed -rn 's/^HDMI-A-1 connected [^0-9]*([0-9x]*).*$/\1/p'); echo "${res:-No monitor detected}"
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: need help with a command line please

Post by Logansfury »

xenopeek wrote: Wed Mar 20, 2024 4:43 pm

Code: Select all

res=$(xrandr | sed -rn 's/^HDMI-A-1 connected [^0-9]*([0-9x]*).*$/\1/p'); echo "${res:-No monitor detected}"
Hey Xenopeek!

Swift and decisive :D

This command worked across the board for all of my inputs:

Code: Select all

logansfury@OptiPlex-5040:~$ res=$(xrandr | sed -rn 's/^HDMI-A-1 connected [^0-9]*([0-9x]*).*$/\1/p'); echo "${res:-No monitor detected}"
2560x1440
logansfury@OptiPlex-5040:~$ res=$(xrandr | sed -rn 's/^HDMI-A-0 connected [^0-9]*([0-9x]*).*$/\1/p'); echo "${res:-No monitor detected}"
2560x1440
logansfury@OptiPlex-5040:~$ res=$(xrandr | sed -rn 's/^DisplayPort-0 connected [^0-9]*([0-9x]*).*$/\1/p'); echo "${res:-No monitor detected}"
No monitor detected
logansfury@OptiPlex-5040:~$
Thank you so much!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: need help with a command line please[SOLVED]

Post by Koentje »

That's a nifty piece of code! echo "${res:-No monitor detected}" 8)
Image
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: need help with a command line please[SOLVED]

Post by xenopeek »

Koentje wrote: Wed Mar 20, 2024 5:42 pm That's a nifty piece of code! echo "${res:-No monitor detected}" 8)
Shell parameter expansion section of the bash manual has gems like that: https://www.gnu.org/software/bash/manua ... nsion.html
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: need help with a command line please[SOLVED]

Post by Logansfury »

xenopeek wrote: Wed Mar 20, 2024 5:56 pm
Koentje wrote: Wed Mar 20, 2024 5:42 pm That's a nifty piece of code! echo "${res:-No monitor detected}" 8)
Shell parameter expansion section of the bash manual has gems like that: https://www.gnu.org/software/bash/manua ... nsion.html
Bless yer heart for sharing such gems :D
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Scripts & Bash”