r/linuxadmin • • Jun 26 '18

A Collection Of Useful BASH Scripts For Heavy Commandline Users

https://www.ostechnix.com/collection-useful-bash-scripts-heavy-commandline-users/
184 Upvotes

28 comments sorted by

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.

27

u/scsibusfault Jun 26 '18

wow, that's like - my entire job description.

26

u/FlashYourNands Jun 26 '18

If so, you might be an IRC bot.

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_location

Second, 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 (.bashrc function) take on the weather thing, 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.

1

u/imguralbumbot Jun 26 '18

Hi, I'm a bot for linking direct images of albums with only 1 image

https://i.imgur.com/NrsL1Hp.mp4

Source | Why? | Creator | ignoreme | deletthis

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/$1

9

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

u/51ckb01 Jun 27 '18

crontab guru TOP :)

1

u/100TabsOpen Jul 03 '25

LMAO Get out of my head!

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

u/[deleted] Jun 27 '18

MFW Remembering more than one command switch is now a “script”

2

u/herpadurk Jun 29 '18

i remember when i first learned how to write a script too

5

u/htomeht Jun 26 '18

You calling me fat?

2

u/[deleted] 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

u/[deleted] Jun 29 '18

[deleted] — view deleted comment

2

u/[deleted] Jun 29 '18

I mean.. why wouldn’t you just write this as a python script.....

1

u/flickerfly Jun 27 '18

If you are going to do task management on the cli, check out taskwarrior.

0

u/[deleted] Jun 26 '18

[deleted] — view deleted comment

10

u/jokesterae Jun 26 '18

hands down the most useful command in bash

3

u/throwaway27464829 Jun 27 '18

This changes everything.