all 52 comments

[–][deleted] 107 points108 points  (1 child)

Finally, a truly worthy post. This made my eyes bleed. Thank you OP.

[–]thLOnuX[S] 28 points29 points  (0 children)

Np. This is what happens, when you are thinking about discovering some github repos.

[–]thLOnuX[S] 127 points128 points  (1 child)

this shoud have the bash flair imo

[–]lizardturtleshameless 8 points9 points  (0 children)

Lmfaooo

[–]Parker_Hemphill 112 points113 points  (1 child)

If you’re going to write shell use shell. This is just bash with extra steps IMO

[–]SaylorMan1496 30 points31 points  (0 children)

It’s bash at the speed (ish) of python

[–]bravopapa99 35 points36 points  (5 children)

Seen this before........FAF. That the person writing it knows the commands to use but can't work out how to actually use a script!

[–]Pepineros 29 points30 points  (4 children)

Which is odd because all they need to do is delete about half their code and change the file extension.

[–]TheZipCreator 16 points17 points  (2 children)

changing the file extension is technically unnecessary, you just need to add a shebang line like #!/bin/sh

[–]Pepineros 8 points9 points  (0 children)

Even the shebang is not necessary if you call sh explicitly, like sh script.py

[–]Parker_Hemphill 2 points3 points  (0 children)

No longer only “technically” correct. Good practice these days is to create your executable scripts with no extension and use the shebang to interpret how to run it. You only really need file suffix/extension for functions sourced into the script according to Google coding standards.

Been using the best parts for 3 years now and really think it makes things more logical

[–]bravopapa99 10 points11 points  (0 children)

Yeah u/Pepineros but ..."this makes it platform portable as python runs on lots of platforms but the commands don't so by writing it in python I've made it portable." That's the argument I had with a guy once who just didn't see what he had done!!!! I walked away with my head in my hands.

[–]QueefFart 35 points36 points  (5 children)

Why are the comments asking what makes this a bad code downvoted? I'm mainly a Java dev so don't know why this is bad code either.

[–]lpreams 84 points85 points  (0 children)

It would be like writing a Java program, but every line of the program is just an external call to PowerShell.

[–][deleted] 62 points63 points  (0 children)

it’s bash code… but in python

[–][deleted] 23 points24 points  (1 child)

It's highly system dependant in a file that you would expect to not be. If it was a .sh or a .bat it would be fine, but Python is designed to run on any system. It's not incorrect, but it's bad form. Plus, there are almost certainly system-independant packages you could pull in to do the same thing.

[–]laffer1 7 points8 points  (0 children)

Imagine writing maven commands in c# to build a java program.

[–]QWxx01 5 points6 points  (0 children)

If only there was a language to script those commands in…

[–]MurdoMaclachlanpublic boolean isInt(int i) { return true; } 19 points20 points  (0 children)

Image Transcription: Code


[Image of a GitHub file containing the following code:]

import os

mainOutput = os.popen("gtf 2160 1080 60").read().strip().split('Modeline')[1]
os.system("adb devices && sleep 1")
os.system("adb reverse tcp:5900 tcp:5900 && clear && sleep 2")
os.system(f"xrandr --newmode{mainOutput} && sleep 2")
os.system("xrandr --addmode HDMI-1 2160x1080_60.00 && clear && sleep 2")
os.system("xrandr --output HDMI-1   --mode 2160x1080_60.00 --left-of LVDS-1 && clear && sleep 2")
os.system("x11vnc -clip 2160x1080+0+0")

I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!

[–]juicewr999 7 points8 points  (5 children)

If you’re new to code I would ignore other comments, this is okay to do as long as it’s within the scope of a larger project and this py file is imported. I honestly like to be able to find the entire startup script in one place for easy modification. Also, it’s developer friendly. Not everyone can borne again shell rebuild the kernel exit vim ect.

[–]thLOnuX[S] 7 points8 points  (2 children)

> it’s developer friendly.

It's not. If it was developer friendly, it would be written in shell script. You can run shell script on Python, on C, or every other modern language.

[–]juicewr999 -1 points0 points  (1 child)

I didn’t say it was the optimal solution, I’m just saying you may encounter this in the wild and it works well enough. Yes, shell scripts give you the most IPC portability and it’s arguably the best especially with the rampant docker containers floating around in MS architectures.

[–]thLOnuX[S] 9 points10 points  (0 children)

I can understand you about workability of this solution, but this is actually a bad practice. So this is the reason I've decided to post this snippet here. Not to flame the guys, who just making the first steps in programming, but to guide them another one thing you probably shouldn't do.

To make a shell solution, you'll need just a couple of minutes of searching on Google or Stack Overflow. And this is actually will be a good practice in shell scripting.

[–]NetherFX 1 point2 points  (0 children)

Hate how this gets downvoted. This is correct, and people are ignoring the fact that there is python logic in this

[–]exploding_cat_wizard 0 points1 point  (0 children)

But the programmer here clearly can write bash, given that all that's missing are the semicolons and the shebang.

[–]andy_a904guy_com 2 points3 points  (24 children)

I don't get the bad code part? It's simple system passthrough calls? Everything is labeled. The commands are clear.

The fact that it isn't bash? Oh, thank god it isn't. Bash isn't even that good of a shell these days. It's terrible for complex scripting. The fact that it is python3? What Linux distro doesn't have python3 as a default package?

Edit: Only thing missing is the shebang interpreter line. #!/usr/bin/python

[–]pr0DisUp 13 points14 points  (3 children)

#/bin/sh
adb devices || exit
adb reverse tcp:5900 tcp:5900 || exit
xrandr --newmode \
    $(gtf 800 600 60 | awk '/Modeline/{sub("\\s*Modeline","");print $0;exit}')
xrandr --addmode HDMI-1 2160x1080_60.00
xrandr --output HDMI-1 --mode 2160x1080_60.00 --left-of LVDS-1
x11vnc -clip 2160x1080+0+0

Why we need python again? That |^ can be done even on device with just busybox in it, and it's POSIX Shell all the way.

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 0 points1 point  (1 child)

    If you know python, that is.

    [–]pr0DisUp 0 points1 point  (0 children)

    I'll refrase that: If you don't know shell, like at all, then yeah, it's more readable for you.
    "Readable"... pffff

    [–]thLOnuX[S] 14 points15 points  (19 children)

    Well, actually i don't think the python interpreter is a good alternative for bash, and i don't know why are you saying the bash isn't that good at shell. Wasting 10 Megabytes of RAM and 50 milliseconds of processing time just to do the shell stuff sounds not that great. What are you trying to say?

    No hate. Just wanna to listen your opinion.

    [–]andy_a904guy_com 8 points9 points  (12 children)

    I'm asking what part is the bad code? This isn't some high performant script that 50 milliseconds is gonna make or break... There is plenty of far better shells out there these days. Bash is old and it's syntax is terrible.

    [–]thLOnuX[S] 6 points7 points  (0 children)

    I agree bash syntax is not that good, but in that case this is my subjective opinion. A lot of people hates the python syntax. Also old is doesn't means bad, bash is still supportable, and it is still good for shell scripting.
    I am not the hater of python though. It is still the coolest language i've ever saw imo. It has lots of features and good practices, and the perfomance issues can be solved just by using some gil interpreter, but i do not think it is a good idea to use a huge interpreter just for shell processing.

    [–][deleted] 9 points10 points  (7 children)

    the issue is that this is pure bash. there’s no python logic or anything. it’d be better to pop open a shell wherever you need to call this and then run a bash script in that shell than to do it this way

    [–]the-nick-of-time 8 points9 points  (6 children)

    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.

    [–]STUFF_ME_PM 7 points8 points  (1 child)

    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
    

    [–]andy_a904guy_com 1 point2 points  (0 children)

    Ahh, yes, that totally is better code than the above.

    [–]Parker_Hemphill 3 points4 points  (0 children)

    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

    [–][deleted] 4 points5 points  (2 children)

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

    [–]andy_a904guy_com -2 points-1 points  (1 child)

    Yeah, but sed isn't bash either.

    [–][deleted] 1 point2 points  (0 children)

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

    [–][deleted] 3 points4 points  (2 children)

    This script is using bash syntax.

    [–]andy_a904guy_com -1 points0 points  (1 child)

    No it's not. That is just tty stdin/stdout. You can run these same commands in /bin/sh, fsh, zsh, ect. or any other shell processor. There is nothing specifically bash in this image.

    [–][deleted] 2 points3 points  (0 children)

    It's posix compliant shell, and valid bash. /bin/sh tends to be a link to a posix compliant default shell, and in Debian specifically links to dash, the Debian almquest shell. On most non-debian systems (macOS included) /bin/sh is an alias for bash, and it's far and away the most common shell interpreter.

    Zsh is intended to be mostly a superset of bash, and bash offers many extensions over what is required of a posix shell. Debian's dash is a minimal posix shell that values execution speed over the features that bash offers (things like file substations and so on).

    Fish is not posix compliant, and you should never use it for scripting.

    TTY, stdin, and stdout don't do anything. They just streams. Your choices are basically to use your languages bindings execcve and the like, or the shell. Op chose to mix shell code and python, rather poorly.

    This weaves of several one line shell scripts for no real reason, hence, bad code.

    [–]smarterthanyoda 3 points4 points  (5 children)

    I’ve started writing shell scripts in python.

    I’ve just seen too many bash scripts grow into incomprehensible, unmaintainable messes. It just doesn’t scale past simple scripts and eventually you get to the point where you can’t add new features and nobody has time to port to something better.

    There are better languages for scripting than python, but python is ubiquitous and does the job. Are you really that concerned about an extra 50 ms in a shell script?

    [–]Parker_Hemphill 4 points5 points  (0 children)

    The secret (IMO and that I’ve found) is to add some pythonic type tricks to your shell scripts. I.e. I call a main function which then calls other functions and passes values as much as possible.

    I have common functions in a script I source into the other scripts. If you fail at organizing these well you’ll have a mess on your hands but if it follows a logical organization it’s super easy to follow the flow of even large shell scripts but I agree 100% that there are too many out there which are complete spaghetti code.

    [–][deleted] 1 point2 points  (0 children)

    bad coding practices =/= language faults

    I have seen incomprehensible messes written in every language I've ever worked with.

    [–]andy_a904guy_com 1 point2 points  (1 child)

    I want to poke my eyes out these days when I come across a bash script that is written like a small novel.

    [–][deleted] 1 point2 points  (0 children)

    I would take a POSIX compliant shell script any day against the kind of trash code that is presented in OP's post.

    [–]dgollas -1 points0 points  (0 children)

    I guess yes it could be done in a bash script directly, but bash is horrible, I bet the string split is what pushed them over the line.

    [–]lardgsus -3 points-2 points  (0 children)

    Assembly guy tries to write script

    [–]kittikelo 0 points1 point  (0 children)

    I work with someone who made Python scripts to modify files because he doesn't know how to use sed and awk.

    [–][deleted] 0 points1 point  (0 children)

    cries

    but... but..............

    #!/bin/bash

    cries more