Can a bash script detect an asterisk? [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.
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Can a bash script detect an asterisk? [SOLVED]

Post by Logansfury »

Hello everyone,

I am working on a conky display using bash commands.

The command xrandr gives the folllowing output:

Code: Select all

logansfury@OptiPlex-5040:~$ xrandr
Screen 0: minimum 320 x 200, current 5120 x 1440, maximum 16384 x 16384
DisplayPort-0 disconnected (normal left inverted right x axis y axis)
HDMI-A-0 connected primary 2560x1440+0+0 (normal left inverted right x axis y axis) 697mm x 392mm
   3840x2160     30.00    25.00    24.00    29.97    23.98  
   2560x1440     59.95* 
   1920x1080    120.00   119.88    60.00    60.00    50.00    59.94  
   1280x720      60.00    50.00    59.94  
   1024x768      60.00  
   800x600       60.32  
   720x576       50.00  
   720x480       60.00    59.94  
   640x480       60.00    59.94  
HDMI-A-1 connected 2560x1440+2560+0 (normal left inverted right x axis y axis) 697mm x 392mm
   3840x2160     30.00    25.00    24.00    29.97    23.98  
   2560x1440     59.95* 
   1920x1080    120.00   119.88    60.00    60.00    50.00    59.94  
   1280x720      60.00    50.00    59.94  
   1024x768      60.00  
   800x600       60.32  
   720x576       50.00  
   720x480       60.00    59.94  
   640x480       60.00    59.94
What I want from this are two one-line commands I can use in conky. One to look under the HDMI-A-0 section, and one to look under the HDMI-A-1 section, then do nothing other than print the only value on the list followed by an asterisk (*)

I have been working for over an hour with an online bot that is supposed to be smarter than humans and it is incapable of giving me these commands.

Can anyone please provide the answer?

Thanks for reading,

Logan
Last edited by Logansfury on Wed Mar 20, 2024 6:54 am, 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: Can a bash script detect an asterisk?

Post by xenopeek »

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/\*$/p' | tail -n 1
xrandr | sed -n '/^HDMI-A-1/,/\*$/p' | tail -n 1
Image
User avatar
Coggy
Level 5
Level 5
Posts: 642
Joined: Thu Mar 31, 2022 10:34 am

Re: Can a bash script detect an asterisk?

Post by Coggy »

There is a space following that asterisk (or possibly a '+' symbol instead). And it may be followed by a list of possible frame rates. Maybe this:
xrandr | awk 'sub("*"," "){print $1;exit}'

Example from my laptop:

Code: Select all

Screen 0: minimum 320 x 200, current 1920 x 2280, maximum 16384 x 16384
eDP-1 connected primary 1920x1080+0+1200 (normal left inverted right x axis y axis) 344mm x 194mm
   1920x1080     60.00*+  60.00    48.00  
   1680x1050     60.00  
   1400x1050     60.00  
   1600x900      60.00  
...
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Can a bash script detect an asterisk?

Post by xenopeek »

Ah. It worked for me but if there can be characters after the * you can do this instead:

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/\*/p' | tail -n 1
xrandr | sed -n '/^HDMI-A-1/,/\*/p' | tail -n 1
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can a bash script detect an asterisk?

Post by Logansfury »

xenopeek wrote: Wed Mar 20, 2024 2:07 am

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/\*$/p' | tail -n 1
xrandr | sed -n '/^HDMI-A-1/,/\*$/p' | tail -n 1
Hello Xenopeek,

Thank you for the reply, but these commands are displaying the wrong data:

Code: Select all

logansfury@OptiPlex-5040:~$ xrandr | sed -n '/^HDMI-A-0/,/\*$/p' | tail -n 1
   640x480       60.00    59.94  
logansfury@OptiPlex-5040:~$ xrandr | sed -n '/^HDMI-A-1/,/\*$/p' | tail -n 1
   640x480       60.00    59.94  
logansfury@OptiPlex-5040:~$ 
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can a bash script detect an asterisk?

Post by Logansfury »

Coggy wrote: Wed Mar 20, 2024 5:09 am There is a space following that asterisk (or possibly a '+' symbol instead). And it may be followed by a list of possible frame rates. Maybe this:
xrandr | awk 'sub("*"," "){print $1;exit}'

Example from my laptop:

Code: Select all

Screen 0: minimum 320 x 200, current 1920 x 2280, maximum 16384 x 16384
eDP-1 connected primary 1920x1080+0+1200 (normal left inverted right x axis y axis) 344mm x 194mm
   1920x1080     60.00*+  60.00    48.00  
   1680x1050     60.00  
   1400x1050     60.00  
   1600x900      60.00  
...
Hello Coggy,

Thank you for the reply, but this isn't displaying the info I need, it's displaying a different value than the one followed by the asterisk:

Code: Select all

logansfury@OptiPlex-5040:~$ xrandr | awk 'sub("*"," "){print $1;exit}'
2560x1440
logansfury@OptiPlex-5040:~$ 
[/'code]
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: Can a bash script detect an asterisk?

Post by xenopeek »

Use the other commands:
xenopeek wrote: Wed Mar 20, 2024 5:55 am Ah. It worked for me but if there can be characters after the * you can do this instead:

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/\*/p' | tail -n 1
xrandr | sed -n '/^HDMI-A-1/,/\*/p' | tail -n 1
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can a bash script detect an asterisk?

Post by Logansfury »

xenopeek wrote: Wed Mar 20, 2024 6:22 am Use the other commands:
xenopeek wrote: Wed Mar 20, 2024 5:55 am Ah. It worked for me but if there can be characters after the * you can do this instead:

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/\*/p' | tail -n 1
xrandr | sed -n '/^HDMI-A-1/,/\*/p' | tail -n 1
I just went thru the tread and tried the last two commands you posted:

Code: Select all

logansfury@OptiPlex-5040:~$ xrandr | awk 'sub("*"," "){print $1;exit}'
2560x1440
logansfury@OptiPlex-5040:~$ xrandr | sed -n '/^HDMI-A-0/,/\*/p' | tail -n 1
   2560x1440     59.95* 
logansfury@OptiPlex-5040:~$ 
The second command came closer but it displayed:

2560x1440 59.95*

All I want displayed is:

59.95

Can everything else be scripted out please?
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: Can a bash script detect an asterisk?

Post by xenopeek »

By combining both:

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/\*/p' | awk 'sub("*"," "){print $2}'
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can a bash script detect an asterisk?

Post by Logansfury »

xenopeek wrote: Wed Mar 20, 2024 6:48 am By combining both:

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/\*/p' | awk 'sub("*"," "){print $2}'
Absolutely perfect!

Exactly what I wanted!

Thank you xenopeek you just provided an answer an AI that is supposed to be smarter than humans could not :D
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can a bash script detect an asterisk? [SOLVED]

Post by Logansfury »

Actually I just realized I could use an edit:

If no information exists at either HDMI-A-0 or HDMI-A-1 can the script display "No monitor detected"?
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: Can a bash script detect an asterisk? [SOLVED]

Post by xenopeek »

Code: Select all

freq=$(xrandr | sed -n '/^HDMI-A-0/,/\*/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-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: Can a bash script detect an asterisk? [SOLVED]

Post by Logansfury »

xenopeek wrote: Wed Mar 20, 2024 10:11 am

Code: Select all

freq=$(xrandr | sed -n '/^HDMI-A-0/,/\*/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
Something odd is happening (as usual)

I tried this command across all three of my gpu ports:

Code: Select all

logansfury@OptiPlex-5040:~$ freq=$(xrandr | sed -n '/^HDMI-A-0/,/\*/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
59.95
logansfury@OptiPlex-5040:~$ freq=$(xrandr | sed -n '/^DisplayPort-0/,/\*/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
59.95
logansfury@OptiPlex-5040:~$ freq=$(xrandr | sed -n '/^HDMI-A-1/,/\*/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
59.95
logansfury@OptiPlex-5040:~$
I currently have an issue with linux and my 3rd monitor and DisplayPort-0 device is disconnected, yet It is displaying as if a monitor was connected and running at 59.95 Hz refresh rate?
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: Can a bash script detect an asterisk? [SOLVED]

Post by xenopeek »

It's matching the first line having a * so if you have multiple ports and one of those ports doesn't have a * line it would match the * line from the next port. That's what is happening.

This better?

Code: Select all

freq=$(xrandr | sed -n '/^HDMI-A-0/,/^[^[:space:]]/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-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: Can a bash script detect an asterisk? [SOLVED]

Post by Logansfury »

xenopeek wrote: Wed Mar 20, 2024 10:37 am It's matching the first line having a * so if you have multiple ports and one of those ports doesn't have a * line it would match the * line from the next port. That's what is happening.

This better?

Code: Select all

freq=$(xrandr | sed -n '/^HDMI-A-0/,/^[^[:space:]]/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
This is perfect :D

Code: Select all

logansfury@OptiPlex-5040:~$ freq=$(xrandr | sed -n '/^HDMI-A-0/,/^[^[:space:]]/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
59.95
logansfury@OptiPlex-5040:~$ freq=$(xrandr | sed -n '/^HDMI-A-1/,/^[^[:space:]]/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
59.95
logansfury@OptiPlex-5040:~$ freq=$(xrandr | sed -n '/^DisplayPort-0/,/^[^[:space:]]/p' | awk 'sub("*"," "){print $2}'); echo "${freq:-No monitor detected}"
No monitor detected
logansfury@OptiPlex-5040:~$ 
Thank you for taking the time to code this for me :)
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: Can a bash script detect an asterisk? [SOLVED]

Post by Koentje »

That doesn't work on my system. The astrix and the plus sign are not always on the same spot!

Code: Select all

Screen 0: minimum 320 x 200, current 4031 x 2520, maximum 16384 x 16384
DisplayPort-1 connected primary 3440x1440+0+1080 (normal left inverted right x axis y axis) 797mm x 334mm
   3440x1440     59.97 + 143.97*  119.99    99.98  
Because '{print $2}' is used, i get a freq of 59,97, but it should be 143.97.
How to change the code so it picks the value in front of the astrix?
Image
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Can a bash script detect an asterisk? [SOLVED]

Post by xenopeek »

Koentje wrote: Wed Mar 20, 2024 1:45 pm 59.97 + 143.97*
What does the + mean?
Image
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can a bash script detect an asterisk? [SOLVED]

Post by Koentje »

I have no idea.. most of the time the plus is direct behind the astrix, but in my case with the primary it's not..

Like with my second monitor

Code: Select all

HDMI-1-1 connected 1920x1080+2111+0 (normal left inverted right x axis y axis) 477mm x 268mm
   1920x1080     60.00*+  50.00    59.94  
Image
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Can a bash script detect an asterisk? [SOLVED]

Post by xenopeek »

Probably still possible with awk but I'm not good with that. So replacing it with sed:

Code: Select all

xrandr | sed -n '/^HDMI-A-0/,/^[^[:space:]]/p' | sed -nr 's/^.*\s([0-9.]+)\*\+?(\s.*)?$/\1/p'
The first sed gets the lines for the port HDMI-A-0 in this case. The second sed gets the line that has a * on it and basically looks for a sequence of digits, optional dot in them, followed directly by a * and optional + and only prints that part of the line.

Edit: forgot the no monitor thingy:

Code: Select all

freq=$(xrandr | sed -n '/^HDMI-A-0/,/^[^[:space:]]/p' | sed -nr 's/^.*\s([0-9.]+)\*\+?(\s.*)?$/\1/p'); echo "${freq:-No monitor detected}"
Image
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: Can a bash script detect an asterisk? [SOLVED]

Post by 1000 »

59.97 + 143.97*
Probably
59.97 is recommended and 143.97 is set. ( it is in description of " man xrandr " )

I wonder why the first value is smaller. Maybe it's just the result of sorting from command.
Koentje Can I see the entire xrandr output?
Last edited by 1000 on Wed Mar 20, 2024 2:28 pm, edited 1 time in total.
Post Reply

Return to “Scripts & Bash”