Day 2: Writing Custom Shell Scripts is Hard

The second day and I’m still struggling to manage but I made some progress. I definitely need to timeslot so that I’m not up late during the week writing blog posts.

TLDR;

Okay, so first and foremost here are the highlights of what I did.

  • Solved exercise 2 from MIT Missing Semester Course which was not easy for me tbh.
  • Wrote my first custom script outside of the Course to help me organize my blog posts for these next 100 days.
  • Reviewed some SQL to alter a sequence since I messed up an entry in my habits database.

Exercise 2 Solution

Here is the problem from the lecture:

Write bash functions marco and polo that do the following. Whenever you execute marco the current working directory should be saved in some manner, then when you execute polo, no matter what directory you are in, polo should cd you back to the directory where you executed marco. For ease of debugging you can write the code in a file marco.sh and (re)load the definitions to your shell by executing source marco.sh.

My solution looked like this:

function marco { marco="$PWD"; }
function polo { cd "$marco"; }

Difficult part

So, I tried to do some aliasing and my original solution was A LOT uglier than the what you can I share above. I ended up getting stuck because a broken alias I wrote was stopping me from calling the marco and polo functions once the script containing them was executing.

After a lot of banging my head I finally figured it out and boy was I thankful.

My First custom shell script

Since I am trying to write blog posts for everyday in this challenge I thought I should have an organized file structure that would help support my success in this. Well, 100 folders in a specific structure seems like the perfect test drive for my shell scripting skills. It went well in my opinion but here’s the code:

#!/bin/bash

function mkfolders {
    local day=1
    while [ $day -le 100 ]
    do
        local folder=$(mkdir "day_${day}_post")
        
        touch "${folder}/day_${day}_post.md"
        day=$(( $day + 1 ))
    done
}

mkfolders

I ended up with some errors because the script did not have write permissions to create the files in each folder. Btw, I am doing all of this with the git bash program on a Windows operating system. I don’t know if that is blasphemous but here I am lol.

SQL Review

I have been tracking my habits for the past month or so:

  • Water intake (hopefully calories soon)
  • Physical activity (rough estimates)
  • Amount of time spent coding

I made a mistake and added two entries instead of one so I wanted to reset the auto-incrementor for the row ids. Anyways here’s the code I used.

# How to view all of your sequences in a Database
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';

# Me altering one of my sequences to reset it values by one back (I hope it's correct lol)
ALTER SEQUENCE exercise_id_seq RESTART WITH 46;

Conclusion

That’s all for today. I hope it’s not too much. Btw if you are interested in the MIT course you can check out the video lecture I’m currently doing the exercises for. The lecture is helpful but isn’t sufficient by itself.  Until next time PEACE!

YouTube player