Tools
Seeing if tools are installed.
- Basic commands
- Variables
- Python versions
- Playing with some Regex
- Jupyter Check
- Conda check for Jupyter
- Directories
- Hacks
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
text="Hello World!"
echo "$text"
Note: Single and double quotes are different!
See example below
text="Hello World!"
echo "$text"
echo '$text'
export
do?
What does 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 --version
python2 --version
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
jupyter --version
jupyter kernelspec list
conda list | grep jupyter
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
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
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
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])+$"
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
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