14. Bash#

A bash script can combine any number of tasks or even more complex operations, and when executed, it will run each command in the specified order. This allows for automation and streamlining of tasks that might be time-consuming or error-prone if performed manually.

One example here describes how the beta-version of this platform was folked & cloned. The script is available:

# Folked from jhutrc/beta
git clone https://github.com/jhufena/home
cp -r home/* our
jb build our
cp -r our/* home
cd home
git add .
git commit -m "jhutrc/beta -> folks jhufena/home -> clone and copy -> our/home"

# overcoming the error: no sufficient permissions
git remote -v
git remote set-url origin "git@github.com:jhufena/home"
git config user.name "jhufena"
git config user.email "jhufena@gmail.com"
git checkout main

# Generate a new SSH key
rm -rf ~/.ssh/id_ourhome*
ssh-keygen -t ed25519 -C "jhufena@gmail.com" -f ~/.ssh/id_ourhome
cat ~/.ssh/id_ourhome.pub
echo "Please manually add the above SSH public key to your GitHub account's SSH keys."
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ourhome
chmod 600 ~/.ssh/id_ourhome

# Configure the remote URL with SSH
git remote set-url origin "git@github.com:jhufena/home"

# Push changes
git push -u origin main
ghp-import -n -p -f _build/html
rm -rf home
echo "jb content updated & pushed to jhufena/home repository!"
Generalizing .sh script and thereby segue to automation on a Unix terminal

Certainly, you can generalize the given script to make it more adaptable. Here’s how you could do that using Bash scripting variables and parameterization:

#!/bin/bash

# Parameters required by the script
read -p "Enter your GitHub username (e.g., abikesa): " GITHUB_USER
read -p "Enter your GitHub repository name (e.g., nabongo): " GITHUB_REPO
read -p "Enter your email address (e.g., abikesa.sh@gmail.com): " EMAIL_ADDRESS
read -p "Enter your root directory (e.g., ~/Dropbox/1f.ἡἔρις,κ/1.ontology): " ROOT_DIR
read -p "Enter the name of the local directory to be created within the root directory (e.g., charles): " LOCAL_DIR
read -p "Enter the SSH key name (e.g., id_charlesnabongo): " SSH_KEYNAME
read -p "Enter your git commit message (e.g., automated abikesa_fromfolks.sh script): " GIT_COMMIT_MESSAGE

# Folked from some_repo/beta
cd $(eval echo $ROOT_DIR)
git clone "https://github.com/$GITHUB_USER/$GITHUB_REPO"
cp -r "$GITHUB_REPO/*" "$LOCAL_DIR"
jb build "$LOCAL_DIR"
cp -r "$LOCAL_DIR/*" "$GITHUB_REPO"
cd "$GITHUB_REPO"
git add .
git commit -m "$GIT_COMMIT_MESSAGE"

# Overcoming the error: no sufficient permissions
git remote -v
git remote set-url origin "git@github.com:$GITHUB_USER/$GITHUB_REPO"
git config user.name "$GITHUB_USER"
git config user.email "$EMAIL_ADDRESS"
git checkout main

# Check if SSH keys already exist, and if not, generate a new one
read -p "Enter your SSH key name (e.g., id_charlesnabongo, not ~/.ssh/id_charlesnabongo): " SSH_KEYNAME
SSH_KEYPATH="$HOME/.ssh/$SSH_KEYNAME"

if [ ! -f "$SSH_KEYPATH" ]; then
  ssh-keygen -t ed25519 -C "$EMAIL_ADDRESS" -f $SSH_KEYPATH
fi

cat "$SSH_KEYPATH.pub"
echo "Please manually add the above SSH public key to your GitHub account's SSH keys."
read -p "Once you have added the SSH key to your GitHub account, press Enter to continue..."
eval "$(ssh-agent -s)"
ssh-add $SSH_KEYPATH
chmod 600 $SSH_KEYPATH

# Configure the remote URL with SSH
git remote set-url origin "git@github.com:$GITHUB_USER/$GITHUB_REPO"

# Push changes
git push -u origin main
ghp-import -n -p -f _build/html
rm -rf "$GITHUB_REPO"
echo "jb content updated & pushed to $GITHUB_USER/$GITHUB_REPO repository!"

Usage example:

./your_script.sh jhufena home our "commit message" id_ourhome jhufena@gmail.com

Here are what the parameters are supposed to be:

  1. GITHUB_USER: GitHub username

  2. GITHUB_REPO: GitHub repository name

  3. LOCAL_DIR: Local directory name where the repo is copied

  4. COMMIT_MSG: Commit message

  5. SSH_KEYNAME: SSH key name

  6. EMAIL: Email associated with the GitHub account

Make sure to provide executable permissions to your script:

chmod +x your_script.sh

This makes the script more reusable and adaptable to different scenarios.

Another example of a `bash` script that will run a `Stata` do-file (see previous section for more):
(myenv) d@Poseidon 1.ontology % ls -l
total 376
-rw-r--r--@   1 d  staff   2983 Aug 23 19:18 README.md
drwxr-xr-x@  13 d  staff    416 Aug 23 13:25 alpha
drwxr-xr-x@  35 d  staff   1120 Aug 23 18:22 bloc
drwxr-xr-x@   9 d  staff    288 Aug 21 12:45 cst
drwxr-xr-x@  16 d  staff    512 Aug 23 17:07 git
drwxr-xr-x@  17 d  staff    544 Aug  7 22:35 git-filter-repo
drwxr-xr-x@   7 d  staff    224 Aug  6 07:33 myenv
drwxr-xr-x@  13 d  staff    416 Aug 14 18:53 nh_projectbeta
-rw-r--r--@   1 d  staff  77753 Aug 23 12:44 particle_animation.gif
-rw-r--r--@   1 d  staff    463 Aug 23 12:43 populate_be.ipynb
-rw-r--r--@   1 d  staff  56185 Aug 23 19:17 python.ipynb
-rw-r--r--@   1 d  staff  34173 Aug 23 12:47 r.ipynb
drwxr-xr-x@  13 d  staff    416 Aug 15 02:12 seasons_projectalpha
drwxr-xr-x@  13 d  staff    416 Aug 17 14:05 sibs
-rw-r--r--@   1 d  staff    355 Aug 23 19:16 stata.do
-rw-r--r--@   1 d  staff   1196 Aug 23 18:43 stata.dta
-rw-r--r--@   1 d  staff   2022 Aug 23 19:20 stata.log
drwxr-xr-x@ 139 d  staff   4448 Jun 25 08:29 summer
drwxr-xr-x@  25 d  staff    800 Jul 20 20:21 verano
drwxr-xr-x@  10 d  staff    320 Aug 21 20:03 yafe
(myenv) d@Poseidon 1.ontology % stata-se -b do stata.do
(myenv) d@Poseidon 1.ontology % cat stata.log

  ___  ____  ____  ____  ____ ®
 /__    /   ____/   /   ____/      18.0
___/   /   /___/   /   /___/       SE—Standard Edition

 Statistics and Data Science       Copyright 1985-2023 StataCorp LLC
                                   StataCorp
                                   4905 Lakeway Drive
                                   College Station, Texas 77845 USA
                                   800-STATA-PC        https://www.stata.com
                                   979-696-4600        stata@stata.com

Stata license: Single-user , expiring 27 Mar 2024
Serial number: 401809200438
  Licensed to: Poseidon
               Johns Hopkins University

Notes:
      1. Stata is running in batch mode.
      2. Unicode is supported; see help unicode_advice.
      3. Maximum number of variables is set to 5,000 but can be increased;
          see help set_maxvar.

. do stata.do 

. if 0 { 
.     
.     export PATH=$PATH:/applications/stata/statase.app/contents/macos/
.     stata-se -b do stata.do
. 
. }

. 
. webuse auto, clear 
(1978 automobile data)

. l make price mpg foreign in 1/9

     +-----------------------------------------+
     | make             price   mpg    foreign |
     |-----------------------------------------|
  1. | AMC Concord      4,099    22   Domestic |
  2. | AMC Pacer        4,749    17   Domestic |
  3. | AMC Spirit       3,799    22   Domestic |
  4. | Buick Century    4,816    20   Domestic |
  5. | Buick Electra    7,827    15   Domestic |
     |-----------------------------------------|
  6. | Buick LeSabre    5,788    18   Domestic |
  7. | Buick Opel       4,453    26   Domestic |
  8. | Buick Regal      5,189    20   Domestic |
  9. | Buick Riviera   10,372    16   Domestic |
     +-----------------------------------------+

. di "`c(N)' obs & `c(k)' vars"
74 obs & 12 vars

. 
end of do-file
(myenv) d@Poseidon 1.ontology %