Earlier quoted context omitted.
> You are source controlling your shell scripts right? My personal heuristic for shell scripts are that if I care enough about them to put them under source control, they shouldn't be a shell script.
I have seen entirely too many Python scripts that are poor reimplementations of shell scripts that don't get the edge cases right, have more security vulnerabilities, etc. than a shell script a fifth of the size. There are things you simply can't do in other languages without excessive verbosity. (One that came up at my workplace recently is using Use the right tool for the job, and then put it in version control (an…
Safe ways to do things in bash
151–160 of 255 posts
Re: Safe ways to do things in bash
#152I've written a ridiculous amount of shell script in my day, especially when doing "devops" before we had a term like "devops" to describe it. I've fallen in almost every pit bash has. With that background, here is my opinion. 1. This article contains excellent advice and should be starred for later retrieval. 2. Having basic scripting skills will make you a way better programmer. Many times I've done huge refactors a…
I probably have a similar history but I've come to the belief that you should not use Bash if you're doing anything fancy or long (>10 lines). Just use Perl, Python or Ruby. One or all of them is installed on every machine you are likely to use.
Re: Safe ways to do things in bash
#153I've written a ridiculous amount of shell script in my day, especially when doing "devops" before we had a term like "devops" to describe it. I've fallen in almost every pit bash has. With that background, here is my opinion. 1. This article contains excellent advice and should be starred for later retrieval. 2. Having basic scripting skills will make you a way better programmer. Many times I've done huge refactors a…
Even by looking at these examples, you see it has less verbosity like "then" and "do", you can reference arguments as $argv instead of cryptic $@ and exit status code as $status instead of $? which is confusing with $! and the likes.
https://blog.codeship.com/lets-talk-about-shell-scripting/
https://fishshell.com/docs/current/tutorial.html
Shell is such an integral part of admins and programmers workflow yet I find it hard to believe this field has been so slow at improving. Even the fish site jokingly states "Finally, a command line shell for the 90s" implying the others are even older.
Re: Safe ways to do things in bash
#154Earlier quoted context omitted.
I probably have a similar history but I've come to the belief that you should not use Bash if you're doing anything fancy or long (>10 lines). Just use Perl, Python or Ruby. One or all of them is installed on every machine you are likely to use.
Yes, but which one, and which version? This is especially painful with Python ;)
Re: Safe ways to do things in bash
#155I've written a ridiculous amount of shell script in my day, especially when doing "devops" before we had a term like "devops" to describe it. I've fallen in almost every pit bash has. With that background, here is my opinion. 1. This article contains excellent advice and should be starred for later retrieval. 2. Having basic scripting skills will make you a way better programmer. Many times I've done huge refactors a…
I did a talk on this a while ago: https://www.youtube.com/watch?v=pb3k0sGKrjQ&t=457s 'Take bash seriously'
Re: Safe ways to do things in bash
#156I always think — when a programming/scripting language requires this much bizarre knowledge just to write basic code that performs basic tasks, perhaps it is time for that language to be retired. I really don't understand why bash still exists. I've switched to fish and am much happier with the change.
Re: Safe ways to do things in bash
#157(Luckily it entered vim relatively soon, from where I could kill the process).
Re: Safe ways to do things in bash
#158I always think — when a programming/scripting language requires this much bizarre knowledge just to write basic code that performs basic tasks, perhaps it is time for that language to be retired. I really don't understand why bash still exists. I've switched to fish and am much happier with the change.
Because it is ubiquitous. You can virtually guarantee that bash will be found on any arbitrary unix-like system.
How often can you just install fish or use other programming language instead of being forced to use bash?
It's sad people keep using the default just because of being afraid that the next system you touch might not have it and you waste tremendous amount of productivity without using something better.
I know a guy who used vim with default config for that reason. Utterly nonsense.
Re: Safe ways to do things in bash
#159How does one properly quote an argument for a command that can contain spaces? For example: parent_dir = "$(basename $dir)" How to quote $dir here? What if it contains spaces or other special characters? That's what I dislike about Bash. Also, always start your scripts with set -e This prevents script from running after error, without any messages though. Also, I always make mistakes when using [ and [[.
echo hello "$(
echo hello \
| sed 's,hello,world,g'
)"
[ is an actual binary file that gets invoked, called test. You can "man [" or "man test" to learn about all of its uses.I use [ exclusively, but with && and ||, instead of -a and -o respectfully.
e.g. [ -e "$file" ] && [ -w "$file" ] instead of [ -e "$file" -a -w "$file" ].
[1] http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu...
Re: Safe ways to do things in bash
#160Why ever resort to shell scripts when we have languages like ruby / python for anything more complex than installs? Clarity is king people
foo | awk '/bar/ {print $3}'
is more clear than import subprocess
foo = subprocess.Popen(['foo'], stdout=subprocess.PIPE)
for line in foo.stdout:
if 'bar' in line:
try:
print(line.split()[2])
except IndexError:
print('')
Sometimes shell scripts are more clear. Especially for tasks that involve running lots of external commands.