Linux Shell and Bash: Hacks

What I learned about using Linux Shell and Bash to install tools and set up a repository on my local machine.

Note: I am not including ‘echo’ commands present in teacher copy because I commment out that information instead

Project Setup

Pull code

Pull code to your local machine using an automated process to speed things up, including some conditional logic ensuring that things only download if they do not already exist.

Commonly Used Commands:

cd: change directory

%%script bash

# define variables used in all other code cells
cat <<EOF > /tmp/variables.sh
export project_dir=$HOME/vscode
export project=\$project_dir/teacher
export project_repo="https://github.com/nighthawkcoders/teacher.git" 
export posts=vscode/student/_posts
EOF

%%script bash 
# used to begin every bash script

# extract saved variables from a preexisting script to save time
source /tmp/variables.sh

# begin in home directory
cd ~

# use a conditional block to create a new project directory
# if directory does not exist:
if [ ! -d $project_dir ] # ! means 'does not'
# then make a directory
then
    mkdir -p $project_dir
fi

# a second block that runs on the same conditions but has a separate function
if [ ! -d $project ]
then
    cd $project_dir
    git clone $project_repo # clones the project to the home directory!
    cd ~
fi

View all files in any given GitHub project

Print a working directory of a project with a simple script

Commonly Used Commands:

ls: list

pwd: print working directory

cd $project: move inside a given project’s directory

%%script bash

source /tmp/variables.sh

cd $project # changes directory to given project name
pwd

ls # lists all files
/Users/maryamabdul-aziz/vscode/teacher
Gemfile
Gemfile.lock
LICENSE
Makefile
README.md
_config.yml
_data
_includes
_layouts
_notebooks
_posts
_site
assets
csa.md
csp.md
csse.md
images
index.md
indexBlogs.md
scripts

Look at list of hidden and long attributes

Some files will be hidden from lists because they contain content that the average user will not engage with, like configuration or settings. Using bash scripts, we can view those files and their attributes

Commonly Used Commands:

ls -a: lists all hidden files

ls -l: lists files with long format

%%script bash
# List hidden files in long format:

source /tmp/variables.sh

cd ~/$project
pwd

# lists all hidden files in long format
ls -al # notice how two commands were used together
bash: line 5: cd: /Users/maryamabdul-aziz//Users/maryamabdul-aziz/vscode/teacher: No such file or directory


/Users/maryamabdul-aziz/vscode/student/_notebooks
total 144
drwxr-xr-x   8 maryamabdul-aziz  staff    256 Sep  6 23:19 .
drwxr-xr-x  24 maryamabdul-aziz  staff    768 Sep  4 23:37 ..
-rw-r--r--@  1 maryamabdul-aziz  staff   6148 Sep  3 22:28 .DS_Store
-rw-r--r--   1 maryamabdul-aziz  staff   1388 Sep  6 23:14 2023-08-30-Hacks-wk-0.ipynb
-rw-r--r--   1 maryamabdul-aziz  staff   5669 Sep  5 23:24 2023-08-30-Hacks-wk-1.ipynb
-rw-r--r--   1 maryamabdul-aziz  staff  21980 Sep  6 23:04 2023-08-31-Hacks-wk-2.ipynb
-rw-r--r--   1 maryamabdul-aziz  staff  18905 Sep  6 23:13 2023-09-05-Hacks-wk-3.ipynb
-rw-r--r--   1 maryamabdul-aziz  staff   6677 Sep  7 12:58 2023-09-06-Pair-Showcase-Linux.ipynb
%%script bash
# List files inside of a given folder in a  project


source /tmp/variables.sh

# looks at _posts inside project. 'posts' can be replaced with any folder within the project
%cd $posts
pwd

# lists files in long format
ls -l
bash: line 7: fg: no job control


/Users/maryamabdul-aziz/vscode/student/_notebooks
total 128
-rw-r--r--  1 maryamabdul-aziz  staff   1388 Sep  6 23:14 2023-08-30-Hacks-wk-0.ipynb
-rw-r--r--  1 maryamabdul-aziz  staff   5669 Sep  5 23:24 2023-08-30-Hacks-wk-1.ipynb
-rw-r--r--  1 maryamabdul-aziz  staff  21980 Sep  6 23:04 2023-08-31-Hacks-wk-2.ipynb
-rw-r--r--  1 maryamabdul-aziz  staff  18905 Sep  6 23:13 2023-09-05-Hacks-wk-3.ipynb
-rw-r--r--  1 maryamabdul-aziz  staff   6677 Sep  7 12:58 2023-09-06-Pair-Showcase-Linux.ipynb

Read data from a Markdown file

Commonly Used Commands:

cat: reads data from a file and gives its contents as its output

%%script bash

source /tmp/variables.sh

cd ~/vscode/student

# show contents of file
cat README.md  
# 'README.md' can be exchanged for any file name
## Blog site using GitHub Pages and Jekyll
> This site is intended for Students.   This is to record plans, complete hacks, and do work for your learnings.
- This can be customized to support computer science as you work through pathway (JavaScript, Python/Flask, Java/Spring)
- All tangible artifact work is in a _posts or in a _notebooks.  
- Front matter (aka meta data) in ipynb and md files is used to organize information according to week and column in running web site.

## GitHub Pages
All `GitHub Pages` websites are managed on GitHub infrastructure. GitHub uses `Jekyll` to tranform your content into static websites and blogs. Each time we change files in GitHub it initiates a GitHub Action that rebuilds and publishes the site with Jekyll.  
- GitHub Pages is powered by: [Jekyll](https://jekyllrb.com/).
- Publised teacher website: [nighthawkcoders.github.io/teacher](https://nighthawkcoders.github.io/teacher/)

## Preparing a Preview Site 
In all development, it is recommended to test your code before deployment.  The GitHub Pages development process is optimized by testing your development on your local machine, prior to files on GitHub

Development Cycle. For GitHub pages, the tooling described below will create a development cycle  `make-code-save-preview`.  In the development cycle, it is a requirement to preview work locally, prior to doing a VSCode `commit` to git.

Deployment Cycle.  In the deplopyment cycle, `sync-github-action-review`, it is a requirement to complete the development cycle prior to doing a VSCode `sync`.  The sync triggers github repository update.  The action starts the jekyll build to publish the website.  Any step can have errors and will require you to do a review.

### WSL and/or Ubuntu installation requirements
- The result of these step is Ubuntu tools to run preview server.  These procedures were created using [jekyllrb.com](https://jekyllrb.com/docs/installation/ubuntu/)
- Run scripts in scripts directory of teacher repo: setup_ubuntu.sh and activate.sh.  Or, follow commands below.
```bash
## WSL/Ubuntu commands
# sudo apt install, installs packages for Ubuntu
echo "=== Ugrade Packages ==="
sudo apt update
sudo apt upgrade -y
#
echo "=== Install Ruby ==="
sudo apt install -y ruby-full build-essential zlib1g-dev
# 
echo "=== Install Python ==="
sudo apt-get install -y python3 python3-pip python-is-python3
#    
echo "=== Install Jupyter Notebook ==="
sudo apt-get install -y jupyter-notebook

# bash commands, install user requirements.
echo "=== GitHub pages build tools  ==="
export GEM_HOME="$HOME/gems"
export PATH="$HOME/gems/bin:$PATH"
echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
echo 'export GEM_HOME="$HOME/gems"' >> ~/.bashrc
echo 'export PATH="$HOME/gems/bin:$PATH"' >> ~/.bashrc
echo "=== Gem install starting, thinking... ==="
gem install jekyll bundler
head -30 ./teacher/scripts/activate.sh
echo "=== !!!Start a new Terminal!!! ==="
```

### MacOs installation requirements 
- Ihe result of these step are MacOS tools to run preview server.  These procedures were created using [jekyllrb.com](https://jekyllrb.com/docs/installation/macos/). Run scripts in scripts directory of teacher repo: setup_macos.sh and activate_macos.sh.  Or, follow commands below.
```bash
# MacOS commands
# brew install, installs packages for MacOS
echo "=== Ugrade Packages ==="
brew update
brew upgrade
#
echo "=== Install Ruby ==="
brew install chruby ruby-install xz
ruby-install ruby 3.1.3
#
echo "=== Install Python ==="
brew install python
#    
echo "=== Install Jupyter Notebook ==="
brew install jupyter

# bash commands, install user requirements.
export GEM_HOME="$HOME/gems"
export PATH="$HOME/gems/bin:$PATH"
echo '# Install Ruby Gems to ~/gems' >> ~/.zshrc
echo 'export GEM_HOME="$HOME/gems"' >> ~/.zshrc
echo 'export PATH="$HOME/gems/bin:$PATH"' >> ~/.zshrc
echo "=== Gem install starting, thinking... ==="
gem install jekyll bundler
head -30 ./teacher/scripts/activate.sh
echo "=== !!!Start a new Terminal!!! ==="
```

### Preview
- The result of these step is server running on: http://0.0.0.0:4100/teacher/.  Regeneration messages will run in terminal on any save.  Press the Enter or Return key in the terminal at any time to enter commands.

- Complete installation
```bash
bundle install
```
- Run Server.  This requires running terminal commands `make`, `make stop`, `make clean`, or `make convert` to manage the running server.  Logging of details will appear in terminal.   A `Makefile` has been created in project to support commands and start processes.

    - Start preview server in terminal
    ```bash
    cd ~/vscode/teacher  # my project location, adapt as necessary
    make


    ```

    - Terminal output of shows server address. Cmd or Ctl click http location to open preview server in browser. Example Server address message... 
    ```
    Server address: http://0.0.0.0:4100/teacher/
    ```

    - Save on ipynb or md activiates "regeneration". Refresh browser to see updates. Example terminal message...
    ```
    Regenerating: 1 file(s) changed at 2023-07-31 06:54:32
        _notebooks/2024-01-04-cockpit-setup.ipynb
    ```

    - Terminal message are generated from background processes.  Click return or enter to obtain prompt and use terminal as needed for other tasks.  Alway return to root of project `cd ~/vscode/teacher` for all "make" actions. 
        

    - Stop preview server, but leave constructed files in project for your review.
    ```bash
    make stop
    ```

    - Stop server and "clean" constructed files, best choice when renaming files to eliminate potential duplicates in constructed files.
    ```bash
    make clean
    ```

    - Test notebook conversions, best choice to see if IPYNB conversion is acting up.
    ```bash
    make convert
    ```

Env, Git, and Github

Note: I don’t fully understand ‘env’ quite yet, but after reading its output, I have some understanding of what it’s meant for

Commonly Used Commands:

env: shows settings for shell

git clone: sets up directory of files on local machine

.git: hidden directory that establishes the relationship between the machine and GitHub’s git server

%%script bash

# While I do not fully understand what 'env' accomplishes, I recognize some of the information that it outputs
env
VSCODE_CRASH_REPORTER_PROCESS_TYPE=extensionHost
GEM_HOME=/Users/maryamabdul-aziz/gems
TERM=xterm-color
SHELL=/bin/zsh
CLICOLOR=1
TMPDIR=/var/folders/mq/kcs_jpf56bz_c4y467zjlml00000gp/T/
PYTHONUNBUFFERED=1
ORIGINAL_XDG_CURRENT_DESKTOP=undefined
MallocNanoZone=0
PYDEVD_USE_FRAME_EVAL=NO
PYTHONIOENCODING=utf-8
USER=maryamabdul-aziz
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.LGFPn3G97r/Listeners
__CF_USER_TEXT_ENCODING=0x1F6:0x0:0x0
PAGER=cat
ELECTRON_RUN_AS_NODE=1
VSCODE_AMD_ENTRYPOINT=vs/workbench/api/node/extensionHostProcess
PATH=/usr/local/bin:/Users/maryamabdul-aziz/Library/Python/3.11/bin:/Users/maryamabdul-aziz/.rbenv/shims:/Users/maryamabdul-aziz/gems/bin:/Users/maryamabdul-aziz/.rbenv/shims:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/maryamabdul-aziz/.rbenv/shims:/Users/maryamabdul-aziz/gems/bin:/Users/maryamabdul-aziz/.rbenv/shims:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
_=/usr/bin/env
__CFBundleIdentifier=com.microsoft.VSCode
PWD=/Users/maryamabdul-aziz/vscode/student/_notebooks
VSCODE_HANDLES_UNCAUGHT_ERRORS=true
MPLBACKEND=module://matplotlib_inline.backend_inline
XPC_FLAGS=0x0
FORCE_COLOR=1
RBENV_SHELL=zsh
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/maryamabdul-aziz
VSCODE_NLS_CONFIG={"locale":"en-us","osLocale":"en-us","availableLanguages":{},"_languagePackSupport":true}


PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING=1
LOGNAME=maryamabdul-aziz
LC_CTYPE=UTF-8
VSCODE_IPC_HOOK=/Users/maryamabdul-aziz/Library/Application Support/Code/1.81-main.sock
VSCODE_CODE_CACHE_PATH=/Users/maryamabdul-aziz/Library/Application Support/Code/CachedData/6c3e3dba23e8fadc360aed75ce363ba185c49794
CLICOLOR_FORCE=1
VSCODE_PID=1551
GIT_PAGER=cat
VSCODE_CWD=/
%%script bash

source /tmp/variables.sh

cd $project

# navigate to hidden directory and print list of files in long form
cd ~/vscode/student
ls -l

# unsure of the function of the next line of code, so I've commented it out
# cat config

total 96
-rw-r--r--   1 maryamabdul-aziz  staff   122 Aug 28 13:30 Gemfile
-rw-r--r--   1 maryamabdul-aziz  staff  7348 Aug 28 13:38 Gemfile.lock
-rw-r--r--   1 maryamabdul-aziz  staff  1081 Aug 28 13:30 LICENSE
-rw-r--r--   1 maryamabdul-aziz  staff  3120 Aug 28 13:30 Makefile
-rw-r--r--   1 maryamabdul-aziz  staff  5798 Aug 28 13:30 README.md
-rw-r--r--   1 maryamabdul-aziz  staff   451 Aug 30 13:55 _config.yml
drwxr-xr-x   3 maryamabdul-aziz  staff    96 Aug 29 13:42 _data
drwxr-xr-x  10 maryamabdul-aziz  staff   320 Aug 28 13:30 _includes
drwxr-xr-x   7 maryamabdul-aziz  staff   224 Aug 28 13:30 _layouts
drwxr-xr-x   8 maryamabdul-aziz  staff   256 Sep  6 23:19 _notebooks
drwxr-xr-x  19 maryamabdul-aziz  staff   608 Sep  6 23:19 _posts
drwxr-xr-x  15 maryamabdul-aziz  staff   480 Sep  6 23:19 _site
-rw-r--r--   1 maryamabdul-aziz  staff   115 Aug 29 13:25 compsci.md
drwxr-xr-x  14 maryamabdul-aziz  staff   448 Sep  5 13:40 images
-rw-------   1 maryamabdul-aziz  staff  1169 Aug 28 13:33 index.md
-rw-r--r--   1 maryamabdul-aziz  staff    53 Aug 28 13:30 indexBlogs.md
-rw-r--r--   1 maryamabdul-aziz  staff    50 Aug 28 13:30 indexHelp.md
drwxr-xr-x   5 maryamabdul-aziz  staff   160 Aug 28 13:30 scripts