Basic commands

ls: Lists the contents in the current directory

cd [file path]: Move to a directory

pwd: List your current directory

echo "Moving to home directory with the cd command"
cd 
echo "What directory am I in?"
pwd 
echo "Moving to my vscode directory"
cd /home/lwu1822/vscode
echo "What directory am I in?"
pwd
echo "What files are within this directory?"
ls
Moving to home directory with the cd command
What directory am I in?
/home/lwu1822
Moving to my vscode directory
What directory am I in?
/home/lwu1822/vscode
What files are within this directory?
APCSA  APCSP  fastpages  spring_portfolio  test

Variables

To create variables, simply assign a value to a variable name.

Ex: x=5

Reference variables with $[var]

text="Hello World!"
echo "$text"
Hello World!

Note: Single and double quotes are different!

See example below

text="Hello World!"
echo "$text"
echo '$text'
Hello World!
$text


What does export do?

While going through the Bash Tutorial, I noticed that variables were declared with the export command, ex: export x=5.

I wondered what the difference was between export, and straight up declaring the variable, like this: x=5.

The answer is simple. Quoting from Super User (a community in Stack Exchange), "Exported variables get passed on to child processes, not-exported variables do not."

Python versions

python --version
python2 --version
Python 3.9.12
Python 2.7.18

Playing with some Regex

echo "Check for packages starting with 'j' and ends with the letter 's'"
# What this bash script does:
# Checks if there is output after grep (searching) for packages starting
# with 'j' and ending with 's'

# First lists all conda packages, then filters to only show the package
# names (excludes Version and Build Channel) with the cut command

# The grep command uses some fun Regex
# ^ means that the character following it (in parenthesis) must start at the
# beginning
# .*: "."" means any character, "*" means match the character before it
# any number of times -> ".*" means match any character any number of times
# $: Character before it (s) must be at the end
if conda list | cut -d " " -f 1 | grep "^j.*s$" ; then
    :
else
    echo "No packages starting with 'j' and ending with 's'"
fi
Check for packages starting with 'j' and ends with the letter 's'
jupyterlab_pygments
jupyterlab_widgets

Jupyter Check

jupyter --version
jupyter kernelspec list
Selected Jupyter core packages...
IPython          : 8.2.0
ipykernel        : 6.9.1
ipywidgets       : 7.6.5
jupyter_client   : 6.1.12
jupyter_core     : 4.9.2
jupyter_server   : 1.13.5
jupyterlab       : 3.3.2
nbclient         : 0.5.13
nbconvert        : 6.4.4
nbformat         : 5.3.0
notebook         : 6.4.8
qtconsole        : 5.3.0
traitlets        : 5.1.1
Available kernels:
  bash          /home/lwu1822/.local/share/jupyter/kernels/bash
  java          /home/lwu1822/.local/share/jupyter/kernels/java
  javascript    /home/lwu1822/.local/share/jupyter/kernels/javascript
  python3       /home/lwu1822/anaconda3/share/jupyter/kernels/python3

Conda check for Jupyter

conda list | grep jupyter
jupyter                   1.0.0            py39h06a4308_7  
jupyter_client            6.1.12             pyhd3eb1b0_0  
jupyter_console           6.4.0              pyhd3eb1b0_0  
jupyter_core              4.9.2            py39h06a4308_0  
jupyter_server            1.13.5             pyhd3eb1b0_0  
jupyterlab                3.3.2              pyhd3eb1b0_0  
jupyterlab_pygments       0.1.2                      py_0  
jupyterlab_server         2.10.3             pyhd3eb1b0_1  
jupyterlab_widgets        1.0.0              pyhd3eb1b0_1  
(I have checked Slack :))
test="python3" # keyword
check=`jupyter kernelspec list | grep $test` # run command
n=${#check} # determine length
if [[ ${n} > 0 ]];  # testt length
then # greater than zero
    echo "$check"
else # less than zero
    echo "$check"
fi
  python3       /home/lwu1822/anaconda3/share/jupyter/kernels/python3


Directories

Check if the APCSP repo exists in /home/lwu1822/vscode/hack. If not, clone the APCSP repo into the directory.

cd /home/lwu1822/vscode/hack

if [ ! -d APCSP ] ; then 
    echo "APCSP repo does not exist in this directory"
    echo "Cloning APCSP repo"
    git clone "https://github.com/nighthawkcoders/APCSP.git"
else 
    echo "APCSP repo does exist in this directory"
fi
APCSP repo does not exist in this directory
Cloning APCSP repo
Cloning into 'APCSP'...
remote: Enumerating objects: 10655, done.
remote: Counting objects: 100% (2230/2230), done.
remote: Compressing objects: 100% (774/774), done.
remote: Total 10655 (delta 1382), reused 2178 (delta 1331), pack-reused 8425
Receiving objects: 100% (10655/10655), 17.37 MiB | 2.26 MiB/s, done.
Resolving deltas: 100% (5877/5877), done.


Check again to see if APCSP directory exists.

cd /home/lwu1822/vscode/hack

echo "Listing /home/lwu1822/vscode/hack"
ls 

echo "Does APCSP repo exist in this directory?"

if [ ! -d APCSP ] ; then 
    echo "APCSP repo does not exist in this directory"
    echo "Cloning APCSP repo"
    git clone "https://github.com/nighthawkcoders/APCSP.git"
else 
    echo "APCSP repo does exist in this directory"
fi
Listing /home/lwu1822/vscode/hack
APCSP  foo.sh  test.sh
Does APCSP repo exist in this directory?
APCSP repo does exist in this directory


Hacks

To verify tools:

For many packages, you can type the name of the package and then --version, or you can grep from the dpkg -l list

python --version
dpkg -l | cut -d " " -f 3 | grep -E "^(python)([0-9])+$"
Python 3.9.12
python2
python3

To verify Conda, you can use the conda list command. I'm not going to do this here because the output is huge, but you can see above where I used regex and grep to search for the specific things I want


To update a repository, first move into the directory of your repository, and update with git pull origin master.

main_dir=vscode/
fastpages_dir=CSP-fastpages/
cd 
cd $main_dir/$fastpages_dir
ls
# git pull origin master
2022-05-15-07-21-16.png  README.md        _notebooks  assets
2022-05-15-07-24-25.png  _action_files    _pages      docker-compose.yml
2022-05-15-07-25-15.png  _config.yml      _plugins    images
Gemfile                  _fastpages_docs  _posts      index.html
LICENSE                  _includes        _sass
Makefile                 _layouts         _word


Automation

I'm not going to run the code cell, because Bash input doesn't work in Jupyter Notebook for some reason. I'll attach a picture though, to show that it works

echo "What package would you like to check? Make sure to type out the full exact name of the package, as it would show in dpkg -l output"

read pkg 


if dpkg -l | grep $pkg  >/dev/null 2>&1 ; then 
    echo "Package is installed!"
else 
    echo "Would you like to install the package? y/n"
    read install

    if [ $install == "y" ] ; then 
        echo "Installing"
        apt-get install -y $pkg 
    else 
        echo "Alright then!"
    fi 
fi