Shell Notebook!
Creating Temporary Variables
cat <<EOF > /tmp/variables.sh
creates a temorary folder and declares that the next commands until the next EOF is gonna be place within the folder as a function.
export project_dir=$HOME/nighthawk
creates a variable called “project_dir” that contains a path to the nighthawk directory.
export project=\$project_dir/justin_2025
creates a variable called “project” that contains a path to this directory (justin_2025). The forward slash “" is an escape character to tell the machine to not expand the variable until everything is done, as it does not exist yet until you declare it.
export project_repo="https://github.com/JustinQ-DNHS/justin_2025.git"
creates a variable called “project_repo” that is just a link to this repository’s GitHub.
EOF
declares the end of the “function”
%%script bash
# Creates a temporary file to store variables
cat <<EOF > /tmp/variables.sh
export project_dir=$HOME/nighthawk # change nighthawk to different name to test your git clone
export project=\$project_dir/portfolio_2025 # change portfolio_2025 to name of project from git clone
export project_repo="https://github.com/nighthawkcoders/portfolio_2025.git" # change to project you created from portfolio_2025 template
EOF
Logging the Variables
source /tmp/variables.sh
references the where to pull the variables from.
echo "Project dir: $project_dir"
is similar to a console.log in JavaScript or a print in Python. It echoes what follows in the console, if you want to declare a variable add a “$variable_name”
%%script bash
# References variables source
source /tmp/variables.sh
# Outputs (Lists variables stated in previous script)
echo "Project dir: $project_dir"
echo "Project: $project"
echo "Repo: $project_repo"
Using Conditional Statements
cd ~
moves the directory to the home, it can go to different directories by changing the “~” to the directory name
if [ ! -d $project_dir ]
then
echo "Directory $project_dir does not exist... making directory $project_dir"
mkdir -p $project_dir
fi
echo "Directory $project_dir exists."
Contains a if statement that checks if the “project_dir” variable directory exists or not. Then if it doesn’t it will send a message in the console saying that it doesn’t exist and creates the directory. The “-p” just creates the entire path if it does not exist. The “fi” ends the if statement and then the it echoes that the directory exists.
%%script bash
source /tmp/variables.sh
echo "Using conditional statement to create a project directory and project"
cd ~ # Moves you to home directory
# Conditional block to make a project directory
if [ ! -d $project_dir ]
then
echo "Directory $project_dir does not exist... making 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 exist... cloning $project_repo"
cd $project_dir
git clone $project_repo
cd ~
fi
echo "Directory $project exists."
Navigating Files
Uses previously explained concepts and introduces the pwd
statement, which displays the path to the directory you are currently in. It also introduces the ls
statement, which displays all files in the current directory.
%%script bash
source tmp/variables.sh
# Takes you to the location of your project variable and lists the path
echo "Navigate to project, then navigate to area wwhere files were cloned"
cd $project
pwd
# Lists the files within the project
echo ""
echo "list top level or root of files with project pulled from github"
ls
ls
Enhanced Behavior
-a
: Lists all files, including hidden files-l
: List in long format-h
: Human-readable file sizes (Converts into more readable units when used with -l)-t
: Sort by modification-time-R
: Reverse the order of the sort
%%script bash
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
%%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 -lR # list posts recursively
%%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 -lR # list notebooks recursively
%%script bash
# Extract saved variables
source /tmp/variables.sh
echo "Look for images, print working directory, list files"
cd $project/images # this should exist per fastpages
pwd
ls -lR
Reading Files from Terminal
Everything has been explained previously but now we get to go into what cat
does. cat
allows you to view the contents of a file. Generally only works for “.txt” files and code files such as “.py”, “.md” or “.html” files.
%%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"
Environment Settings
env
lists all shell settings
%%script bash
echo "Show the shell environment variables, key on left of equal value on right"
echo ""
# Shows shell settings
env
Setting Files
This line of code goes into the “config.yml” file and reveals the settings by using the “cat” line. This sums up most of the ideas behind using the terminal and shell commands.
%%script bash
source /tmp/variables.sh
cd $project
echo ""
echo "show the secrets of .git config file"
cd .git
ls -l config
echo ""
echo "look at config file"
cat config