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
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.
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.
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.
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.
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.
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?
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?
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.
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.
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
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.
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:
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
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.
130
u/thLOnuX Mar 14 '23
this shoud have the bash flair imo
108
u/[deleted] Mar 14 '23
Finally, a truly worthy post. This made my eyes bleed. Thank you OP.
114
u/Parker_Hemphill Mar 14 '23
If you’re going to write shell use shell. This is just bash with extra steps IMO
36
u/bravopapa99 Mar 14 '23
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!
9
u/juicewr999 Mar 14 '23
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.
36
u/QueefFart Mar 14 '23
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.
20
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '23
Image Transcription: Code
[Image of a GitHub file containing the following code:]
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!
7
u/QWxx01 Mar 15 '23
If only there was a language to script those commands in…
1
u/kittikelo Mar 15 '23
I work with someone who made Python scripts to modify files because he doesn't know how to use sed and awk.
1
u/[deleted] Mar 16 '23
cries
but... but..............
#!/bin/bash
cries more
-1
u/dgollas Mar 15 '23
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.
-3
u/lardgsus Mar 15 '23
Assembly guy tries to write script
0
u/[deleted] Mar 14 '23
[deleted] — view deleted comment