Ask HN: What are your best shell scripts that you use?
I saw a note[0] about using a script to activate Redshift in Linux and wondered what other cool, useful, or otherwise interesting scripts you might be using.
[0] https://news.ycombinator.com/item?id=21916949
63 comments
[ 1.4 ms ] story [ 123 ms ] threadIf VPN stops working, I can always check to see if the address changed. May not be the best solution, but it works for me.
Script itself: https://sirtoffski.github.io/docs/my-pub-ip
But it’s more like a proxy to be geolocalized in France (some services block foreign IPs) when I’m not.
Pretty handy.
lw which does ls -l “$(which -a “$1”)”
ew which does ”$EDITOR” “$(which “$1”)”
newsh which does touch ~/bin/“$1” && chmod +x ~/bin/“$1” && “$EDITOR” ~/bin/“$1”
And habitat (hab) and arch pkgbuild, which use shell scripts as their package DSL... the former I’ve hacked up to screen-scrape package versions (due to the dearth of RDF/metalink usage in release artifact publication) and check gpg keys.
I also have another one called "store" which does `mkdir "${@: -1}" && mv $@`. I basically wanted an easy way to move everything into a new directory ("store * backup").
btex - interactively compile LaTeX to PDF or PS or DVI, highlighting warnings and errors ( https://github.com/ppurka/btex )
searchforfile - search for files interactively with results "as-you-type" ( https://github.com/ppurka/searchforfile )
displaymessage - wrapper around a bunch of gui dialogs ( https://github.com/ppurka/displaymessage )
A couple more in my GitHub account.
I have another one that splits AAX files from Audible by chapters: https://github.com/captn3m0/Scripts/blob/master/split-audio-... (After stripping the DRM)
ansi2html converts terminal output to HTML: https://github.com/captn3m0/Scripts/blob/master/ansi2html.sh
dydns.sh sets up a dynamic DNS entry on CloudFlare against my current local IP address. https://github.com/captn3m0/Scripts/blob/master/dydns.sh
emojify converts emoji shortcodes to emojies on the command line: https://github.com/captn3m0/Scripts/blob/master/emojify
vtt2srt: convert vtt subtitle files to SRT https://github.com/captn3m0/Scripts/blob/master/vtt2srt
#!/usr/bin/perl
$| = 1; $i = 0;
if ($#ARGV < 0) { $number = 1000; } else { $number = shift(@ARGV); }
while (<STDIN>) { print '.' unless ($i % $number); $i++; }
print "\n";
https://linux.die.net/man/1/pv
So, if you have a command that produces lots of lines of output (e.g., for demo purposes, "yes" - which literally floods the terminal with lines):
Example progress output, showing lines processed (total & rate):The script then runs "devices", looks for an alias "niss", and substitutes in the corresponding address. I use expect in Python to script it all together.
All commands you enter pass directly to bluetoothctl except aliases in curly braces are replaced. You can use it interactively or pipe to it.
Complex piped stuff may or may not actually work. If stdin isn't a tty the program exits once bluetoothctl reports a success/failure after it gets an EOF on stdin. This means you can write `btctl <<< "connect {my_device_alias}"` and it will exit once it's done connecting or couldn't connect.
If you're using it interactively it only waits 0.01 seconds for results before displaying them and moving on to the next input() (whereas bluetoothctl will asynchronously display more results even while you're typing in a new input) so you may need to spam enter to see results.
https://gist.github.com/nl5887/56912b70b782baa4bd580ae22bde6...
https://github.com/robgibbons/highlander/
These days I use Ubuntu MATE which has a Mac-like dock with the same functionality for free.
Boom - histogram!
So, this will automatically file away your screenshots to your $HOME/Documents/screenshot/ folder, organized by year/year_month/file.png.
Where file.png is in the format yyyy_mmdd_hhmmss.png.
I use it to take an area screenshot of all my research notes, useful comments, gold nuggets, etc. The automatic folder organization files it away nicely, and keeps it organized as the years go by.
Create it and set the execute bit:
Then copy the contents below: Then map it to the printscreen key.https://en.m.wikipedia.org/wiki/Scrot
https://gist.github.com/davidmoreno/c049e922e41aaa94e18955b9...
Might be useful, I'm not entirely sure if it suits your case because my experience with inotify is limited to playing around with it and nothing substantial coming from it.
1: http://z3bra.org/wendy/ (Author's blog post: http://blog.z3bra.org/2015/03/under-wendys-dress.html)
https://github.com/ivanmaeder/vimv
Basically it loads up the output of `ls` into an editor, then runs a `mv` command for each line.
Not something I use daily, but still handy.
Also worth bearing in mind most shells can do this sort of thing easily enough too.
The example from your video with zsh's zmv (bundled: https://github.com/zsh-users/zsh/blob/master/Functions/Misc/... )
Check all also all the other tools from that package: https://manpages.debian.org/buster/moreutils/index.html
The package's tagline is "collection of the Unix tools that nobody thought to write long ago, when Unix was young", so pretty relevant to the whole topic.
There's a bit of scaffolding around things going into a docs folder, some JS and CSS tweaks for the various output formats (ePub, HTML & PDF), but there is a very simple script I use to reduce some friction whilst writing.
It's nice to have a lot of small files. However, you may often want to:
1. Rearrange those files
2. Insert a new section between two other files
So, I hacked together this incredibly tiny script that means anytime I want to add anything, I can immediately get to wherever I'm up to:
Bonus: It outputs the current filename to the console, so that if I want to add stuff after where I'm currently at, I have the starting number.The entire scaffold for my books is three scripts, some standard CSS, JS and YAML. Which makes setting up a new one to match my sensibilities, quick and simple.
- SSH wrappers/eval-ed Aliases, which do: exec ssh -l user host "$@"
- AWS wrappers, that allow me to: exec aws --profile blah "$@"
- DB wrappers, that allow me to hit a target DB
- cred: Allows me to pick creds from a password manager / keychain independent of OS; Usually a polyfill.
- subtract, intersect, union, sample: for handling one liner data, incl. CSV files
- csvcut: Python wrapper though that does -f, -d, -c but on CSV files
- j2y, y2j: Python wrappers for JSON to YAML, YAML to JSON
- envsubst, watch: polyfills for some environments
- vdimount, imgmount: Allow loopback mounting for partitions / images for VirtualBox/Qemu
- nanny: Nanny for starting processes, passing listen sockets on restart, change NS properties; Usually a polyfill
sub [2] is a simple shell based template processor.
[1]: https://github.com/oxplot/gists/blob/master/flashserve
[2]: https://github.com/oxplot/gists/blob/master/sub
[1] - https://www.commandlinefu.com/commands/browse
(This goes with the idea, which I also try to encourage, that any repeated process should be first documented in some rough form at least (like a personal note-base or team wiki), then improved over time, via improving the doc, scripting it, and moving toward full automation based on balancing considerations of cost/benefit over time, YAGNI, and avoiding debt, and ideas from the "checklist manifesto", such as the realization that even smart people can forget important things, drop the ball sometimes, or leave.)
Edit: This also lets me script away differences between platforms, so I can just remember my usual little 1-3 letter command and it takes care of the rest, while the script records the details for reference.
But I have enough to remember as it is, so I make backups of my scripts (as with everything personal on my main computer) using tar/gz/gpg/rsync/scp, optical media, etc., so I can refer to them when memory fails. Overall, I'd have to guess it depends on the person and the priorities, and how well one way works for you vs. the other. I currently always use a computer under my control, and over time, enough has stuck in my memory.
I also want to save my hands by making things ergonomic (shorter to type when practical), as the hands are useful in the long-term. :)
Edit: Do you mean, personal, local notes on tasks, as the notes evolve into scripts? I also keep a lot of personal notes which I can export into a web site (similarly to putting into a wiki, in a very loose comparison, an example being my site at http://lukecall.net or the other at http://onemodel.org), using something like a big, efficient, outline of all my notes. (I would probably put it in org-vim like org-mode, or maybe taskwarrior, if I didn't have http://onemodel.org, which I hope to make much easier to install, sometime.)
Thanks.
Is there an easy way other than copy paste to rectify this?
The other comment in this discussion (do Ctrl+F for "most wizardly of the wizards") might have a good idea, but I don't understand it yet.
This, to me, seems like part of the Golden Rule of working together (treat others the way one would want to be treated), as are things like not leaving useful but hard-to-maintain messes behind for others without some clarification, or having important processes that only one person knows how to do, so when they leave it is a crisis (or no one really knows how to do it so it is a crisis every time).</speech>
Feedback welcome. :)