DEV Community

Cover image for What do you use shell scripts for?
Philippe Desjardins
Philippe Desjardins

Posted on • Updated on

 

What do you use shell scripts for?

Devops? Dev workflows? I'm curious what people are doing with those

Top comments (20)

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

Anything sysadmin related that does not warrant more complex automation such as a Python script or Ansible playbook.

Shell scripts are really powerful if used right. A simple example that’s part of the sysadmin scripts I use myself would be:

#!/bin/sh

tag="${1}"
ctx="${2}"

parent="$(docker inspect ${tag} --format "{{.Parent}}")"
created="$(date -d "$(docker inspect ${tag} --format "{{.Created}}")" +%s)"
parent_created="$(date -d "$(docker inspect ${parent} --format "{{.Created}}")" +%s)"

if [ ${created} -lt ${parent_created} ] ; then
        echo "Parent image updated, rebuilding ${tag}"
        docker build ${ctx} -t ${tag}
else
        if [ `docker run --rm -it ${tag} /bin/sh -c "apk update >/dev/null && (apk upgrade --available --simulate 2>&1 | wc -l)"` -gt 1 ] ; then
                echo "Updates available, rebuilding ${tag}"
                docker build ${ctx} -t ${tag} --no-cache
        else
                echo "No updates available, skipping rebuild of ${tag}"
        fi
fi

This is actually POSIX compliant Bourne shell, not bash, but that’s kind of irrelevant to my point here. The script itself handles updating Alpine Linux based Docker containers built locally if their parent image has been updated or the packages they include have updates in the Alpine repositories. It’s part of the tooling I use with the various Docker Compose projects I have set up on personal systems, as I have a lot of local custom containers based on Alpine Linux that I want to keep up to date while avoiding needless rebuilds. It mostly gets called by a different script that orchestrates the proper order of docker pull, docker build, and docker-compose up -d commands to do a coherent update of said Docker Compose projects while minimizing overall downtime.

Collapse
 
pdesjardins90 profile image
Philippe Desjardins

That's pretty great

Collapse
 
vonheikemen profile image
Heiker

Mostly wrappers around other commands. Got one for apt mostly to "rename" some commands (I'm looking at you apt purge), one for xdotool to move the mouse, one for deno, a "powermenu", a calculator thingy using bc.

 
pdesjardins90 profile image
Philippe Desjardins

You've got to make an article about it, this is so cool

Collapse
 
erickhavel profile image
Erick Havel

I use shell scripts way more than I probably should. For example I smacked together a simple site generator with Bash (with Node/Commonmark.js to convert MD to HTML) the other day. If I were to write something for the general public I would likely have used something like Node, Python or Go, but being aware of the limitations and being able to avoid edge cases as the only user it does its job perfectly well.

Collapse
 
fultonbrowne profile image
Fulton Browne

The Linux distro I use uses a package manager written in POSIX sh

github.com/kisslinux/kiss

I have a power management utility written in POSIX sh

git.sr.ht/~fultonbrowne/fpm

The tool called shellcheck can be used to check to POSIX compliance of your scripts

github.com/koalaman/shellcheck

Collapse
 
jmfayard profile image
Jean-Michel Fayard 🇫🇷🇩🇪🇬🇧🇪🇸🇨🇴 • Edited

My rule is that if the bash script is more than three lines long, it should be rewritten in $YOUR_FAVORITE_LANGUAGE instead of a hack like Bash where you have to google up every time basic things like "How do I do a for loop?"

Collapse
 
jcolag profile image
John Colagioia (he/him)

I do a fair amount of quasi-prototyping using shells, since it's trivial to take repetitious command-line tasks and turn them into a script. I say "quasi," because a lot of the scripts are fine as-is, so I don't convert them to a more typical programming language.

The scripts I currently use regularly are...

  • An "end of day" script that kills my e-mail client, performs a bunch of minor backups of things I'd want immediately in the event of a crash, and take care of some other cleanup tasks.
  • An "overnight" script that does some trivial sleep-tracking (as in I run it when I close my eyes and stop it when I wake up), but it also gives me a simple visual representation of how long it's been running, an on-demand text-to-speech of the current time, a quick note-taking mode, turns the network off/on to prevent notifications, and provides a quick summary of things I need to know for the morning.
  • A script that regenerates my blog (it's a Jekyll static site), including some pre-processing and posting to the open source social networks I use. It also commits the released posts to the git repository, generates the tag pages, and so forth.

I also have a bunch of scripts that coordinate a bunch of small programs, like when I generate my monthly newsletter, since each component is independent (so I don't want them in a combined program, since that's not modular) and it's not worth writing something more complex than "call these in succession and dump the output to the same file before sending it to MailChimp."

Occasionally, something graduates to a (depending on my mood) Ruby or Node script, when the shell code becomes hard to maintain and I need to maintain it, but a lot of it also just stays as-is.

Collapse
 
pdesjardins90 profile image
Philippe Desjardins

Ok now I'm impressed!

Collapse
 
melezhik profile image
Alexey Melezhik

Hi! One can find a lot of bash scripts on sparrowhub.io - a repository of automation tools. Many plugins here are some bash code integrated into high level Raku scenarios.

Collapse
 
melezhik profile image
Alexey Melezhik

It's dead easy to add new plugins written on Bash to the repository. Let me know if someone is interested to distribute their scripts ...

Collapse
 
gwutama profile image
Galuh Utama

Init scripts. That’s all.

Collapse
 
melezhik profile image
Alexey Melezhik

There is a lot of shell scripting in Sparrow plugins - sparrowhub.io

Collapse
 
gkawamoto profile image
Gustavo Kawamoto
  1. Automating things 🤖
  2. The challenge of using a language that has a completely different approach 👊
  3. Self-contained applications/tools 🔧
Collapse
 
luistak profile image
Luís Takahashi
  • Generate changelogs
  • Deep reset scripts, removing every cache and nodemodules (monorepos)
  • Small pipeline stuffs like renaming moving around folders
Collapse
 
unfor19 profile image
Meir Gabay

Exactly what you wrote, DevOps + Dev workflows (pipelines)

Collapse
 
adriangrigore profile image
Adrian Emil Grigore • Edited

Generating static sites mkws.sh. 😜

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git