Daily Bash Scripts

Posted on Jan 6, 2024

During my work I encountered repetitive commands which I reused by typing the command again or by using CTRL + R in the CLI.

That approach is definitely not using the full power of a UNIX system and feels really counterintuitive.

Furthermore, there are some things which I like to do in the morning and be part of my morning routine. By that I mean doing the following:

  • Pull the latest changes from the remote repositories to my local repository and show me the changes. With that I see what happened the past day.
  • Update all local images. This keeps the environment updated and by using always the latest images it’s easier to notice side-effects of changes or if a change which came in the day before may affect todays work.

I found this two things to be really helpful for me, but I didn’t like to change to the directories and execute the commands in every directory.

Thankfully, bash makes it easy to automate such stuff. I wrote it and as a next step used install to put it into the /bin directory.

This enabled me to just execute a command from anywhere where I’m in the CLI right now. So, when opening my machine in the morning the first thing I do is to execute two commands and that’s all to get up-to-date.

The register script

Heart of the registration is the following script which takes care of installing the scripts.

Scanning the directory and using just install on every file would also work, but this keeps a list of scripts to install (in SCRIPTS). That’s important when I don’t want to register a script which is untested/WIP.

BIN_DIR=/bin
SCRIPTS=("some-script.sh")

for SCRIPT_NAME in "${SCRIPTS[@]}"; do
    SCRIPT_BASENAME=$(basename "$SCRIPT_NAME" .sh)

    install -m 755 "$SCRIPT_NAME" "$BIN_DIR/$SCRIPT_BASENAME"

    echo "Script '$SCRIPT_NAME' installed to $BIN_DIR as $SCRIPT_BASENAME and made executable."
done

The script above would make some-script available as a command in the bash.

Other scripts I have

Some other scripts I have are:

  • Show the latest x commits in git by using a specified format.
  • Run a local build of an image and show the logs.
  • Run the basic services of the project I’m working on.
  • Build a local image for test purposes by skipping tests/coverage checks and so on.

Conclusion

Writing scripts for daily and repetitive stuff helped me to speed up my daily routines and workflows.

I recommend to everyone doing the same and to include the scripts into their daily routine.

Want to know more?

Keep on reading and choose one of the related articles. You can also check the home page for my latest thoughts, notes and articles.