Live data from Hacker News

Ask HN: What are some small scripts you use daily?

news.ycombinator.com

1–10 of 59 posts

Re: Ask HN: What are some small scripts you use daily?

#3
post #2

I have a shell alias that creates a temp directory labeled with the current time stamp and suffixed tag then changes to it. Makes it very quick get a new scratch area for shell fu. Plus all the scratch dirs are in one place so cleanup is a breeze to free up space.

Sharing is caring? :)

Re: Ask HN: What are some small scripts you use daily?

#4
I have letters like r and b aliased in my bash profile to check for and run a bash script, if it exists, in each project directory (r = ./run.sh, b = ./build.sh).

In each of those scripts, I typically have a one liner depending on what the project requires. A simple build one is:

    #!/usr/bin/env bash

    make build
And run:

    #!/usr/bin/env bash

    docker run foo/bar
Or maybe:

    #!/usr/bin/env bash

    python manage.py runserver
I might also add (source) environment variable settings, etc. Sort of like my own personal decentralized makefile.

Then I add each script to my .git/info/exclude for each project. It saves so much time switching between projects to not have to remember any particular one's build or run commands.

Re: Ask HN: What are some small scripts you use daily?

#5
post #3
post #2

I have a shell alias that creates a temp directory labeled with the current time stamp and suffixed tag then changes to it. Makes it very quick get a new scratch area for shell fu. Plus all the scratch dirs are in one place so cleanup is a breeze to free up space.

Sharing is caring? :)

Ask and ye shall receive!

    make-scratch-dir () {
      local name="$1"
      local pattern='^[a-z0-9\\-]+$'
      if [[ "$#" != 1 ]]; then
        echo "Usage: make-scratch-dir " 1>&2
        return 1
      elif [[ ! "$name" =~ $pattern ]]; then
        echo "Invalid name: ${name}"
      fi
      local full_path="$HOME/tmp/scratch/$(date +%Y%m%d-%H%M%S)-${name}"
      mkdir -p "${full_path}"
      pushd "${full_path}"
      echo "Now in temp dir: ${full_path}"
    }
The directory change is done via pushd so you can hop back via popd. Also, it restricts the suffixes to ensure the directories are all "simple" names.

Re: Ask HN: What are some small scripts you use daily?

#9
This Bash function rebases and pushes all my feature branches on the upstream "develop" branch:

  rebase-all () 
  { 
      old=`git rev-parse --abbrev-ref HEAD`;
      stashed=`git stash`;
      for b in $(git branch --format '%(authorname) %(refname:short)' | sed -ne "s/^`git config --get user.name` //p" | grep -- -);
      do
          git checkout $b && git rebase origin/develop && git push --force || ( git rebase --abort && echo Could not rebase $b );
          echo;
      done;
      git checkout $old;
      if [ "$stashed" != "No local changes to save" ]; then
          git stash pop;
      fi
  }
Post reply on HN