7 ms·
Can someone name me examples of where a bash script is actually the _correct_ tool for the job in modern times? Or is it still used only because it’s the easily
by callalex 2y ago
Can someone name me examples of where a bash script is actually the _correct_ tool for the job in modern times? Or is it still used only because it’s the easily available tool?
- jofer 2y agoAny time you're running lots of CLI utilities directly is a good start. Bash is very very good for that, and other than perl, most other things aren't particularly good at it.
- lambdaba 2y agoRuby is the greatest at it and inline deps make it perfect for scripting.
- jcranmer 2y agoUnless you need to run programs with arguments with spaces in their name. Not that long ago, I needed to write a script that would grab the last command in a shell file that had a list of commands, add an argument or two to it, and run it. Which proceeded to barf because the command had quotes, for some of the arguments had necessary spaces in them, and I couldn't figure out the way to get the necessary string in a way that bash could properly execute the command. My eventual solution was to just use python's shlex module to parse the line and output an executable command (and remark that I should have just started the script in Python in the first place)...
- PhilipRoman 2y ago>Unless you need to run programs with arguments with spaces in their name. It does that just fine, I don't have all the details obviously but it sounds like printf %q or ${arg@Q} is your friend here.
- wiseowise 2y ago> ${arg@Q} Right, how couldn’t they know to type actual gibberish to make it work.
- PhilipRoman 2y agoGoogle "bash escape string", click on first result? It mentions both of these options btw. It's not like it's any easier with python, if I'm new to the language I have to look up shlex anyway.
- skydhash 2y agoNot gibberish. It’s all explained there: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html https://www.gnu.org/software/bash/manual/html_node/Shell-Par... If you’re doing stuff with something, why not read the manual os that thing? Or, at least, the section related to what you’re doing?
- jcranmer 2y agoI have a variable, say $X, that contains a command, and the command has quoted arguments with spaces, let's say <<echo "A B">>. Now how do I execute it? If I do $X, the result <<"A B">>, not <<A B>>. If I do "$X", it fails, because there's no filename <<echo "A B">> to execute. In a program with proper arrays, I can instead parse X to the strings ["echo", "A B"], and then run execve to run ["echo", "A B"] without any issues. And adding another argument to make ["echo", "A B", "C"] is damn trivial. No, bash doesn't handle arguments with spaces just fine. If you do the basic, obvious way to do things, things break, and you're expected to know magic invocations to do the right thing to handle potential spaces.
- PhilipRoman 2y agoI see your point, but I still have to disagree. Your task would be inherently complicated for any language - imagine trying to properly add another argument to a function call written in python. In fact it has nothing to do with spaces in arguments, but rather re-parsing strings as shell commands which is always a tricky task. >In a program with proper arrays, I can instead parse X to the strings Well, if I were to write it in Java or Javascript, both of which have excellent support for arrays (the shell does as well, actually - via "set"), it still wouldn't get me an inch closer to achieving the goal. It seems to me that the real problem is lack of a first-class shell grammar parser, which affects most languages, not just the shell itself. Python just happens to have a shell grammar parser that is shipped with the installation.
- databasher 2y agoWith lines.txt: printf '(%s)\n' "one two" "three four five" echo "How you doin'?" Running: while read -r line; do eval command=($line) command+=("arg with spaces" "other arg") "${command[@]}" done < lines.txt Yields (one two) (three four five) (arg with spaces) (other arg) How you doin'? arg with spaces other arg
- throwaway290232 2y ago[dead]
- citizen_friend 2y agoIts always a great tool if you need to automate a computer task.
- yjftsjthsd-h 2y agoShell is good for running a bunch of other programs and doing file operations. In that niche it seems unbeatable, and outside that niche it's generally a terrible choice.
- voidfunc 2y agoAlmost never but there's a lot of folks that have gaslit themselves into believing "its the right tool for the job". There's no requirement that a shell language suck as much as bash.
- patmcc 2y agoThere's a server. You can ssh into it, but you don't have root. You don't know if it'll have python (2 or 3), perl, ruby, node, whatever. You need to write a cronjob that'll run every 5 minutes, or a script that'll do something on-demand, or just a one-off to clean some stuff up or grab particular log files or something. Performance isn't going to be a problem. User input isn't going to be a problem. If you never run into this problem, fair enough, maybe you don't need bash. A lot of us do.
- NegativeLatency 2y agoI've used linux brew a couple of times to escape this problem, able to get a newer version of python or ruby instead of bash: https://docs.brew.sh/Homebrew-on-Linux https://docs.brew.sh/Homebrew-on-Linux
- pxc 2y agoNix is great for this, if you have the disk space. It's nicest if Nix is already installed on the box by a sysadmin, but you can use it in a pivot_root environment if you don't have a cooperative sysadmin.
- mediumsmart 2y agoI don’t know if it’s the correct tool but my llm used it to replace a weather widget iFrame hazard on a static site. It’s a cronjob that lynxdumps the weather forecast, writes the values into an html block and then sshs into the clients server that has the site (but no lynx) and replaces the html block in all html files. Runs every hour and might work for years, we shall see.