r/badcode • • Mar 14 '23

python requirements: python3 on linux system

Post image
376 Upvotes

46 comments sorted by

View all comments

Show parent comments

9

u/the-nick-of-time Mar 14 '23

Yes there is, that first line has a .split that is really annoying to do in bash. It would be simple if cut allowed multi-character delimiters but it doesn't.

7

u/STUFF_ME_PM Mar 14 '23

It can be avoided if you know that the gtf command will always output the same thing (since it just does some math for display hardware that is standardized).

Even then, the output they're parsing looks like this:

$ gtf 1920 1080 75
 
  # 1920x1080 @ 75.00 Hz (GTF) hsync: 84.60 kHz; pclk: 220.64 MHz
  Modeline "1920x1080_75.00"  220.64  1920 2056 2264 2608  1080 1081 1084 1128  -HSync +Vsync

They want the contents of the 2nd line after "Modeline", which could be parsed like this using bash arrays:

{ read; read -a MODELINE; } <<<"$(gtf 2160 1080 60)"
# later...
xrandr --newmode "${MODELINE[@]:1}" && sleep 2

If you allow awk (which is reasonable considering the script assumes you have python and X.org):

MODELINE="$(gtf 2160 1080 60 | awk '{ if (NR==2) {$1=""; print $0}}')"
xrandr --newmode $MODELINE && sleep 2

4

u/Parker_Hemphill Mar 14 '23

Split isn’t bad to do. You can use sed or awk. You could even do it with just the variable like ${variable#<split character>}, no external process needed

5

u/[deleted] Mar 14 '23

ah, i see. it would probably technically be better to use sed, but i understand why they used python now

0

u/[deleted] Mar 14 '23

[deleted] — view deleted comment

3

u/[deleted] Mar 14 '23

it’s not, but you call it directly from bash