Junedays scripting guidelines

From Juneday education
Jump to: navigation, search

Environment variables and similar

$HOME vs ~

Commands

echo

What's the path to echo? Which gives us:

  • cygwin: /usr/bin/echo
  • MacOS: /bin/echo
  • Ubuntu & Fedora: /usr/bin/echo

On Cygwin, Ubuntu and Fedora there are hard links (either from or to) so /bin/echo works there as well.

Sometimes the build in echo (in bash) works fine. But in some cases

Solution

  1. if you don't need any fancy features of echo use echo and don't bother if it is the built in version of echo, BSD's echo or GNU coreutils' echo
  2. if you need features that only exists in GNU's coreutils package:
    1. source the resource file (.junedayrc) which you can read about below and use echo which is aliased to bash's builtin echo

awk

On cygwin awk seems to be malfunctioning for some reason.

Solution

Use the awk as set up by the Juneday resource file

  1. source the resource file (.junedayrc) which you can read about below and use awk which is then aliased to GNU's coreutils gawk

date

On MacOS the date command use is BSD's which differs from GNU coreutils' version. We need one and the same version in some scripts.

Solution

Use the date as set up by the Juneday resource file

  1. source the resource file (.junedayrc) which you can read about below and use date which is then aliased to GNU's coreutils date

junedayrc

Requirements

To use this you need to have curlinstalled.

We provide a bash resource file that you can use. We suggest you do any of the two below:

Complete solution

If you want to download the resource file if it is missing, you can use the following. Add the following to your script (just after #!/bin/bash):

JUNEDAY_RC=~/.junedayrc
if [ ! -f ${JUNEDAY_RC} ]
then
    echo "Downloading resource file for Juneday's educational material"
    curl -o ${JUNEDAY_RC} https://raw.githubusercontent.com/progund/utils/master/etc/junedayrc
    if [ $(grep "determine_os" ${JUNEDAY_RC} | wc -l) -eq 0 ]
    then
        echo "Failed to download Juneday's resource file"
        echo ".. cowardly bailing out."
        rm -f ${JUNEDAY_RC}
        exit 1
    fi
fi
. ${JUNEDAY_RC}

Simple solution

If you don't want to download the resource file and simply bail out if missing, add the following to your script (just after #!/bin/bash):

JUNEDAY_RC=~/.junedayrc
if [ ! -f ${JUNEDAY_RC} ]
then
  echo "Can't find Juneday's resource file: ${JUNEDAY_RC}"
  echo ".. cowardly bailing out."
  exit 1
fi
. ${JUNEDAY_RC}