Script Execution

  • BASH script for making a project directory, files …
  • Download: makeproject.sh
Basic structure:

  |-- /mnt/Vancouver/projects
      |-- "project"/      ## project name, e.g.: info_extraction
          |-- README.md   ## project readme
          |-- setup.py
          |-- bak/
          |-- bin/        ## binaries (GitHub ...)
          |-- data/
          |-- docs/       ## external-sourced readme files, PDF, web pages ...
          |-- logs/
          |-- src/        ## my scripts
          |-- tmp/

[victoria@victoria]$ date; pwd

  Mon Apr  8 09:03:07 PDT 2019
  /mnt/Vancouver/projects/ie

## mkproj | makeproj | makeproject: ~/.bashrc alias for
## /mnt/Vancouver/programming/scripts/makeproject.sh

[victoria@victoria]$ mkproj info_extraction

	            USAGE: makeproject <projdir>  ## e.g. info_extraction
	Current directory: /mnt/Vancouver/projects/ie
	      Project dir: /mnt/Vancouver/projects/ie/info_extraction

[victoria@victoria]$ pwd; ls -l | grep info_

  /mnt/Vancouver/projects/ie
  drwxr-xr-x 9 victoria victoria  4096 Apr  8 10:46 info_extraction

[victoria@victoria]$ tree -L 3 -F info_extraction

  info_extraction
  ├── bak/
  ├── bin/
  ├── data/
  │   ├── input/
  │   ├── output/
  │   ├── processed/
  │   ├── raw/
  │   └── test/
  │       ├── input/
  │       └── output/
  ├── docs/
  ├── logs/
  ├── README2.md
  ├── README.md
  ├── src/
  │   ├── bash/
  │   │   ├── download.sh*
  │   │   └── main.sh*
  │   ├── models/
  │   │   └── results/
  │   └── python3/
  │       └── project.py*
  └── tmp/

  18 directories, 5 files

[victoria@victoria]$ 

makeproject.sh

#!/bin/bash
# vim: set filetype=sh :
# vim: syntax=sh

# https://web.archive.org/web/20110314180918/http://www.davidpashley.com/articles/writing-robust-shell-scripts.html
# https://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error/2871034#2871034
set -e

## chmod 755 makeproject.sh
# file: /mnt/Vancouver/programming/scripts/makeproject.sh                       
# ~/.bashrc aliases: mkproj | makeproj | makeproject
## Usage: mkproj <projdir>
## e.g. makeproject.sh info_extraction [via alias: makeproject info_extraction]

printf '\t            USAGE: makeproject <projdir>  ## e.g. makeproject info_extraction\n'

BASEDIR=$(pwd)
printf '\tCurrent directory: '; echo $BASEDIR

PROJDIR=$1
printf '\t      Project dir: '; echo $BASEDIR/$PROJDIR
# https://unix.stackexchange.com/questions/228331/avoid-running-the-script-if-a-variable-is-not-defined
# This must appear after the external variable definition:
if [[ -z "$PROJDIR" ]]; then
  printf "\n\tERR: Must provide external variable (project directory)\n\tUsage: makeproject <projdir>\n\tExample: makeproj info_extraction\n\n" 1>&2
  exit 1
fi

## https://stackoverflow.com/questions/793858/how-to-mkdir-only-if-a-dir-does-not-already-exist
## likewise, `touch` does not overwrite an existing file
mkdir -p $BASEDIR/$PROJDIR

touch $BASEDIR/$PROJDIR/README.md
touch $BASEDIR/$PROJDIR/README2.md
mkdir -p $BASEDIR/$PROJDIR/bak/
mkdir -p $BASEDIR/$PROJDIR/bin/
mkdir -p $BASEDIR/$PROJDIR/data/
mkdir -p $BASEDIR/$PROJDIR/data/input/
mkdir -p $BASEDIR/$PROJDIR/data/output/
mkdir -p $BASEDIR/$PROJDIR/data/processed/
mkdir -p $BASEDIR/$PROJDIR/data/raw/
mkdir -p $BASEDIR/$PROJDIR/data/test/
mkdir -p $BASEDIR/$PROJDIR/data/test/input/
mkdir -p $BASEDIR/$PROJDIR/data/test/output/
mkdir -p $BASEDIR/$PROJDIR/docs/
mkdir -p $BASEDIR/$PROJDIR/logs/
mkdir -p $BASEDIR/$PROJDIR/src/
mkdir -p $BASEDIR/$PROJDIR/src/bash/
touch $BASEDIR/$PROJDIR/src/bash/download.sh ; chmod 755 $BASEDIR/$PROJDIR/src/bash/download.sh
touch $BASEDIR/$PROJDIR/src/bash/main.sh ; chmod 755 $BASEDIR/$PROJDIR/src/bash/main.sh
mkdir -p $BASEDIR/$PROJDIR/src/models/
mkdir -p $BASEDIR/$PROJDIR/src/models//results/
mkdir -p $BASEDIR/$PROJDIR/src/python3/
touch $BASEDIR/$PROJDIR/src/python3/project.py ; chmod 755 $BASEDIR/$PROJDIR/src/python3/project.py
mkdir -p $BASEDIR/$PROJDIR/tmp/

echo

Comments

  • Reserve projects for actual projects not languages. E.g., info_extraction project may use Python, bash scripts, nltk, tf-idf, perl, sed, …
  • Place system-wide (non-project) programming languages in: /mnt/Vancouver/programming/
  • Place system-wide (non-project) binaries (e.g. GitHub) in: /mnt/Vancouver/apps/
  • Place system-wide (non-project) scripts in: /mnt/Vancouver/programming/scripts/

  • Don’t use Jupyter notebooks: the short-term conveniences are offset by buggy fragmented code, interrupted logic, and poor programming practice.

    I edit plain text scripts in Neovim, and execute those scripts in a tabbed xfce4-terminal. … Use (Python) virtual environments …


References