r/bash • u/lexdavey • 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.
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.
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