
Bash-Snippets: A collection of small bash scripts for heavy terminal users - epstein43
https://github.com/alexanderepstein/Bash-Snippets
======
tokenizerrr
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.

~~~
ehsankia
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

~~~
urda
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.

------
skanga
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

~~~
aaronchall
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.

~~~
skanga
Thanks. Good idea.

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

------
elliottcarlson
In addition to ytview; mpsyt ([https://github.com/mps-youtube/mps-
youtube](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](https://github.com/PromyLOPh/pianobar)

------
wyldfire
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](https://github.com/fukuchi/libqrencode)

~~~
xorcist
Do instead:

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

~~~
kseistrup
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.)

------
sillysaurus3

      $ llbin | nlines
      738
    

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

~~~
tambourine_man
There's no day like today, as they say

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

~~~
Jach
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..."

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

------
bluejekyll
Not bash, but oh-my-zsh changed my life in the terminal:
[http://ohmyz.sh](http://ohmyz.sh)

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

~~~
nthcolumn
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.

~~~
bluejekyll
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](https://github.com/bhilburn/powerlevel9k)

try it, you won't be disappointed.

------
skanga
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}'; }

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

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

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

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

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

------
johnhenry
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.

------
m-j-fox
Is Linyos Torovoltos a joke name?

~~~
FRex
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](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).

------
adtac
I created [https://github.com/adtac/climate](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.)

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

The most beloved bash script collection S2

------
flanbiscuit
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.

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

------
gkfasdfasdf
> 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.

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

------
kronos29296
Nice set of scripts. Is it available in AUR?

------
invsblduck
Lots of Bash bugs, wow.

------
xyzxyz998
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.

~~~
tyingq
The crypt script is just doing this:

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

------
megamindbrian
This will go perfect with my own chrestomathy!

------
the_duke
Meh.

------
finchisko
Guys, you're crazy ;-)

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

