r/linuxadmin • u/ask2sk • Jun 26 '18
A Collection Of Useful BASH Scripts For Heavy Commandline Users
https://www.ostechnix.com/collection-useful-bash-scripts-heavy-commandline-users/45
u/ase1590 Jun 26 '18
Weather – Displays weather details of your place.
As if we didn't have enough fucking simple weather apps.
or why even bother with an app when you can just do curl wttr.in/your_location
And yet no one has bothered to implement a fucking radar map in any of these.
So then I have to be the change I want to see and learn Qt5 and slowly design a fucking functional radar widget.
/rant
12
u/cstrawfield Jun 26 '18 edited Jun 26 '18
First, thank you for showing me
curl wttr.in/your_locationSecond, the radar widget rocks.
6
u/raymus Jun 27 '18
fucking functional radar widget.
What a cool widget! Have you published the source for it anywhere?
2
u/setdx Jun 27 '18
I downloaded this just for the hell of it, and then rewrote five of the scripts to do what I actually wanted them to do, and now aliasing 'weather' to 'curl wttr.in/your_location' just replaced a 200-line bash script. Cheers! I took this as more of a "here are a few ideas for things that might speed up your productivity" rather than things that are actually useful.
3
u/whetu Jun 27 '18
FWIW, here's my (
.bashrcfunction) take on theweatherthing, because it comes up in r/bash and r/commandline all the goddamned time:# Get local weather and present it nicely weather() { # We require 'curl' so check for it if ! exists curl; then printf '%s\n' "[ERROR] weather: This command requires 'curl', please install it." return 1 fi # If no arg is given, default to Wellington NZ curl -m 10 "http://wttr.in/${*:-Wellington}" 2>/dev/null || printf '%s\n' "[ERROR] weather: Could not connect to weather service." }This way you can call it with whatever location you like, or it will default to your current location (obviously Wellington, NZ for me). I have also found that leaving the service to its built-in geolocation can be a bit hit and miss. For a while, it detected me as being in Serbia... go figure.
10
u/WOLF3D_exe Jun 26 '18 edited Jun 26 '18
#!/usr/bin/env bash
# Author: Alexander Epstein https://github.com/alexanderepstein
currentVersion="1.22.0"
declare -a tools=(bash-snippets cheat cloudup crypt cryptocurrency currency geo lyrics meme movies newton pwned qrify short siteciphers stocks taste todo transfer weather ytview)
declare -a extraLinuxTools=(maps)
declare -a extraDarwinTools
usedGithubInstallMethod="0"
prefix="/usr/local"
askInstall()
{
echo -n "Do you wish to install $1 [Y/n]: "
read -r answer
if [[ "$answer" == [Yy] ]]; then
cd $1 || return 1
echo -n "Installing $1: "
chmod a+x $1
cp $1 /usr/local/bin > /dev/null 2>&1 || { echo "Failure"; echo "Error copying file, try running install script as sudo"; exit 1; }chmod on $1 and cp on $1, but no check to see if $1 is set....
At least they check $1 before running rm -f
askUninstall()
{
if [[ -f /usr/local/bin/$1 ]]; then
echo -n "Do you wish to uninstall $1 [Y/n]: "
read -r answer
if [[ "$answer" == [Yy] ]]; then
echo -n "Removing $1: "
rm -f /usr/local/bin/$19
u/mriswithe Jun 26 '18
Meh rm -f on a directory still won't delete it. Then again I have never made a bash script and been like 'yeah this will help people, and is worth sharing'. Usually it is: 'Meh that will work I guess....most the time.'
7
u/mobilediesel Jun 26 '18
Meh that will work I guess....most the time.
Then months later something stops working as expected so you go to edit the script:
What idiot wrote this garbage? ... oh ... I'm the idiot
good thing I never shared this on github or something!2
u/mriswithe Jun 26 '18
Yep basically that. Hell I am writing scripts and the like for infra as code pipelines at work, and it is only recently I feel less than utter embarrassment at committing some of my work to our internal repo.
11
u/whetu Jun 26 '18
If you haven't heard of it yet: shellcheck.net. Absolute game changer.
4
u/WOLF3D_exe Jun 27 '18
Thanks, just bookmarked that beside https://explainshell.com/ and https://crontab.guru/
1
1
1
u/100TabsOpen Jul 03 '25
Sorry for the necro, I'm just glad it's not just me who feels like this lol Imposter syndrome is real.
1
u/mriswithe Jul 03 '25
7 years later, I still would never hand out a bash script to others. Python now? Yes, Python then? No
6
2
2
5
2
Jun 27 '18
RANT
declare -a should just be an alias for exit 1; python
I know these are just silly examples, but if you ever find yourself needing a data structure and you’re starting to write functions that vaguely mimic something object oriented it’s probably time to start using anything other than bash.
/RANT
0
1
0
109
u/FlashYourNands Jun 26 '18
These are perfect if you're a Bryan Lunduke wannabe.
I don't think scripts for looking up weather and stocks, creating memes, watching youtube, and getting book recommendations are useful for linux admins, though.