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.

echo "hello world"
STR="Hello World"
echo $STR 
hello world


Hello World
%%script bash

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

bash: fg: %%script: no such job

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


source /tmp/variables.sh

echo "Project dir: $project_dir"
echo "Project: $project"
echo "Repo: $project_repo"
bash: /tmp/variables.sh: No such file or directory
Project dir: 
Project: 
Repo: 

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." 

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

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
bash: fg: %%script: no such job
bash: /tmp/variables.sh: No such file or directory
Navigate to project, then navigate to area wwhere files were cloned
/Users/vn1

list all files in long format
total 824
drwxr-x---+  135 vn1   staff    4320 Aug 22 09:07 .
drwxr-xr-x     7 root  admin     224 Mar 19 11:32 ..
-r--------     1 vn1   staff       7 Nov  3  2021 .CFUserTextEncoding
-rw-r--r--@    1 vn1   staff   18436 Aug  7 20:03 .DS_Store
drwxr-xr-x     7 vn1   staff     224 Jun  8 12:28 .IdentityService
drwx------+   37 vn1   staff    1184 Aug 21 23:36 .Trash
drwxr-xr-x     4 vn1   staff     128 Apr 27 00:14 .anaconda
drwxr-xr-x     7 vn1   staff     224 Jul 25  2022 .android
-rw-------@    1 vn1   staff   27934 Aug 22 19:09 .bash_history
-rw-r--r--     1 vn1   staff     641 Jan  8  2023 .bash_profile
drwx------    55 vn1   staff    1760 Aug 22 19:19 .bash_sessions
drwxr-xr-x@    3 vn1   staff      96 Jan  5  2023 .bundle
drwxr-xr-x     4 vn1   staff     128 Jan  5  2023 .cache
drwxr-xr-x     3 vn1   staff      96 Jan  5  2023 .cocoapods
drwxr-xr-x     4 vn1   staff     128 Sep  6  2022 .conda
-rw-r--r--     1 vn1   staff      23 Aug 18  2022 .condarc
drwxr-xr-x    10 vn1   staff     320 Aug 19  2022 .config
drwxr-xr-x     3 vn1   staff      96 Aug 18  2022 .continuum
-rw-r--r--@    1 vn1   staff     616 Nov 13  2021 .cshrc
-rw-r--r--@    1 vn1   staff     463 Nov  7  2021 .cshrc.pysave
drwx------     3 vn1   staff      96 Nov 18  2021 .cups
drwxr-xr-x     5 vn1   staff     160 Jul 26  2022 .dart
drwxr-xr-x     5 vn1   staff     160 Jul 27  2022 .dartServer
drwxr-xr-x     3 vn1   staff      96 Feb  6  2023 .degit
drwxr-xr-x    13 vn1   staff     416 Sep 16  2022 .docker
drwxr-xr-x    19 vn1   staff     608 May 27 16:42 .dotnet
-rw-r--r--     1 vn1   staff      78 Jul 25  2022 .flutter
drwxr-xr-x@    3 vn1   staff      96 Jan  9  2023 .flutter-devtools
drwxr-xr-x@    4 vn1   staff     128 Jan  5  2023 .gem
-rw-r--r--     1 vn1   staff     174 Jun 29 12:22 .gitconfig
drwxr-xr-x    12 vn1   staff     384 Jan  3  2023 .gradle
drwxr-xr-x     5 vn1   staff     160 Aug  5  2022 .idlerc
drwxr-xr-x     3 vn1   staff      96 Aug 22  2022 .ipynb_checkpoints
drwxr-xr-x     5 vn1   staff     160 Sep 23  2022 .ipython
drwxr-xr-x     3 vn1   staff      96 Aug 19  2022 .jupyter
drwxr-xr-x     3 vn1   staff      96 Oct 30  2022 .keras
-rw-r--r--@    1 vn1   staff    1468 Feb  5  2023 .labelImgSettings.pkl
-rw-------     1 vn1   staff    1072 Aug  6 18:03 .lesshst
drwxr-xr-x     4 vn1   staff     128 Jul  5  2022 .local
drwxr-xr-x@    3 vn1   staff      96 Dec 13  2022 .matplotlib
drwxr-xr-x     3 vn1   staff      96 Jan 30  2022 .mono
-rw-------@    1 vn1   staff     135 Aug 22 19:08 .node_repl_history
drwxr-xr-x     9 vn1   staff     288 Feb  6  2023 .npm
drwxr-xr-x     3 vn1   staff      96 Jul 10  2022 .nuget
drwxr-xr-x     2 vn1   staff      64 Aug 29  2022 .omnisharp
drwxr-xr-x@    3 vn1   staff      96 Jun 12 11:54 .openjfx
drwxr-xr-x   102 vn1   staff    3264 Jun 12 19:17 .oracle_jre_usage
drwxr-xr-x     4 vn1   staff     128 Jun 28  2022 .plastic4
-rw-r--r--     1 vn1   staff     195 Sep 16  2022 .profile
drwxr-xr-x     6 vn1   staff     192 Jan  3  2023 .pub-cache
-rw-------     1 vn1   staff     779 Aug 21 09:20 .python_history
-rw-------     1 vn1   staff   20480 Jul  4  2022 .random.swp
drwxr-xr-x     3 vn1   staff      96 Jan  8  2023 .rubies
-rw-------     1 vn1   staff     969 Jan 18  2023 .sqlite_history
drwx------     6 vn1   staff     192 Feb 12  2023 .ssh
drwxr-xr-x     5 vn1   staff     160 Feb 13  2023 .swiftpm
-rw-r--r--     1 vn1   staff     464 Sep 11  2022 .tcshrc
-rw-r--r--     1 vn1   staff     311 Aug 19  2022 .tcshrc.pysave
drwxr-xr-x     4 vn1   staff     128 Jul  3  2022 .templateengine
drwxr-xr-x     4 vn1   staff     128 Nov 21  2021 .thumbnails
-rw-------     1 vn1   staff   15672 Aug  5 16:01 .viminfo
-rw-r--r--@    1 vn1   staff      80 Nov  7  2021 .vimrc
drwx------     6 vn1   staff     192 Dec 12  2021 .vnc
drwxr-xr-x     5 vn1   staff     160 Jun 27 22:08 .vscode
-rw-r--r--     1 vn1   staff     347 Aug 22 18:57 .wget-hsts
-rw-r--r--     1 vn1   staff     594 Aug 19  2022 .xonshrc
-rw-------     1 vn1   staff   19736 Aug 22 09:07 .zsh_history
drwx------    79 vn1   staff    2528 Aug 22 09:07 .zsh_sessions
-rw-r--r--@    1 vn1   staff     893 Jan  8  2023 .zshrc
-rw-r--r--     1 vn1   staff   52614 Mar 13 08:09 AP-unit2-4a.html
drwxr-xr-x    29 vn1   staff     928 Mar  6 12:04 APCSP
drwxr-xr-x    28 vn1   staff     896 Dec 12  2022 APCSP-Tri-2
drwx------@    5 vn1   staff     160 Mar  8 22:03 Applications
drwxr-xr-x     3 vn1   staff      96 Aug 17 08:48 CSA
drwxr-xr-x    16 vn1   staff     512 Feb 12  2023 CWCsite
drwxr-xr-x    18 vn1   staff     576 Feb 18  2023 ChezRat
drwx------@  483 vn1   staff   15456 Aug 21 17:58 Desktop
drwxr-xr-x     3 vn1   staff      96 Jul 25  2022 Dev
drwx------@  138 vn1   staff    4416 Aug  7 19:52 Documents
drwx------@ 1749 vn1   staff   55968 Aug 22 08:08 Downloads
drwxr-xr-x    20 vn1   staff     640 Jan  9  2023 IDKwhatthisisfor
drwxr-xr-x     5 vn1   staff     160 Jul  6  2022 Java-Training-Exercises
drwx------@  100 vn1   staff    3200 Feb  2  2023 Library
drwx------     7 vn1   staff     224 Nov  3  2021 Movies
drwx------+    6 vn1   staff     192 Nov  3  2021 Music
drwxr-xr-x     6 vn1   staff     192 Mar 30 14:41 OptixStuff
drwx------+   13 vn1   staff     416 Jun 29 10:57 Pictures
drwxr-xr-x@    3 vn1   staff      96 Jan 19  2023 Postman
drwxr-xr-x     5 vn1   staff     160 Aug  4  2022 Projects
drwxr-xr-x+    4 vn1   staff     128 Nov  3  2021 Public
drwxr-xr-x     2 vn1   staff      64 Jun 14 19:14 REHS
drwxr-xr-x@   15 vn1   staff     480 Jun 29 12:30 SpellSlinger
drwxr-xr-x     2 vn1   staff      64 Apr 25 13:48 THISSHIT
drwxr-xr-x    11 vn1   staff     352 Nov  4  2022 acedemic-organizer
-rw-r--r--@    1 vn1   staff      38 Feb  5  2023 apitest.py
drwxr-xr-x@   21 vn1   staff     672 Feb 13  2023 cameratest
drwxr-xr-x    14 vn1   staff     448 Aug 18 09:47 darkhallways
-rw-r--r--     1 vn1   staff      14 Feb 13  2023 death.txt
drwxr-xr-x     6 vn1   staff     192 Sep 25  2022 discBots
drwxr-xr-x    21 vn1   staff     672 Jun  1 22:34 doItAllBackend
drwxr-xr-x    19 vn1   staff     608 Sep 24  2022 flask
drwxr-xr-x    20 vn1   staff     640 Oct  2  2022 flask_portfolio
drwxr-xr-x     4 vn1   staff     128 Sep 24  2022 flask_portfolio_nvarap
drwxr-xr-x    22 vn1   staff     704 Feb 24 20:58 freshflask
-rw-r--r--     1 vn1   staff     237 Mar 13 15:19 fuck.txt
drwxr-xr-x    11 vn1   staff     352 Mar 20 22:48 fuckMyLife
drwxr-xr-x     9 vn1   staff     288 Dec 11  2022 gethinsome
drwxr-xr-x    17 vn1   staff     544 Sep  2  2022 getting-started
-rw-r--r--@    1 vn1   staff     838 Jan 13  2023 goddamn.ipynb
drwxr-xr-x    19 vn1   staff     608 Jun  1 22:33 groupFlaskProject
drwxr-xr-x     7 vn1   staff     224 May  1 17:58 hardyweinburg
drwxr-xr-x     4 vn1   staff     128 Sep 25  2022 helloworld
drwxr-xr-x     4 vn1   staff     128 Jun 29 12:23 knightmare
drwxr-xr-x     6 vn1   staff     192 Jan 17  2023 mortsshit
drwxr-xr-x     4 vn1   staff     128 May 23 21:24 myblog
drwxr-xr-x    14 vn1   staff     448 Jun  2 00:13 nVarap.github.io
drwxr-xr-x@    3 vn1   staff      96 May 15 13:30 nltk_data
drwxr-xr-x    50 vn1   staff    1600 Feb 10  2023 node_modules
drwxr-xr-x     3 vn1   staff      96 Aug 19  2022 opt
drwxr-xr-x    10 vn1   staff     320 Mar  3 18:01 otherpeoplesshit
-rw-r--r--     1 vn1   staff   37307 May 18 19:55 package-lock.json
-rw-r--r--     1 vn1   staff      82 Feb 10  2023 package.json
drwxr-xr-x     8 vn1   staff     256 Feb 26 20:44 pgkstuff
-rw-r--r--@    1 vn1   staff  116253 May 10 09:56 princeton-review-ap-biology-prep-2023-by-the-princeton-review
drwxr-xr-x@    3 vn1   staff      96 Mar  6 13:09 seaborn-data
drwxr-xr-x     5 vn1   staff     160 Oct 23  2022 seocnd
drwxr-xr-x     4 vn1   staff     128 Jan  8  2023 src
drwxrwxr-x    10 vn1   staff     320 Feb  6  2023 svelte-typescript-app
drwxr-xr-x     8 vn1   staff     256 May 31 18:56 thirdtri
drwxr-xr-x    14 vn1   staff     448 Dec  4  2022 try
drwxr-xr-x     3 vn1   staff      96 May 18 22:01 urhur
drwxr-xr-x     4 vn1   staff     128 Jan 10  2023 vscode
drwxr-xr-x    12 vn1   staff     384 Aug 18 14:59 wekafiles
-rw-r--r--@    1 vn1   staff    2241 Jun 20 14:11 were.py
drwxr-xr-x     3 vn1   staff      96 Jul  3  2022 wpilib
%%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
bash: fg: %%script: no such job
bash: /tmp/variables.sh: No such file or directory
Look for posts
bash: cd: /_posts: No such file or directory
/Users/vn1
total 464
-rw-r--r--     1 vn1  staff   52614 Mar 13 08:09 AP-unit2-4a.html
drwxr-xr-x    29 vn1  staff     928 Mar  6 12:04 APCSP
drwxr-xr-x    28 vn1  staff     896 Dec 12  2022 APCSP-Tri-2
drwx------@    5 vn1  staff     160 Mar  8 22:03 Applications
drwxr-xr-x     3 vn1  staff      96 Aug 17 08:48 CSA
drwxr-xr-x    16 vn1  staff     512 Feb 12  2023 CWCsite
drwxr-xr-x    18 vn1  staff     576 Feb 18  2023 ChezRat
drwx------@  483 vn1  staff   15456 Aug 21 17:58 Desktop
drwxr-xr-x     3 vn1  staff      96 Jul 25  2022 Dev
drwx------@  138 vn1  staff    4416 Aug  7 19:52 Documents
drwx------@ 1749 vn1  staff   55968 Aug 22 08:08 Downloads
drwxr-xr-x    20 vn1  staff     640 Jan  9  2023 IDKwhatthisisfor
drwxr-xr-x     5 vn1  staff     160 Jul  6  2022 Java-Training-Exercises
drwx------@  100 vn1  staff    3200 Feb  2  2023 Library
drwx------     7 vn1  staff     224 Nov  3  2021 Movies
drwx------+    6 vn1  staff     192 Nov  3  2021 Music
drwxr-xr-x     6 vn1  staff     192 Mar 30 14:41 OptixStuff
drwx------+   13 vn1  staff     416 Jun 29 10:57 Pictures
drwxr-xr-x@    3 vn1  staff      96 Jan 19  2023 Postman
drwxr-xr-x     5 vn1  staff     160 Aug  4  2022 Projects
drwxr-xr-x+    4 vn1  staff     128 Nov  3  2021 Public
drwxr-xr-x     2 vn1  staff      64 Jun 14 19:14 REHS
drwxr-xr-x@   15 vn1  staff     480 Jun 29 12:30 SpellSlinger
drwxr-xr-x     2 vn1  staff      64 Apr 25 13:48 THISSHIT
drwxr-xr-x    11 vn1  staff     352 Nov  4  2022 acedemic-organizer
-rw-r--r--@    1 vn1  staff      38 Feb  5  2023 apitest.py
drwxr-xr-x@   21 vn1  staff     672 Feb 13  2023 cameratest
drwxr-xr-x    14 vn1  staff     448 Aug 18 09:47 darkhallways
-rw-r--r--     1 vn1  staff      14 Feb 13  2023 death.txt
drwxr-xr-x     6 vn1  staff     192 Sep 25  2022 discBots
drwxr-xr-x    21 vn1  staff     672 Jun  1 22:34 doItAllBackend
drwxr-xr-x    19 vn1  staff     608 Sep 24  2022 flask
drwxr-xr-x    20 vn1  staff     640 Oct  2  2022 flask_portfolio
drwxr-xr-x     4 vn1  staff     128 Sep 24  2022 flask_portfolio_nvarap
drwxr-xr-x    22 vn1  staff     704 Feb 24 20:58 freshflask
-rw-r--r--     1 vn1  staff     237 Mar 13 15:19 fuck.txt
drwxr-xr-x    11 vn1  staff     352 Mar 20 22:48 fuckMyLife
drwxr-xr-x     9 vn1  staff     288 Dec 11  2022 gethinsome
drwxr-xr-x    17 vn1  staff     544 Sep  2  2022 getting-started
-rw-r--r--@    1 vn1  staff     838 Jan 13  2023 goddamn.ipynb
drwxr-xr-x    19 vn1  staff     608 Jun  1 22:33 groupFlaskProject
drwxr-xr-x     7 vn1  staff     224 May  1 17:58 hardyweinburg
drwxr-xr-x     4 vn1  staff     128 Sep 25  2022 helloworld
drwxr-xr-x     4 vn1  staff     128 Jun 29 12:23 knightmare
drwxr-xr-x     6 vn1  staff     192 Jan 17  2023 mortsshit
drwxr-xr-x     4 vn1  staff     128 May 23 21:24 myblog
drwxr-xr-x    14 vn1  staff     448 Jun  2 00:13 nVarap.github.io
drwxr-xr-x@    3 vn1  staff      96 May 15 13:30 nltk_data
drwxr-xr-x    50 vn1  staff    1600 Feb 10  2023 node_modules
drwxr-xr-x     3 vn1  staff      96 Aug 19  2022 opt
drwxr-xr-x    10 vn1  staff     320 Mar  3 18:01 otherpeoplesshit
-rw-r--r--     1 vn1  staff   37307 May 18 19:55 package-lock.json
-rw-r--r--     1 vn1  staff      82 Feb 10  2023 package.json
drwxr-xr-x     8 vn1  staff     256 Feb 26 20:44 pgkstuff
-rw-r--r--@    1 vn1  staff  116253 May 10 09:56 princeton-review-ap-biology-prep-2023-by-the-princeton-review
drwxr-xr-x@    3 vn1  staff      96 Mar  6 13:09 seaborn-data
drwxr-xr-x     5 vn1  staff     160 Oct 23  2022 seocnd
drwxr-xr-x     4 vn1  staff     128 Jan  8  2023 src
drwxrwxr-x    10 vn1  staff     320 Feb  6  2023 svelte-typescript-app
drwxr-xr-x     8 vn1  staff     256 May 31 18:56 thirdtri
drwxr-xr-x    14 vn1  staff     448 Dec  4  2022 try
drwxr-xr-x     3 vn1  staff      96 May 18 22:01 urhur
drwxr-xr-x     4 vn1  staff     128 Jan 10  2023 vscode
drwxr-xr-x    12 vn1  staff     384 Aug 18 14:59 wekafiles
-rw-r--r--@    1 vn1  staff    2241 Jun 20 14:11 were.py
drwxr-xr-x     3 vn1  staff      96 Jul  3  2022 wpilib
%%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
bash: fg: %%script: no such job
bash: /tmp/variables.sh: No such file or directory
Look for notebooks
bash: cd: /_notebooks: No such file or directory
/Users/vn1
total 464
-rw-r--r--     1 vn1  staff   52614 Mar 13 08:09 AP-unit2-4a.html
drwxr-xr-x    29 vn1  staff     928 Mar  6 12:04 APCSP
drwxr-xr-x    28 vn1  staff     896 Dec 12  2022 APCSP-Tri-2
drwx------@    5 vn1  staff     160 Mar  8 22:03 Applications
drwxr-xr-x     3 vn1  staff      96 Aug 17 08:48 CSA
drwxr-xr-x    16 vn1  staff     512 Feb 12  2023 CWCsite
drwxr-xr-x    18 vn1  staff     576 Feb 18  2023 ChezRat
drwx------@  483 vn1  staff   15456 Aug 21 17:58 Desktop
drwxr-xr-x     3 vn1  staff      96 Jul 25  2022 Dev
drwx------@  138 vn1  staff    4416 Aug  7 19:52 Documents
drwx------@ 1749 vn1  staff   55968 Aug 22 08:08 Downloads
drwxr-xr-x    20 vn1  staff     640 Jan  9  2023 IDKwhatthisisfor
drwxr-xr-x     5 vn1  staff     160 Jul  6  2022 Java-Training-Exercises
drwx------@  100 vn1  staff    3200 Feb  2  2023 Library
drwx------     7 vn1  staff     224 Nov  3  2021 Movies
drwx------+    6 vn1  staff     192 Nov  3  2021 Music
drwxr-xr-x     6 vn1  staff     192 Mar 30 14:41 OptixStuff
drwx------+   13 vn1  staff     416 Jun 29 10:57 Pictures
drwxr-xr-x@    3 vn1  staff      96 Jan 19  2023 Postman
drwxr-xr-x     5 vn1  staff     160 Aug  4  2022 Projects
drwxr-xr-x+    4 vn1  staff     128 Nov  3  2021 Public
drwxr-xr-x     2 vn1  staff      64 Jun 14 19:14 REHS
drwxr-xr-x@   15 vn1  staff     480 Jun 29 12:30 SpellSlinger
drwxr-xr-x     2 vn1  staff      64 Apr 25 13:48 THISSHIT
drwxr-xr-x    11 vn1  staff     352 Nov  4  2022 acedemic-organizer
-rw-r--r--@    1 vn1  staff      38 Feb  5  2023 apitest.py
drwxr-xr-x@   21 vn1  staff     672 Feb 13  2023 cameratest
drwxr-xr-x    14 vn1  staff     448 Aug 18 09:47 darkhallways
-rw-r--r--     1 vn1  staff      14 Feb 13  2023 death.txt
drwxr-xr-x     6 vn1  staff     192 Sep 25  2022 discBots
drwxr-xr-x    21 vn1  staff     672 Jun  1 22:34 doItAllBackend
drwxr-xr-x    19 vn1  staff     608 Sep 24  2022 flask
drwxr-xr-x    20 vn1  staff     640 Oct  2  2022 flask_portfolio
drwxr-xr-x     4 vn1  staff     128 Sep 24  2022 flask_portfolio_nvarap
drwxr-xr-x    22 vn1  staff     704 Feb 24 20:58 freshflask
-rw-r--r--     1 vn1  staff     237 Mar 13 15:19 fuck.txt
drwxr-xr-x    11 vn1  staff     352 Mar 20 22:48 fuckMyLife
drwxr-xr-x     9 vn1  staff     288 Dec 11  2022 gethinsome
drwxr-xr-x    17 vn1  staff     544 Sep  2  2022 getting-started
-rw-r--r--@    1 vn1  staff     838 Jan 13  2023 goddamn.ipynb
drwxr-xr-x    19 vn1  staff     608 Jun  1 22:33 groupFlaskProject
drwxr-xr-x     7 vn1  staff     224 May  1 17:58 hardyweinburg
drwxr-xr-x     4 vn1  staff     128 Sep 25  2022 helloworld
drwxr-xr-x     4 vn1  staff     128 Jun 29 12:23 knightmare
drwxr-xr-x     6 vn1  staff     192 Jan 17  2023 mortsshit
drwxr-xr-x     4 vn1  staff     128 May 23 21:24 myblog
drwxr-xr-x    14 vn1  staff     448 Jun  2 00:13 nVarap.github.io
drwxr-xr-x@    3 vn1  staff      96 May 15 13:30 nltk_data
drwxr-xr-x    50 vn1  staff    1600 Feb 10  2023 node_modules
drwxr-xr-x     3 vn1  staff      96 Aug 19  2022 opt
drwxr-xr-x    10 vn1  staff     320 Mar  3 18:01 otherpeoplesshit
-rw-r--r--     1 vn1  staff   37307 May 18 19:55 package-lock.json
-rw-r--r--     1 vn1  staff      82 Feb 10  2023 package.json
drwxr-xr-x     8 vn1  staff     256 Feb 26 20:44 pgkstuff
-rw-r--r--@    1 vn1  staff  116253 May 10 09:56 princeton-review-ap-biology-prep-2023-by-the-princeton-review
drwxr-xr-x@    3 vn1  staff      96 Mar  6 13:09 seaborn-data
drwxr-xr-x     5 vn1  staff     160 Oct 23  2022 seocnd
drwxr-xr-x     4 vn1  staff     128 Jan  8  2023 src
drwxrwxr-x    10 vn1  staff     320 Feb  6  2023 svelte-typescript-app
drwxr-xr-x     8 vn1  staff     256 May 31 18:56 thirdtri
drwxr-xr-x    14 vn1  staff     448 Dec  4  2022 try
drwxr-xr-x     3 vn1  staff      96 May 18 22:01 urhur
drwxr-xr-x     4 vn1  staff     128 Jan 10  2023 vscode
drwxr-xr-x    12 vn1  staff     384 Aug 18 14:59 wekafiles
-rw-r--r--@    1 vn1  staff    2241 Jun 20 14:11 were.py
drwxr-xr-x     3 vn1  staff      96 Jul  3  2022 wpilib
%%script bash

# Extract saved variables
source /tmp/variables.sh

echo "Look for images in notebooks, print working directory, list files"
cd $notebooks/images  # this should exist per fastpages
pwd
ls -l
bash: fg: %%script: no such job
bash: /tmp/variables.sh: No such file or directory
Look for images in notebooks, print working directory, list files
bash: cd: /images: No such file or directory
/Users/vn1
total 464
-rw-r--r--     1 vn1  staff   52614 Mar 13 08:09 AP-unit2-4a.html
drwxr-xr-x    29 vn1  staff     928 Mar  6 12:04 APCSP
drwxr-xr-x    28 vn1  staff     896 Dec 12  2022 APCSP-Tri-2
drwx------@    5 vn1  staff     160 Mar  8 22:03 Applications
drwxr-xr-x     3 vn1  staff      96 Aug 17 08:48 CSA
drwxr-xr-x    16 vn1  staff     512 Feb 12  2023 CWCsite
drwxr-xr-x    18 vn1  staff     576 Feb 18  2023 ChezRat
drwx------@  483 vn1  staff   15456 Aug 21 17:58 Desktop
drwxr-xr-x     3 vn1  staff      96 Jul 25  2022 Dev
drwx------@  138 vn1  staff    4416 Aug  7 19:52 Documents
drwx------@ 1749 vn1  staff   55968 Aug 22 08:08 Downloads
drwxr-xr-x    20 vn1  staff     640 Jan  9  2023 IDKwhatthisisfor
drwxr-xr-x     5 vn1  staff     160 Jul  6  2022 Java-Training-Exercises
drwx------@  100 vn1  staff    3200 Feb  2  2023 Library
drwx------     7 vn1  staff     224 Nov  3  2021 Movies
drwx------+    6 vn1  staff     192 Nov  3  2021 Music
drwxr-xr-x     6 vn1  staff     192 Mar 30 14:41 OptixStuff
drwx------+   13 vn1  staff     416 Jun 29 10:57 Pictures
drwxr-xr-x@    3 vn1  staff      96 Jan 19  2023 Postman
drwxr-xr-x     5 vn1  staff     160 Aug  4  2022 Projects
drwxr-xr-x+    4 vn1  staff     128 Nov  3  2021 Public
drwxr-xr-x     2 vn1  staff      64 Jun 14 19:14 REHS
drwxr-xr-x@   15 vn1  staff     480 Jun 29 12:30 SpellSlinger
drwxr-xr-x     2 vn1  staff      64 Apr 25 13:48 THISSHIT
drwxr-xr-x    11 vn1  staff     352 Nov  4  2022 acedemic-organizer
-rw-r--r--@    1 vn1  staff      38 Feb  5  2023 apitest.py
drwxr-xr-x@   21 vn1  staff     672 Feb 13  2023 cameratest
drwxr-xr-x    14 vn1  staff     448 Aug 18 09:47 darkhallways
-rw-r--r--     1 vn1  staff      14 Feb 13  2023 death.txt
drwxr-xr-x     6 vn1  staff     192 Sep 25  2022 discBots
drwxr-xr-x    21 vn1  staff     672 Jun  1 22:34 doItAllBackend
drwxr-xr-x    19 vn1  staff     608 Sep 24  2022 flask
drwxr-xr-x    20 vn1  staff     640 Oct  2  2022 flask_portfolio
drwxr-xr-x     4 vn1  staff     128 Sep 24  2022 flask_portfolio_nvarap
drwxr-xr-x    22 vn1  staff     704 Feb 24 20:58 freshflask
-rw-r--r--     1 vn1  staff     237 Mar 13 15:19 fuck.txt
drwxr-xr-x    11 vn1  staff     352 Mar 20 22:48 fuckMyLife
drwxr-xr-x     9 vn1  staff     288 Dec 11  2022 gethinsome
drwxr-xr-x    17 vn1  staff     544 Sep  2  2022 getting-started
-rw-r--r--@    1 vn1  staff     838 Jan 13  2023 goddamn.ipynb
drwxr-xr-x    19 vn1  staff     608 Jun  1 22:33 groupFlaskProject
drwxr-xr-x     7 vn1  staff     224 May  1 17:58 hardyweinburg
drwxr-xr-x     4 vn1  staff     128 Sep 25  2022 helloworld
drwxr-xr-x     4 vn1  staff     128 Jun 29 12:23 knightmare
drwxr-xr-x     6 vn1  staff     192 Jan 17  2023 mortsshit
drwxr-xr-x     4 vn1  staff     128 May 23 21:24 myblog
drwxr-xr-x    14 vn1  staff     448 Jun  2 00:13 nVarap.github.io
drwxr-xr-x@    3 vn1  staff      96 May 15 13:30 nltk_data
drwxr-xr-x    50 vn1  staff    1600 Feb 10  2023 node_modules
drwxr-xr-x     3 vn1  staff      96 Aug 19  2022 opt
drwxr-xr-x    10 vn1  staff     320 Mar  3 18:01 otherpeoplesshit
-rw-r--r--     1 vn1  staff   37307 May 18 19:55 package-lock.json
-rw-r--r--     1 vn1  staff      82 Feb 10  2023 package.json
drwxr-xr-x     8 vn1  staff     256 Feb 26 20:44 pgkstuff
-rw-r--r--@    1 vn1  staff  116253 May 10 09:56 princeton-review-ap-biology-prep-2023-by-the-princeton-review
drwxr-xr-x@    3 vn1  staff      96 Mar  6 13:09 seaborn-data
drwxr-xr-x     5 vn1  staff     160 Oct 23  2022 seocnd
drwxr-xr-x     4 vn1  staff     128 Jan  8  2023 src
drwxrwxr-x    10 vn1  staff     320 Feb  6  2023 svelte-typescript-app
drwxr-xr-x     8 vn1  staff     256 May 31 18:56 thirdtri
drwxr-xr-x    14 vn1  staff     448 Dec  4  2022 try
drwxr-xr-x     3 vn1  staff      96 May 18 22:01 urhur
drwxr-xr-x     4 vn1  staff     128 Jan 10  2023 vscode
drwxr-xr-x    12 vn1  staff     384 Aug 18 14:59 wekafiles
-rw-r--r--@    1 vn1  staff    2241 Jun 20 14:11 were.py
drwxr-xr-x     3 vn1  staff      96 Jul  3  2022 wpilib

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"

bash: fg: %%script: no such job
bash: /tmp/variables.sh: No such file or directory
Navigate to project, then navigate to area wwhere files were cloned
show the contents of README.md

cat: README.md: No such file or directory

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
%%script bash

# Extract saved variables
source /tmp/variables.sh
 
cd $project # CD into the project

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

echo ""
echo "look at config file"
cat config # Shows git file
bash: fg: %%script: no such job
bash: /tmp/variables.sh: No such file or directory

show the secrets of .git
bash: cd: .git: No such file or directory
total 464
-rw-r--r--     1 vn1  staff   52614 Mar 13 08:09 AP-unit2-4a.html
drwxr-xr-x    29 vn1  staff     928 Mar  6 12:04 APCSP
drwxr-xr-x    28 vn1  staff     896 Dec 12  2022 APCSP-Tri-2
drwx------@    5 vn1  staff     160 Mar  8 22:03 Applications
drwxr-xr-x     3 vn1  staff      96 Aug 17 08:48 CSA
drwxr-xr-x    16 vn1  staff     512 Feb 12  2023 CWCsite
drwxr-xr-x    18 vn1  staff     576 Feb 18  2023 ChezRat
drwx------@  483 vn1  staff   15456 Aug 21 17:58 Desktop
drwxr-xr-x     3 vn1  staff      96 Jul 25  2022 Dev
drwx------@  138 vn1  staff    4416 Aug  7 19:52 Documents
drwx------@ 1749 vn1  staff   55968 Aug 22 08:08 Downloads
drwxr-xr-x    20 vn1  staff     640 Jan  9  2023 IDKwhatthisisfor
drwxr-xr-x     5 vn1  staff     160 Jul  6  2022 Java-Training-Exercises
drwx------@  100 vn1  staff    3200 Feb  2  2023 Library
drwx------     7 vn1  staff     224 Nov  3  2021 Movies
drwx------+    6 vn1  staff     192 Nov  3  2021 Music
drwxr-xr-x     6 vn1  staff     192 Mar 30 14:41 OptixStuff
drwx------+   13 vn1  staff     416 Jun 29 10:57 Pictures
drwxr-xr-x@    3 vn1  staff      96 Jan 19  2023 Postman
drwxr-xr-x     5 vn1  staff     160 Aug  4  2022 Projects
drwxr-xr-x+    4 vn1  staff     128 Nov  3  2021 Public
drwxr-xr-x     2 vn1  staff      64 Jun 14 19:14 REHS
drwxr-xr-x@   15 vn1  staff     480 Jun 29 12:30 SpellSlinger
drwxr-xr-x     2 vn1  staff      64 Apr 25 13:48 THISSHIT
drwxr-xr-x    11 vn1  staff     352 Nov  4  2022 acedemic-organizer
-rw-r--r--@    1 vn1  staff      38 Feb  5  2023 apitest.py
drwxr-xr-x@   21 vn1  staff     672 Feb 13  2023 cameratest
drwxr-xr-x    14 vn1  staff     448 Aug 18 09:47 darkhallways
-rw-r--r--     1 vn1  staff      14 Feb 13  2023 death.txt
drwxr-xr-x     6 vn1  staff     192 Sep 25  2022 discBots
drwxr-xr-x    21 vn1  staff     672 Jun  1 22:34 doItAllBackend
drwxr-xr-x    19 vn1  staff     608 Sep 24  2022 flask
drwxr-xr-x    20 vn1  staff     640 Oct  2  2022 flask_portfolio
drwxr-xr-x     4 vn1  staff     128 Sep 24  2022 flask_portfolio_nvarap
drwxr-xr-x    22 vn1  staff     704 Feb 24 20:58 freshflask
-rw-r--r--     1 vn1  staff     237 Mar 13 15:19 fuck.txt
drwxr-xr-x    11 vn1  staff     352 Mar 20 22:48 fuckMyLife
drwxr-xr-x     9 vn1  staff     288 Dec 11  2022 gethinsome
drwxr-xr-x    17 vn1  staff     544 Sep  2  2022 getting-started
-rw-r--r--@    1 vn1  staff     838 Jan 13  2023 goddamn.ipynb
drwxr-xr-x    19 vn1  staff     608 Jun  1 22:33 groupFlaskProject
drwxr-xr-x     7 vn1  staff     224 May  1 17:58 hardyweinburg
drwxr-xr-x     4 vn1  staff     128 Sep 25  2022 helloworld
drwxr-xr-x     4 vn1  staff     128 Jun 29 12:23 knightmare
drwxr-xr-x     6 vn1  staff     192 Jan 17  2023 mortsshit
drwxr-xr-x     4 vn1  staff     128 May 23 21:24 myblog
drwxr-xr-x    14 vn1  staff     448 Jun  2 00:13 nVarap.github.io
drwxr-xr-x@    3 vn1  staff      96 May 15 13:30 nltk_data
drwxr-xr-x    50 vn1  staff    1600 Feb 10  2023 node_modules
drwxr-xr-x     3 vn1  staff      96 Aug 19  2022 opt
drwxr-xr-x    10 vn1  staff     320 Mar  3 18:01 otherpeoplesshit
-rw-r--r--     1 vn1  staff   37307 May 18 19:55 package-lock.json
-rw-r--r--     1 vn1  staff      82 Feb 10  2023 package.json
drwxr-xr-x     8 vn1  staff     256 Feb 26 20:44 pgkstuff
-rw-r--r--@    1 vn1  staff  116253 May 10 09:56 princeton-review-ap-biology-prep-2023-by-the-princeton-review
drwxr-xr-x@    3 vn1  staff      96 Mar  6 13:09 seaborn-data
drwxr-xr-x     5 vn1  staff     160 Oct 23  2022 seocnd
drwxr-xr-x     4 vn1  staff     128 Jan  8  2023 src
drwxrwxr-x    10 vn1  staff     320 Feb  6  2023 svelte-typescript-app
drwxr-xr-x     8 vn1  staff     256 May 31 18:56 thirdtri
drwxr-xr-x    14 vn1  staff     448 Dec  4  2022 try
drwxr-xr-x     3 vn1  staff      96 May 18 22:01 urhur
drwxr-xr-x     4 vn1  staff     128 Jan 10  2023 vscode
drwxr-xr-x    12 vn1  staff     384 Aug 18 14:59 wekafiles
-rw-r--r--@    1 vn1  staff    2241 Jun 20 14:11 were.py
drwxr-xr-x     3 vn1  staff      96 Jul  3  2022 wpilib

look at config file
cat: config: No such file or directory

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

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?

Version Checker

Using the code before, we can check to see if certain tools we’ve installed work, and if versions are good. This can be update for selection, but this is a simple way of doing this.

# Check tools install
 
array=(python3 pip3 java javac node)
for str in ${array[@]}; do
    echo "version of $str:"
    $str --version
done
version of python3:
Python 3.10.7
version of pip3:
pip 22.3 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
version of java:
java 20.0.2 2023-07-18
Java(TM) SE Runtime Environment (build 20.0.2+9-78)
Java HotSpot(TM) 64-Bit Server VM (build 20.0.2+9-78, mixed mode, sharing)
version of javac:
javac 20.0.2
version of node:
v20.2.0

Auto Git Committer

The code below could be used in order to commit github repositories

# Set your GitHub username and repository name
USERNAME="nVarap"
REPO_NAME="test"
COMMIT_MESSAGE="Automated commit"
BRANCH=main

# Changes to the repository directory
cd /Users/vn1/test

# Adds all changes
git add .

# Commits with a message
git commit -m "$COMMIT_MESSAGE"

# Pushes to GitHub
git push 

[main e71d853] Automated commit
 1 file changed, 1 insertion(+)
 create mode 100644 Hello.md
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 292 bytes | 292.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/nVarap/test.git
   382b08d..e71d853  main -> main

What I learned

I relearned a majority of bash and shell command that I forgot last year. Some issues I face while undergoing this notebook involved