Hacker News new | past | comments | ask | show | jobs | submit login
Bash-Snippets: A collection of small bash scripts for heavy terminal users (github.com/alexanderepstein)
277 points by epstein43 on July 14, 2017 | hide | past | favorite | 51 comments



It seems a bit silly for nearly all of the scripts to first ping Google before doing anything. It seems to want to check if internet is available (by GETing Google) before doing anything else. It performs this check using nc, however it also depends on either wget, curl or fetch for the actual HTTP requests.

I'd just drop the ping to google entirely. If wget/curl/fetch fail, so be it. No need to introduce additional slowness for the small chance internet isn't working.


Shouldn't the pattern be:

1. Contact service you want result from

2. If 1 fails, THEN contact Google

3. if 2 fails, internet is down, otherwise, service is down


No, the pattern should be:

1. Run script / snippet

2. If 1 fails, it failed, like any other *nix tool.

... there is zero need to get Google involved.


In addition, the use of "nc" to ping google is bad. This causes the scripts to break in all places that use an http proxy. You already have wget/curl which honors the HTTP_PROXY environment variable. Just use that ...


This seems insanely over-engineered. So I re-wrote most of them as bash alias one-liners.

  # Encrypt a file
  function encrypt() { openssl enc -aes-256-cbc -salt -a -in $1 -out $2 ; }

  # Decrypt a file
  function decrypt() { openssl enc -aes-256-cbc -d -a -in $1 -out $2 ; }

  # Fetch weather forecast
  function weather() { curl "http://wttr.in/$1"; }

  # Convert input text into a QR code
  function qrify() { curl "http://qrenco.de/$1"; }

  # Fetch information about a stock
  function stock() { curl -s "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=AAPL&apikey=KPCCCRJVMOGN9L6T" | awk '/\. / {$1=""; gsub("\"|,",""); print $0}'; }

  # Conversion rate between currencies
  function currency { curl -s "http://api.fixer.io/latest?base=$1&symbols=$2" | grep -Eo "[0-9]*[.][0-9]*"; }

  # Fetch movie info
  function movie { curl -s "http://www.omdbapi.com/?t=${1/ /+}&apikey=946f500a" | jq ". | {Title, Year, Ratings:[.Ratings[1].Source, .Ratings[1].Value ], Rated, Genre, Director, Actors, Plot}" | awk -F "\"" '/:|%|Tomato/ {print $2 $3 $4}' | sed '/: \[/d' | perl  -pe 's/,\n/: /'; }

  # Fetch cheatsheet
  function cheat() { curl "http://cheat.sh/$1"; }

  # Fetches DNS nameserver ???
  function dns_nameserver() { cat /etc/resolv.conf | grep -i ^nameserver | cut -d ' ' -f2; }

  # Fetches WAN ip address ???
  function wan_search() { dig +short myip.opendns.com @resolver1.opendns.com ; }

  # Show actual destinatoin of a tinyurl. Eg: untiny "tinyurl.com/savepii"
  function untiny() { curl -s "http://x.datasig.io/short?url=http://$1" | awk -F '"' '/d


Very nice, but if you lose the function keyword and just use

  name () {... ; }
you'll have greater cross-shell compatibility and they'll be a little shorter.


Thanks. Good idea.


I see you made the changes in a new post below. Very cool, but maybe a gist or repo on github would be better?


Cute rewrites, but I think you could do better a lot of the time.

For example rather than using `cat`, `grep`, and `cut`, just use awk:

     function dns_nameserver() {
       awk '/nameserver/ {print $2}' /etc/resolv.conf 
     }


Nice. Will do.


In the stock() function, replace AAPL with $1.


Thanks. will do.


In addition to ytview; mpsyt (https://github.com/mps-youtube/mps-youtube) is a great tool for finding, creating playlists, and playing audio from youtube.

And for Pandora, there is Pianobar, a console based pandora player: https://github.com/PromyLOPh/pianobar


Wow, qrify is pretty clever! Aren't there multiple encodings? I wonder how easily this could be used to generate the densest encoding?

EDIT: ...oh, it sends your data to a public service, which is not what I would prefer. But the cleverness of the service (rendering the pixels via ASCII) seems like it would make a great local utility.

Another EDIT: the encoding lib is LGPL, support for ASCII rendering contributed by Ralf Ertzinger -- https://github.com/fukuchi/libqrencode


Do instead:

  apt-get install qrencode
  qrencode -v 1 -o hn.png "https://news.ycombinator.com/"


Or:

    qrencode -t ansi https://news.ycombinator.com/
for output to the terminal.

(Type can also be one of ansi256, ascii, asciii, utf8, or ansiutf8.)


I expected bash/python/perl scripts but it's just wrappers around APIs.


Not bash, but oh-my-zsh changed my life in the terminal: http://ohmyz.sh

Until then I had used bash exclusively for years. Now I will absolutely never go back.


I never really though about it. Had to put with c-shells and k-shells and then bash. Anything in particular which made it better? What super-power or feature would you lose if you went back to bash from this z-shell you speak of.


It's a million little things. tab completion is better in every way. It's not that bash doesn't have this feature, but it's better in zsh.

But the real magic is zsh + oh-my-zsh. oh-my-zsh is a set of curated aliases and other scripts for improving your experience with a lot of different tools.

For example, I use all these extensions:

    plugins=(cargo docker git heroku mvn rust)
The git aliases in particular are really nice. Example, gpsup is:

    gpsup='git push --set-upstream origin $(git_current_branch)'
Which is really handy. On top of that, I really like the support for the customizable ps1's. I use powerlevel9k, and have it customized to tell me about the time the last program took to run, current git status, current time, etc.

https://github.com/bhilburn/powerlevel9k

try it, you won't be disappointed.


Here are the updates with everyones changes:

  # Encrypt a file
  encrypt() { openssl enc -aes-256-cbc -salt -a -in $1 -out $2; }

  # Decrypt a file
  decrypt() { openssl enc -aes-256-cbc -d -a -in $1 -out $2; }

  # Fetch weather forecast
  weather() { curl "http://wttr.in/$1"; }

  # Convert input text into a QR code
  qrify() { curl "http://qrenco.de/$1"; }

  # Fetch information about a stock
  stock() { curl -s "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$1&apikey=KPCCCRJVMOGN9L6T" | awk '/\. / {$1=""; gsub("\"|,",""); print $0}'; }

  # Conversion rate between currencies
  currency() { curl -s "http://api.fixer.io/latest?base=${1^^}&symbols=${2^^}" | grep -Eo "[0-9]*[.][0-9]*"; }

  # Fetch cheatsheet
  cheat() { curl "http://cheat.sh/$1"; }

  # Fetches DNS nameserver
  dns_nameserver() { awk '/nameserver/ {print $2}' /etc/resolv.conf; }

  # Fetches WAN ip address
  wan_search() { dig +short myip.opendns.com @resolver1.opendns.com; }

  # Show actual destination of a tinyurl. Eg: untiny "tinyurl.com/savepii"
  untiny() { curl -s "http://x.datasig.io/short?url=http://$1" | awk -F '"' '/dest/ {print $4}'; }

  # Get your remote IP addr
  myip() { curl -s http://x.datasig.io/me | awk -F"\"" '/ip/ {print $4}'; }

  # Fetch movie info
  movie() { curl -s "http://www.omdbapi.com/?t=${1/ /+}&apikey=946f500a" | jq ". | {Ratings:[.Ratings[1].Source, .Ratings[1].Value ], Plot, Actors, Director, Genre, Rated, Year, Title}" | sed -e '/Ratings/{N;N;N;s/\n/ /g;s/"Ratings": \[     //;s/,    /:/;s/   ]//}' | awk -F'"' '/: / {print $2 ": " $4}'; }


Metaquestion -- I'm interested in how the poster came across this this? I've been seeing things like this pop up in my google news feed for the past month or so -- including this specific article more than once.


Is Linyos Torovoltos a joke name?


Almost certainly yes. If it's not then this person is very unlucky. It's listed on urban dict and used in http://www.adequacy.org/stories/2001.12.2.42056.2147.html where it says Linux is a virus (ironically a translation of this was my first information about Linux ever when I was a gullible teen.., I clearly remember the 'LILO' part, since when I was starting to get into it Linux was already using GRUB).


I believe that originated here: http://www.adequacy.org/stories/2001.12.2.42056.2147.html

Scroll down to point 8.

More context: https://en.m.wikipedia.org/wiki/Adequacy.org


I created https://github.com/adtac/climate a while ago. It's basically a lot of shell commands in one tool.

(Note: it's mostly an educational tool; for example, you could enable an option to make climate print the actual command before executing so that you can learn your way around using the shell effectively.)


Speaking of which... if you speak portuguese you might want to try "Funções ZZ" (funcoeszz.net).

The most beloved bash script collection S2


Love that logo. Did you create that yourself?

I've been keeping my bash scrips in github gists but I like this organization as well.


That logo was the one that was voted on* to replace the old bash logo IIRC.


> Does not work with Cygwin or Mysys2.

:(

I would be curious to know why - if it's just bash, Cygwin has great support for it.


I've been getting back into using the terminal and bash recently, so these are nice to have.


Nice set of scripts. Is it available in AUR?


Lots of Bash bugs, wow.


  $ llbin | nlines
  738
Apparently I've written 738 bash/python scripts (and counting). Should probably publish those someday...


There's no day like today, as they say


Releasing them is one thing. Documenting them is another. This holds many of us back from releasing our work.


If there are some things that are really useful, you can quickly call them out in the readme, but even if you don't people will browse and figure out how to use them anyway if they want to. There are plenty of projects with poor or entirely absent documentation that still see use.

In some ways I could see how going through the effort of attempting to have documentation might just make things worse because then people might have an expectation that you're going to keep working on it or respond to filed issues. Anyway I thought the more common excuses were "eh, this isn't that useful, no one will look at it let alone use it", or "I'd have to go through everything and make sure I didn't leave my password in a comment or something stupid", or maybe the most common "there's a lot of hacky code, I don't want to have random people / a potential future employer see this and think I suck at coding..."


I had done a kind of framework to manage my Bash functions as packages (cf http://github.com/lolive/shinyshell). Even the documentation of functions and packages was tool-assisted (à la Javadoc). But documenting is a kind of chicken and egg problem. It requires an outsider point of view, that is difficult to have when you are in the developpent phase, and everything is in your mind.

Nevertheless i agree that documentation is a key part of the success of a project.


Please do


Some tools are for people who live in terminal but want to do things outside of computers- weather/currency/stock et al. May not be related to everyone.

But there are some tools which are compute related and I'd recommend everyone try once:

1) cheat. Total lifesaver. I used to backup my histories but I no longer do so since most commands I use have an example there.

2) qrify looks good, not sure how often i'll use.

About crypt: I'd suggest installing openssl and using tools in there. Crypto is hard to get right, not to dissuade anyone from trying to create but as an end user, always use something widely used.


The crypt script is just doing this:

openssl enc -aes-256-cbc -salt -a -in $1 -out $2


The first one is just curl wttr.in. Which is IMO easier to remember.


that's easier to remember than 'weather' for getting the weather?


`function weather() { curl "http://wttr.in/$1";}`

Not sure why it needs to be more complicated than that.


the function keyword is unnecessary - removing it makes the function creation more cross-compatible with different shells.


This will go perfect with my own chrestomathy!


[flagged]


Did you only create your account to poop on this guy's idea?


+1. I also created an account to say, "Lots of Bash bugs."

Thank you for the cool ideas!


Meh.


Guys, you're crazy ;-)


A collection of things that should have been written in a proper language ;-)




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: