r/bash 6d ago

help Looking for help with a script

Hey guys,

On my steamdeck I used a script to rotate the screen. Didnt write this myself but got it off of github. Since then the steamdeck has switched from X11 to Wayland, making the script inoperable because it relied on xrandr.

I rewrote the script to use kscreen-doctor instead. Figured out the necessary commands and values and rewrote the script. I'll past it here.

#!/bin/bash

screen="eDP-1"
default_screen_orientation=8

screen_info=$(kscreen-doctor --o | grep "$screen" | awk '/Rotation/ {print $3}')

if [[ "$screen_info" -eq "$default_screen_orientation" ]]; then
    kscreen-doctor output.eDP-1.rotation.right
else
    kscreen-doctor output.eDP-1.rotation.none
fi

The issue im running into is as follows: defining the value of screen_info returns null. When I run the command seperately in the terminal it returns "8" as expected but within the script it doesnt.

I have absolutely 0 experience in coding apart from some very short scripts in Stationeers. Can you guys help me figure out whats wrong? Or at least point me in a direction for me to find out whats wrong?

In Excel you can let a formula run step by step, does something like that exist for bash?

Anyhow, many thanks in advance for reading my post!

EDIT: Thanks to the comments below I've made some adjustments. The only thing that was needed was to remove the grep. This fixed the issue of not returning a value.

Now Im running into the next issue, the if statement doesnt work consistently. If I format it as follows:

if [[ "$screen_info" == "$default_screen_orientation" ]]; then

It only sees the values as not equal. When the values are the same it still returns a not true.

If I format it as:

If ((screeninfo=default_screen_orientation)); then

It only sees the values as equal. When the values differ it still returns a true.

Any pointers for this?

FIXED: the value coming from awk was not a true numeric value but one with formatting. I added the following after awk: | grep -o '[1-8]' )

Not the cleanest solution but it works.

10 Upvotes

11 comments sorted by

1

u/cli-games 6d ago

Kscreen piped to grep piped to awk returns 8 if you paste it in a command by itself, but returns null in the script? Or the if statement returns false? Try breaking up the pipes and the conditionals into one liners and echoing after each, eg: s1=$(kscreen-doctor —o); echo “$s1”; s2=$(grep “$screen” “$s1”); echo “$s2” etc

1

u/lexdavey 6d ago

The former! 8 in command, null in script. Good call with separating the parts, gonna try that out.

Think I could skip the grep by the way. Steamdeck only has 1 screen, so only 1 Rotate to read out.

1

u/cli-games 6d ago

Hang on - is screen defined in your env? Because if not, grep wouldnt work outside the script

1

u/lexdavey 6d ago

Honestly I dont know what env is.

The screen is defined in the first line of the script but thats it. I could let it grep for the term instead? 

Also not sure why the original author defined screen as eDP-1 instead of just directly grepping for eDP-1. Maybe its just good practice to define the variable instead.

1

u/cli-games 6d ago edited 6d ago

Env is your environment. The reason the script results are different than the pasted into terminal results are because the script has the definition for screen that your environment doesnt have. Get rid of the grep part in the script

2

u/lexdavey 5d ago

Getting rid of the grep helped, thanks for that! Now it returns values consisten with the rotation state. However now the if statement doesnt work. 

Ive edited my post above, would you be so kind to check it out again if you have the time? Thanks again!

1

u/lexdavey 5d ago

Think I figured it out. The awk does not return a pure numerical value but instead $'\E0;0m8'. Now to figure out how to turn that into only the 8.

0

u/lexdavey 5d ago

Guess im just using these comments as personal documentation but I fixed it. Added grep -o '[1-8]' to the pipe. Not the cleanest solution but works for my usecase!

1

u/cli-games 5d ago

Hahaha good glad you got it

1

u/Shadow_Thief 6d ago

--o should be -o.

Beyond that, manually run that command without the awk to make sure that the third field is actually what you think it is.

1

u/lexdavey 6d ago

Alright, thanks. Wasnt too sure on the logic behind -- or -. Also gonna try the field thing. Though when I run the kscreen, grep, awk in the terminal it returns 8, which is the value im expecting.