Bash Tutorial

A brief overview of Bash, on your way to becoming a Linux expert. When a computer boots up, a kernel (MacOS, Windows, Linux) is started. This kernel provides a shell, or terminal, that allows user to interact with a most basic set of commands. Typically, the casual user will not interact with the shell/terminal as a Desktop User Interface is started by the computer boot up process. To activate a shell directly, users will run a “terminal” through the Desktop. VS Code provides ability to activate "terminal" while in the IDE.

Variable Prerequisites

Setup bash shell dependency variables for this page. Variables are one of the first aspects of programming. Variables have "name" and a "value".

  • Hack Note: Change variables to match your student project.

Define variable

The following code cell defines 3 variables and assigns each a value. There are some extra command, called a HERE document, that write these variables to a file. This is so we can use these variables over and over below.

%%script bash

# Dependency Variables, set to match your project directories

cat <<EOF > /tmp/variables.sh
export project_dir=$HOME/vscode  # change vscode to different name to test git clone
export project=\$project_dir/emaad-github-pages1  # change teacher to name of project from git clone
export project_repo="https://github.com/Emaad-Mir/emaad-github-pages1.git"  # change to project of choice
EOF

Output the value of a variable

The following code cell outputs the value of the variables, using the echo command. For visual understanding in the output, each echo command provide a title before the $variable

%%script bash

# Extract saved variables
source /tmp/variables.sh

# Output shown title and value variables
echo "Project dir: $project_dir"
echo "Project: $project"
echo "Repo: $project_repo"
Project dir: /home/emaad/vscode
Project: /home/emaad/vscode/emaad-github-pages1
Repo: https://github.com/Emaad-Mir/emaad-github-pages1.git

Project Setup and Analysis with Bash Scripts

The bash scripts that follow automate what was done in the setup procedures. The purpose of this is to show that many of the commands we performed can be added to a script, then performed automatically.

Pull Code

Pull code from GitHub to your machine. This is a bash script, a sequence of commands, that will create a project directory and add the “project” from GitHub to the vscode directory. There is conditional logic to make sure that clone only happen if it does not (!) exist. Here are some key elements in this code…

  • cd command (change directory), remember this from terminal session
  • if statements (conditional statement, called selection statement by College Board), code inside only happens if condition is met
%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Using conditional statement to create a project directory and project"

cd ~    # start in home directory

# Conditional block to make a project directory
if [ ! -d $project_dir ]
then 
    echo "Directory $project_dir does not exists... makinng directory $project_dir"
    mkdir -p $project_dir
fi
echo "Directory $project_dir exists." 

# Conditional block to git clone a project from project_repo
if [ ! -d $project ]
then
    echo "Directory $project does not exists... cloning $project_repo"
    cd $project_dir
    git clone $project_repo
    cd ~
fi
echo "Directory $project exists." 
Using conditional statement to create a project directory and project
Directory /home/emaad/vscode exists.
Directory /home/emaad/vscode/emaad-github-pages1 exists.

Look at files Github project

All computers contain files and directories. The clone brought more files from cloud to your machine. Review the bash shell script, observe the commands that show and interact with files and directories. These were used during setup.

  • “ls” lists computer files in Unix and Unix-like operating systems
  • “cd” offers way to navigate and change working directory
  • “pwd” print working directory
  • “echo” used to display line of text/string that are passed as an argument
%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Navigate to project, then navigate to area wwhere files were cloned"
cd $project
pwd

echo ""
echo "list top level or root of files with project pulled from github"
ls

Navigate to project, then navigate to area wwhere files were cloned
/home/emaad/vscode/emaad-github-pages1

list top level or root of files with project pulled from github
Gemfile
Gemfile.lock
LICENSE
Makefile
README.md
_config.yml
_data
_includes
_layouts
_notebooks
_posts
_site
aboutme.md
activate.sh
binarycalculator.md
csa.md
hangman.md
ijava-1.3.0.zip
images
index.md
indexBlogs.md
install.py
java
monthlycalendar.md
scripts
styles.css
timebox.md

Look at file list with hidden and long attributes

Most linux commands have options to enhance behavior. The enhanced listing below shows permission bits, owner of file, size and date.

ls reference

%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Navigate to project, then navigate to area wwhere files were cloned"
cd $project
pwd

echo ""
echo "list all files in long format"
ls -al   # all files -a (hidden) in -l long listing
Navigate to project, then navigate to area wwhere files were cloned
/home/emaad/vscode/emaad-github-pages1

list all files in long format
total 3424
drwxr-xr-x 13 emaad emaad    4096 Aug 23 09:55 .
drwxr-xr-x 30 emaad emaad    4096 Aug 17 08:49 ..
drwxr-xr-x  8 emaad emaad    4096 Aug 23 09:55 .git
drwxr-xr-x  3 emaad emaad    4096 Aug 17 08:49 .github
-rw-r--r--  1 emaad emaad     104 Aug 17 08:49 .gitignore
-rw-r--r--  1 emaad emaad     122 Aug 19 15:25 Gemfile
-rw-r--r--  1 emaad emaad    7303 Aug 19 15:24 Gemfile.lock
-rw-r--r--  1 emaad emaad    1081 Aug 17 08:49 LICENSE
-rw-r--r--  1 emaad emaad    3115 Aug 17 18:03 Makefile
-rw-r--r--  1 emaad emaad    5887 Aug 22 23:41 README.md
-rw-r--r--  1 emaad emaad    1388 Aug 19 16:28 _config.yml
drwxr-xr-x  2 emaad emaad    4096 Aug 17 08:49 _data
drwxr-xr-x  2 emaad emaad    4096 Aug 23 00:18 _includes
drwxr-xr-x  2 emaad emaad    4096 Aug 22 23:28 _layouts
drwxr-xr-x  4 emaad emaad    4096 Aug 22 23:24 _notebooks
drwxr-xr-x  2 emaad emaad    4096 Aug 22 08:59 _posts
drwxr-xr-x 11 emaad emaad    4096 Aug 23 00:19 _site
-rw-r--r--  1 emaad emaad     511 Aug 19 13:48 aboutme.md
-rwxr-xr-x  1 emaad emaad    1291 Aug 17 08:49 activate.sh
-rw-r--r--  1 emaad emaad      87 Aug 23 00:04 binarycalculator.md
-rw-r--r--  1 emaad emaad      95 Aug 22 09:01 csa.md
-rw-r--r--  1 emaad emaad      66 Aug 18 09:28 hangman.md
-rw-r--r--  1 emaad emaad 3366077 Dec  6  2021 ijava-1.3.0.zip
drwxr-xr-x  2 emaad emaad    4096 Aug 22 20:36 images
-rw-r--r--  1 emaad emaad     239 Aug 22 23:29 index.md
-rw-r--r--  1 emaad emaad     141 Aug 17 09:31 indexBlogs.md
-rw-r--r--  1 emaad emaad    7471 May  5  2019 install.py
drwxr-xr-x  3 emaad emaad    4096 May  5  2019 java
-rw-r--r--  1 emaad emaad      84 Aug 23 00:20 monthlycalendar.md
drwxr-xr-x  3 emaad emaad    4096 Aug 20 01:43 scripts
-rw-r--r--  1 emaad emaad    1270 Aug 19 12:53 styles.css
-rw-r--r--  1 emaad emaad      67 Aug 18 09:12 timebox.md
%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Look for posts"
export posts=$project/_posts  # _posts inside project
cd $posts  # this should exist per fastpages
pwd  # present working directory
ls -l  # list posts
Look for posts
/home/emaad/vscode/emaad-github-pages1/_posts
total 96
-rw-r--r-- 1 emaad emaad  1812 Aug 17 08:49 2023-08-15-Tools_Sprint.md
-rw-r--r-- 1 emaad emaad  4397 Aug 17 08:49 2023-08-16-Tools_Equipment.md
-rw-r--r-- 1 emaad emaad 28738 Aug 20 01:43 2023-08-16-linux_shell_IPYNB_2_.md
-rw-r--r-- 1 emaad emaad  3752 Aug 20 01:43 2023-08-17-AP-pseudo-vs-python_IPYNB_2_.md
-rw-r--r-- 1 emaad emaad  1843 Aug 20 00:24 2023-08-17-Freeform-Pic.md
-rw-r--r-- 1 emaad emaad  9580 Aug 20 00:03 2023-08-19-linux-commands.md
-rw-r--r-- 1 emaad emaad  4375 Aug 20 01:52 2023-08-20-ErrorBlog1.md
-rw-r--r-- 1 emaad emaad   523 Aug 20 01:43 2023-08-20-first-java-nb_IPYNB_2_.md
-rw-r--r-- 1 emaad emaad   468 Aug 17 08:49 2023-08-21-GitHub_Pages.md
-rw-r--r-- 1 emaad emaad  6059 Aug 20 01:43 2023-08-21-VSCode-GitHub_Pages_IPYNB_2_.md
-rw-r--r-- 1 emaad emaad   211 Aug 22 08:59 Firstjavanb_IPYNB_2_.md
-rw-r--r-- 1 emaad emaad   523 Aug 20 01:43 Untitled_IPYNB_2_.md
%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Look for notebooks"
export notebooks=$project/_notebooks  # _notebooks is inside project
cd $notebooks   # this should exist per fastpages
pwd  # present working directory
ls -l  # list notebooks
Look for notebooks
/home/emaad/vscode/emaad-github-pages1/_notebooks
total 80
-rw-r--r-- 1 emaad emaad 10638 Aug 22 23:32 2023-08-15-java-hello.ipynb
-rw-r--r-- 1 emaad emaad 38775 Aug 20 00:53 2023-08-16-linux_shell.ipynb
-rw-r--r-- 1 emaad emaad  5415 Aug 17 08:49 2023-08-17-AP-pseudo-vs-python.ipynb
-rw-r--r-- 1 emaad emaad  8615 Aug 17 08:49 2023-08-21-VSCode-GitHub_Pages.ipynb
-rw-r--r-- 1 emaad emaad  1699 Aug 22 09:02 Firstjavanb.ipynb
drwxr-xr-x 2 emaad emaad  4096 Aug 22 23:30 _site
%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Look for images in notebooks, print working directory, list files"
cd $notebooks  # this should exist per fastpages
pwd
ls -l
Look for images in notebooks, print working directory, list files
/home/emaad
total 16
drwxr-xr-x 28 emaad emaad 4096 Nov 26  2022 anaconda3
drwxr-xr-x  4 emaad emaad 4096 Apr 27 22:01 darkhallways
drwxr-xr-x 10 emaad emaad 4096 Aug 19 15:24 gems
drwxr-xr-x 30 emaad emaad 4096 Aug 17 08:49 vscode

Look inside a Markdown File

“cat” reads data from the file and gives its content as output

%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Navigate to project, then navigate to area wwhere files were cloned"

cd $project
echo "show the contents of README.md"
echo ""

cat README.md  # show contents of file, in this case markdown
echo ""
echo "end of README.md"

Navigate to project, then navigate to area wwhere files were cloned
show the contents of README.md

# Link to Emaad's GitHub Pages Here: https://emaad-mir.github.io/emaad-github-pages1/

## 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
    ```

end of README.md

Env, Git and GitHub

Env(ironment) is used to capture things like path to Code or Home directory. Git and GitHub is NOT Only used to exchange code between individuals, it is often used to exchange code through servers, in our case deployment for Website. All tools we use have a behind the scenes relationships with the system they run on (MacOS, Windows, Linus) or a relationship with servers which they are connected to (ie GitHub). There is an “env” command in bash. There are environment files and setting files (.git/config) for Git. They both use a key/value concept.

  • “env” show setting for your shell
  • “git clone” sets up a director of files
  • “cd $project” allows user to move inside that directory of files
  • “.git” is a hidden directory that is used by git to establish relationship between machine and the git server on GitHub.
%%script bash

# This command has no dependencies

echo "Show the shell environment variables, key on left of equal value on right"
echo ""

env
Show the shell environment variables, key on left of equal value on right

SHELL=/bin/bash
PYTHONUNBUFFERED=1
NVM_INC=/home/emaad/.nvm/versions/node/v18.16.0/include/node
WSL2_GUI_APPS_ENABLED=1
CONDA_EXE=/home/emaad/anaconda3/bin/conda
_CE_M=
APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=1
WSL_DISTRO_NAME=Ubuntu
ELECTRON_RUN_AS_NODE=1
VSCODE_AMD_ENTRYPOINT=vs/workbench/api/node/extensionHostProcess
NAME=emaad-mir
PWD=/home/emaad/vscode/emaad-github-pages1/_notebooks
NIX_PROFILES=/nix/var/nix/profiles/default /home/emaad/.nix-profile
LOGNAME=emaad
CONDA_ROOT=/home/emaad/anaconda3
CONDA_PREFIX=/home/emaad/anaconda3
PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING=1
HOME=/home/emaad
LANG=C.UTF-8
WSL_INTEROP=/run/WSL/12230_interop
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
WAYLAND_DISPLAY=wayland-0
NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
CONDA_PROMPT_MODIFIER=(base) 
PYDEVD_USE_FRAME_EVAL=NO
CLICOLOR=1
VSCODE_L10N_BUNDLE_LOCATION=
NVM_DIR=/home/emaad/.nvm
GEM_HOME=/home/emaad/gems
LESSCLOSE=/usr/bin/lesspipe %s %s
VSCODE_HANDLES_SIGPIPE=true
TERM=xterm-color
_CE_CONDA=
LESSOPEN=| /usr/bin/lesspipe %s
USER=emaad
GIT_PAGER=cat
PYTHONIOENCODING=utf-8
CONDA_SHLVL=1
DISPLAY=:0
SHLVL=2
NVM_CD_FLAGS=
PAGER=cat
VSCODE_CWD=/mnt/c/Users/emaad/AppData/Local/Programs/Microsoft VS Code
MPLBACKEND=module://matplotlib_inline.backend_inline
CONDA_PYTHON_EXE=/home/emaad/anaconda3/bin/python
XDG_RUNTIME_DIR=/mnt/wslg/runtime-dir
CONDA_DEFAULT_ENV=base
WSLENV=VSCODE_WSL_EXT_LOCATION/up
VSCODE_WSL_EXT_LOCATION=/mnt/c/Users/emaad/.vscode/extensions/ms-vscode-remote.remote-wsl-0.81.0
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/home/emaad/anaconda3/bin:/home/emaad/.vscode-server/bin/6c3e3dba23e8fadc360aed75ce363ba185c49794/bin/remote-cli:/home/emaad/.nix-profile/bin:/home/emaad/.local/bin:/home/emaad/.nvm/versions/node/v18.16.0/bin:/home/emaad/gems/bin:/home/emaad/gems/bin:/home/emaad/gems/bin:/home/emaad/gems/bin:/home/emaad/gems/bin:/home/emaad/gems/bin:/home/emaad/anaconda3/bin:/home/emaad/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/WINDOWS/system32:/mnt/c/Users/emaad/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/emaad/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Users/emaad/AppData/Roaming/npm:/snap/bin
VSCODE_NLS_CONFIG={"locale":"en","osLocale":"en","availableLanguages":{}}
NVM_BIN=/home/emaad/.nvm/versions/node/v18.16.0/bin
HOSTTYPE=x86_64
PULSE_SERVER=unix:/mnt/wslg/PulseServer
VSCODE_HANDLES_UNCAUGHT_ERRORS=true
VSCODE_IPC_HOOK_CLI=/mnt/wslg/runtime-dir/vscode-ipc-9cc89e8b-00b4-4550-a254-eb95745604fd.sock
_=/usr/bin/env
%%script bash

# Extract saved variables
source /tmp/variables.sh

cd $project

echo ""
echo "show the secrets of .git"
cd .git
ls -l

echo ""
echo "look at config file"
cat config
show the secrets of .git
total 56
-rw-r--r--   1 emaad emaad   15 Aug 23 09:55 COMMIT_EDITMSG
-rw-r--r--   1 emaad emaad  108 Aug 23 10:25 FETCH_HEAD
-rw-r--r--   1 emaad emaad   21 Aug 17 08:49 HEAD
drwxr-xr-x   2 emaad emaad 4096 Aug 17 08:49 branches
-rw-r--r--   1 emaad emaad  273 Aug 17 08:49 config
-rw-r--r--   1 emaad emaad   73 Aug 17 08:49 description
drwxr-xr-x   2 emaad emaad 4096 Aug 17 08:49 hooks
-rw-r--r--   1 emaad emaad 5816 Aug 23 09:55 index
drwxr-xr-x   2 emaad emaad 4096 Aug 17 08:49 info
drwxr-xr-x   3 emaad emaad 4096 Aug 17 08:49 logs
drwxr-xr-x 121 emaad emaad 4096 Aug 23 10:25 objects
-rw-r--r--   1 emaad emaad  112 Aug 17 08:49 packed-refs
drwxr-xr-x   5 emaad emaad 4096 Aug 17 08:49 refs

look at config file
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://github.com/Emaad-Mir/emaad-github-pages1.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	remote = origin
	merge = refs/heads/main

Advanced Student Request - Make a file in Bash

This example was requested by a student (Jun Lim, CSA). The request was to make jupyer file using bash, I adapted the request to markdown. This type of thought will have great extrapolation to coding and possibilities of using List, Arrays, or APIs to build user interfaces. JavaScript is a language where building HTML is very common.

To get more interesting output from terminal, this will require using something like mdless (https://github.com/ttscoff/mdless). This enables see markdown in rendered format.

Output of the example is much nicer in “jupyter”

%%script bash

# This example has error in VSCode, it run best on Jupyter
cd /tmp

file="sample.md"
if [ -f "$file" ]; then
    rm $file
fi

tee -a $file >/dev/null <<EOF
# Show Generated Markdown
This introductory paragraph and this line and the title above are generated using tee with the standard input (<<) redirection operator.
- This bulleted element is still part of the tee body.
EOF

echo "- This bulleted element and lines below are generated using echo with standard output (>>) redirection operator." >> $file
echo "- The list definition, as is, is using space to seperate lines.  Thus the use of commas and hyphens in output." >> $file
actions=("ls,list-directory" "cd,change-directory" "pwd,present-working-directory" "if-then-fi,test-condition" "env,bash-environment-variables" "cat,view-file-contents" "tee,write-to-output" "echo,display-content-of-string" "echo_text_>\$file,write-content-to-file" "echo_text_>>\$file,append-content-to-file")
for action in ${actions[@]}; do  # for loop is very similar to other language, though [@], semi-colon, do are new
  action=${action//-/ }  # convert dash to space
  action=${action//,/: } # convert comma to colon
  action=${action//_text_/ \"sample text\" } # convert _text_ to sample text, note escape character \ to avoid "" having meaning
  echo "    - ${action//-/ }" >> $file  # echo is redirected to file with >>
done

echo ""
echo "File listing and status"
ls -l $file # list file
wc $file   # show words
mdless $file  # this requires installation, but renders markown from terminal

rm $file  # clean up termporary file
File listing and status
-rw-r--r-- 1 emaad emaad 809 Aug 23 10:26 sample.md
 15 132 809 sample.md



Show Generated Markdown ========================================================

This introductory paragraph and this line and the title above are generated
using tee with the standard input (<<) redirection operator.
- This bulleted element is still part of the tee body.
- This bulleted element and lines below are generated using echo with standard
output (>>) redirection operator.
- The list definition, as is, is using space to seperate lines. Thus the use of
commas and hyphens in output.
      - ls: list directory
      - cd: change directory
      - pwd: present working directory
      - if then fi: test condition
      - env: bash environment variables
      - cat: view file contents
      - tee: write to output
      - echo: display content of string
      - echo "sample text" >$file: write content to file
      - echo "sample text" >>$file: append content to file



Hack Preparation.

Review Tool Setup Procedures and think about some thing you could verify through a Shell notebook.

  • Come up with your own student view of this procedure to show your tools are installed. It is best that you keep the few things you understand, add things later as you start to understand them.
  • Name and create blog notes on some Linux commands you will use frequently.
  • Is there anything we use to verify tools we installed? Review versions?
  • How would you update a repository? Use the git command line?