Bash-Snippets: A collection of small bash scripts for heavy terminal users
github.com
github.com
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.
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
1. Run script / snippet
2. If 1 fails, it failed, like any other *nix tool.
... there is zero need to get Google involved.
# 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 name () {... ; }
you'll have greater cross-shell compatibility and they'll be a little shorter.For example rather than using `cat`, `grep`, and `cut`, just use awk:
function dns_nameserver() {
awk '/nameserver/ {print $2}' /etc/resolv.conf
}And for Pandora, there is Pianobar, a console based pandora player: https://github.com/PromyLOPh/pianobar
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
apt-get install qrencode
qrencode -v 1 -o hn.png "https://news.ycombinator.com/" qrencode -t ansi https://news.ycombinator.com/
for output to the terminal.(Type can also be one of ansi256, ascii, asciii, utf8, or ansiutf8.)
Until then I had used bash exclusively for years. Now I will absolutely never go back.
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.
# 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}'; }Scroll down to point 8.
More context: https://en.m.wikipedia.org/wiki/Adequacy.org
(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.)
The most beloved bash script collection S2
I've been keeping my bash scrips in github gists but I like this organization as well.
:(
I would be curious to know why - if it's just bash, Cygwin has great support for it.
$ llbin | nlines
738
Apparently I've written 738 bash/python scripts (and counting). Should probably publish those someday...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..."
Nevertheless i agree that documentation is a key part of the success of a project.
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.
openssl enc -aes-256-cbc -salt -a -in $1 -out $2
Not sure why it needs to be more complicated than that.