Prerequisites for Bash on Jupyter

Being able to find a Bash Reference. Here is a Bash Cheat Sheet Install the Bash Kernel for Jupyter notebooks. Expectations are pip3 and python3 are defined within the path. The bash shell allowed construction of this tutorial.

  • pip install bash_kernel
  • python -m bash_kernel.install

Selecting a kernel in "code --project--" or "jupyter notebook --file.ipynb--" that have bash in options.

Prerequisites for Project Directory

Practice pulling code from git to your machine. This will create a project directory and add APCSP github project to the directory. There is conditional logic to make sure that directory and pull only happen if it does not (!) exist.

  • Hack:Change variables, make new directory, clone to that directory.
echo "Using conditional statement to create a project directory and project"

# Variable section
export project_dir=$HOME/vscode  # change vscode to different name to test git clone
export project=$project_dir/APCSP  # change APCSP to name of project from git clone
export project_repo="https://github.com/nighthawkcoders/APCSP.git"  # change to project of choice

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/haoxuan/vscode exists.
Directory /home/haoxuan/vscode/APCSP exists.

Look inside Fastpages/Github page project

All computers contain files and directories. The clone brought more files from cloud to your machine. Using the bash shell you will see some commands that show and interact with files and directories.

  • "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
ls
 Volume in drive C is Windows
 Volume Serial Number is A6D3-D43E

 Directory of c:\AAA\coding\Visual Studio\fastproject2\FastProject2\_notebooks

08/29/2022  02:53 PM    <DIR>          .
08/29/2022  12:08 AM    <DIR>          ..
08/23/2022  11:51 PM         3,162,569 2020-02-20-test.ipynb
08/30/2022  02:36 PM             9,754 2022-08-22-bash_test.ipynb
08/30/2022  02:36 PM             1,195 2022-08-23-new-notebook.ipynb
08/29/2022  02:47 PM             3,179 2022-08-27-quiz.ipynb
08/23/2022  11:41 PM    <DIR>          ghtop_images
08/23/2022  11:41 PM    <DIR>          my_icons
08/23/2022  11:41 PM               784 README.md
               5 File(s)      3,177,481 bytes
               4 Dir(s)  344,116,977,664 bytes free
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

echo ""
echo "list again with hidden files pulled from github"
ls -a   # hidden files flag, many shell commands have flags

echo ""
echo "list all files in long format"
ls -al   # all files and long listing
Navigate to project, then navigate to area wwhere files were cloned
/home/haoxuan

list top level or root of files with project pulled from github
anaconda3  vscode

list again with hidden files pulled from github
.              .cache      .local                     .vscode-server
..             .conda      .motd_shown                .wget-hsts
.aws           .docker     .node-gyp                  anaconda3
.azure         .gitconfig  .npm                       vscode
.bash_history  .ipython    .profile
.bash_logout   .jupyter    .sudo_as_admin_successful
.bashrc        .landscape  .vscode-remote-containers

list all files in long format
total 96
drwxr-xr-x 15 haoxuan haoxuan 4096 Oct 10 15:19 .
drwxr-xr-x  3 root    root    4096 Aug 23 23:16 ..
lrwxrwxrwx  1 haoxuan haoxuan   23 Sep 20 01:15 .aws -> /mnt/c/Users/haoxu/.aws
lrwxrwxrwx  1 haoxuan haoxuan   25 Sep 20 01:15 .azure -> /mnt/c/Users/haoxu/.azure
-rw-------  1 haoxuan haoxuan 9611 Oct 13 19:01 .bash_history
-rw-r--r--  1 haoxuan haoxuan  220 Aug 23 23:16 .bash_logout
-rw-r--r--  1 haoxuan haoxuan 4254 Aug 30 14:47 .bashrc
drwxr-xr-x  6 haoxuan haoxuan 4096 Sep 22 14:59 .cache
drwxr-xr-x  2 haoxuan haoxuan 4096 Aug 30 14:46 .conda
drwxr-xr-x  5 haoxuan haoxuan 4096 Sep 20 14:29 .docker
-rw-r--r--  1 haoxuan haoxuan   59 Sep 21 01:32 .gitconfig
drwxr-xr-x  3 haoxuan haoxuan 4096 Aug 30 14:57 .ipython
drwxr-xr-x  2 haoxuan haoxuan 4096 Aug 30 14:59 .jupyter
drwxr-xr-x  2 haoxuan haoxuan 4096 Aug 23 23:16 .landscape
drwxr-xr-x  5 haoxuan haoxuan 4096 Oct  3 14:45 .local
-rw-r--r--  1 haoxuan haoxuan    0 Oct 15 14:09 .motd_shown
drwxr-xr-x  3 haoxuan haoxuan 4096 Sep 21 15:15 .node-gyp
drwxr-xr-x 13 haoxuan haoxuan 4096 Sep 22 14:59 .npm
-rw-r--r--  1 haoxuan haoxuan  807 Aug 23 23:16 .profile
-rw-r--r--  1 haoxuan haoxuan    0 Sep 20 14:25 .sudo_as_admin_successful
drwxr-xr-x  3 haoxuan haoxuan 4096 Oct 10 15:19 .vscode-remote-containers
drwxr-xr-x  5 haoxuan haoxuan 4096 Aug 30 14:52 .vscode-server
-rw-r--r--  1 haoxuan haoxuan  183 Oct 12 15:29 .wget-hsts
drwxr-xr-x 28 haoxuan haoxuan 4096 Aug 30 15:01 anaconda3
drwxr-xr-x  9 haoxuan haoxuan 4096 Oct 12 15:01 vscode
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
bash: cd: /_posts: No such file or directory
/home/haoxuan
total 8
drwxr-xr-x 28 haoxuan haoxuan 4096 Aug 30 15:01 anaconda3
drwxr-xr-x  9 haoxuan haoxuan 4096 Oct 12 15:01 vscode
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
bash: cd: /_notebooks: No such file or directory
/home/haoxuan
total 8
drwxr-xr-x 28 haoxuan haoxuan 4096 Aug 30 15:01 anaconda3
drwxr-xr-x  9 haoxuan haoxuan 4096 Oct 12 15:01 vscode
echo "Look for images in notebooks, print working directory, list files"
cd $notebooks/images  # this should exist per fastpages
pwd
ls -l
Look for images in notebooks, print working directory, list files
bash: cd: /_notebooks/images: No such file or directory
/home/haoxuan
total 8
drwxr-xr-x 28 haoxuan haoxuan 4096 Aug 30 15:01 anaconda3
drwxr-xr-x  9 haoxuan haoxuan 4096 Oct 12 15:01 vscode

Look inside a Markdown File

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

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

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 hav relationship 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.
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
CONDA_EXE=/home/haoxuan/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=LAPTOP-V9GL5S0O
PWD=/home/haoxuan
LOGNAME=haoxuan
CONDA_PREFIX=/home/haoxuan/anaconda3
MOTD_SHOWN=update-motd
HOME=/home/haoxuan
LANG=C.UTF-8
WSL_INTEROP=/run/WSL/11_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:*.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
CONDA_PROMPT_MODIFIER=(base) 
PYDEVD_USE_FRAME_EVAL=NO
posts=/_posts
LESSCLOSE=/usr/bin/lesspipe %s %s
VSCODE_HANDLES_SIGPIPE=true
TERM=xterm-256color
_CE_CONDA=
LESSOPEN=| /usr/bin/lesspipe %s
USER=haoxuan
PYTHONIOENCODING=utf-8
notebooks=/_notebooks
CONDA_SHLVL=1
DISPLAY=:0
SHLVL=1
PAGER=cat
VSCODE_CWD=/mnt/c/AAA/coding/Visual Studio/Microsoft VS Code
CONDA_PYTHON_EXE=/home/haoxuan/anaconda3/bin/python
XDG_RUNTIME_DIR=/mnt/wslg/runtime-dir
PS1=[PEXP\[\]ECT_PROMPT>
CONDA_DEFAULT_ENV=base
WSLENV=VSCODE_WSL_EXT_LOCATION/up
VSCODE_WSL_EXT_LOCATION=/mnt/c/Users/haoxu/.vscode/extensions/ms-vscode-remote.remote-wsl-0.72.0
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/home/haoxuan/.vscode-server/bin/129500ee4c8ab7263461ffe327268ba56b9f210d/bin/remote-cli:/home/haoxuan/.local/bin:/home/haoxuan/anaconda3/bin:/home/haoxuan/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/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0:/mnt/c/WINDOWS/System32/OpenSSH:/mnt/c/Program Files/Intel/WiFi/bin:/mnt/c/Program Files/Common Files/Intel/WirelessCommon:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/haoxu/AppData/Local/Programs/Python/Python310/Scripts:/mnt/c/Users/haoxu/AppData/Local/Programs/Python/Python310:/mnt/c/Users/haoxu/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/haoxu/AppData/Local/JetBrains/PyCharm Community Edition 2019.2.3/bin:/mnt/c/AAA/coding/Visual Studio/Microsoft VS Code/bin:/mnt/c/Users/haoxu/AppData/Local/Programs/Git/cmd:/snap/bin
VSCODE_NLS_CONFIG={"locale":"en","availableLanguages":{}}
HOSTTYPE=x86_64
PULSE_SERVER=/mnt/wslg/PulseServer
VSCODE_HANDLES_UNCAUGHT_ERRORS=true
OLDPWD=/home/haoxuan
VSCODE_IPC_HOOK_CLI=/mnt/wslg/runtime-dir/vscode-ipc-c6bbfe09-49c2-4921-95fa-d98203437b83.sock
_=/usr/bin/env
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
bash: cd: .git: No such file or directory
total 8
drwxr-xr-x 28 haoxuan haoxuan 4096 Aug 30 15:01 anaconda3
drwxr-xr-x  9 haoxuan haoxuan 4096 Oct 12 15:01 vscode

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

Hacks

Go back to some of the deployment procedures and think about some thing you could verify through Bash notebook.

  • Is there anything we use to verify tools we install? Think about versions.
  • Is there anything we could verify with Anaconda?
  • How would you update a repository?
  • Really cool would be automating a procedure from installation.