From 08310d3624e7a842a42da029ffb088da0ab2cfba Mon Sep 17 00:00:00 2001 From: michael Date: Fri, 18 Aug 2023 14:40:37 +0100 Subject: [PATCH 1/6] Unix: add uninstall.sh; update install.sh to create and add variables to m269 config file --- install.sh | 34 ++++++++++++++------ uninstall.sh | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 10 deletions(-) create mode 100755 uninstall.sh diff --git a/install.sh b/install.sh index e90108e..c900bb5 100755 --- a/install.sh +++ b/install.sh @@ -21,6 +21,7 @@ CHECK="allowed.py m269.json" FILES="$CSS $REQS $CHECK" COURSE=m269-23j VENV=~/venvs/$COURSE +CONFIG_VARS=("VENV" "COURSE") # find out under which shell this script is running parent_shell=$(ps -o command $PPID) @@ -120,6 +121,8 @@ else cp -a $CHECK "$FOLDER" fi +CONFIG_VARS+=("FOLDER") + echo "Creating Python environment $VENV... (this will take a bit)" python3.10 -m venv --prompt $COURSE $VENV @@ -139,25 +142,36 @@ ALLOWED="python3.10 \"$FOLDER/allowed.py\" -c \"$FOLDER/m269.json\"" if [ $shell = "fish" ] then - FILE=~/.config/fish/config.fish + SHELL_CONFIG_FILE=~/.config/fish/config.fish else - FILE=~/.${shell}rc + SHELL_CONFIG_FILE=~/.${shell}rc fi +CONFIG_VARS+=("SHELL_CONFIG_FILE") + if [ $shell = "csh" ] || [ $shell = "tcsh" ] then - echo "alias $COURSE '$M269.csh'" >> $FILE - echo "alias nb '$NB'" >> $FILE - echo "alias allowed '$ALLOWED'" >> $FILE + echo "alias $COURSE '$M269.csh'" >> $SHELL_CONFIG_FILE + echo "alias nb '$NB'" >> $SHELL_CONFIG_FILE + echo "alias allowed '$ALLOWED'" >> $SHELL_CONFIG_FILE else if [ $shell = "fish" ] then - echo "alias $COURSE='$M269.fish'" >> $FILE + echo "alias $COURSE='$M269.fish'" >> $SHELL_CONFIG_FILE else - echo "alias $COURSE='$M269'" >> $FILE + echo "alias $COURSE='$M269'" >> $SHELL_CONFIG_FILE fi - echo "alias nb='$NB'" >> $FILE - echo "alias allowed='$ALLOWED'" >> $FILE + echo "alias nb='$NB'" >> $SHELL_CONFIG_FILE + echo "alias allowed='$ALLOWED'" >> $SHELL_CONFIG_FILE fi -echo "All done. Go to $SITE for further instructions." \ No newline at end of file +M269_CONFIG_FILE=$FOLDER/.m269rc +CONFIG_VARS+=("M269_CONFIG_FILE") + +# Write the name=value pairs to m269 config file +for var_name in "${CONFIG_VARS[@]}"; do + var_value="${!var_name}" + echo "$var_name=$var_value" >> "$M269_CONFIG_FILE" +done + +echo "All done. Go to $SITE for further instructions." diff --git a/uninstall.sh b/uninstall.sh new file mode 100755 index 0000000..4d8b52c --- /dev/null +++ b/uninstall.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +BOLD="\033[1m" +NORMAL="\033[0m" + +confirm() { + local message="$1" + echo -en "${BOLD}:: $message [y/N] ${NORMAL}" + read -r response + case "$response" in + [yY]) + return 0 # true + ;; + *) + return 1 # false + ;; + esac +} + +# Confirm uninstallation +echo "Warning: After uninstallation, you will need a Jupyter environment to open and run your notebooks." +if ! confirm "Proceed with uninstallation?"; then + exit 1 +fi + +# Assume variables VENV, COURSE, FOLDER, SHELL_CONFIG_FILE, +# will be in the M269 config file +if [ -e .m269rc ]; then + source .m269rc +else + echo "Failed to source the M269 configuration file " + exit 1 +fi +CSS_FILE=~/.jupyter/custom/custom.css +COURSE_YEAR="${COURSE:5:2}" + +# Remove the virtual environment +remove_venv() { + test -n "$VENV" || return 1 + test -d "$VENV" || return 1 + # Check for existence of typical virtual environment files + test -f "$VENV/bin/activate" || return 1 + test -f "$VENV/pyvenv.cfg" || return 1 + echo "Removing the virtual environment..." + rm -r "$VENV" +} +if confirm "Remove the $COURSE virtual environment?"; then + remove_venv || echo "Warning: failed to remove the virtual environment." +fi + +# Remove allowed.py and m269.json from M269 folder +if confirm "Remove allowed.py and m269.json from $FOLDER ?"; then + for file in "allowed.py" "m269.json"; do + target="$FOLDER/$file" + if [ -e "$target" ]; then + echo "Removing $file from $FOLDER..." + rm "$target" + else + echo "Warning: $target does not exist." + fi + done +fi + +# Remove aliases from the configuration file +if confirm "Remove shortcut commands from $SHELL_CONFIG_FILE ?"; then + ALIASES=("alias nb" "alias allowed" "alias $COURSE") + if [ -e $SHELL_CONFIG_FILE ]; then + echo "Removing shortcut commands from $SHELL_CONFIG_FILE..." + cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup + for alias in "${ALIASES[@]}"; do + # Delete lines that start with $alias and contain current course "code" + sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" + done + fi +fi + +# Remove lines from ~/.jupyter/custom/custom.css +if confirm "Remove M269 custom styling from $CSS_FILE ?"; then + # Special characters need to be escaped for use in regex + START_DELIM="\/\* Start of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" + END_DELIM="\/\* End of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" + if [ -e $CSS_FILE ]; then + cp "$CSS_FILE" "$CSS_FILE".backup + echo "Removing M269 custom styling from $CSS_FILE..." + sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" + fi + +fi + +echo "The uninstallation process has now completed." From 3a98edbf9139edddbfe777fe4cf75553b55718c2 Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 24 Aug 2023 00:31:04 +0100 Subject: [PATCH 2/6] Update uninstall.sh and install.sh as per Michel's comments and suggestions --- install.sh | 30 ++++++++-------- uninstall.sh | 100 ++++++++++++++++++++++++++++----------------------- 2 files changed, 71 insertions(+), 59 deletions(-) diff --git a/install.sh b/install.sh index c900bb5..1bb0149 100755 --- a/install.sh +++ b/install.sh @@ -21,7 +21,7 @@ CHECK="allowed.py m269.json" FILES="$CSS $REQS $CHECK" COURSE=m269-23j VENV=~/venvs/$COURSE -CONFIG_VARS=("VENV" "COURSE") +UNINSTALL=uninstall.sh # find out under which shell this script is running parent_shell=$(ps -o command $PPID) @@ -88,7 +88,8 @@ then echo "Downloading and installing M269 files..." for file in $FILES do - curl -LO https://github.com/dsa-ou/m269-installer/raw/main/$file + # WARNING: CHANGE URL BACK TO MAIN BRANCH BEFORE MERGING!!! + curl -LO https://github.com/dsa-ou/m269-installer/raw/14-create-uninstallation-scripts/$file done mkdir -p ~/.jupyter/custom # don't overwrite existing CSS file @@ -118,11 +119,9 @@ else else cp -a $CSS ~/.jupyter/custom fi - cp -a $CHECK "$FOLDER" + cp -a $CHECK $UNINSTALL "$FOLDER" fi -CONFIG_VARS+=("FOLDER") - echo "Creating Python environment $VENV... (this will take a bit)" python3.10 -m venv --prompt $COURSE $VENV @@ -147,8 +146,6 @@ else SHELL_CONFIG_FILE=~/.${shell}rc fi -CONFIG_VARS+=("SHELL_CONFIG_FILE") - if [ $shell = "csh" ] || [ $shell = "tcsh" ] then echo "alias $COURSE '$M269.csh'" >> $SHELL_CONFIG_FILE @@ -165,13 +162,16 @@ else echo "alias allowed='$ALLOWED'" >> $SHELL_CONFIG_FILE fi -M269_CONFIG_FILE=$FOLDER/.m269rc -CONFIG_VARS+=("M269_CONFIG_FILE") - -# Write the name=value pairs to m269 config file -for var_name in "${CONFIG_VARS[@]}"; do - var_value="${!var_name}" - echo "$var_name=$var_value" >> "$M269_CONFIG_FILE" -done +# Set variables in uninstall.sh +if [[ -f "$FOLDER/$UNINSTALL" ]] +then + echo "Setting variables into uninstall.sh ..." + sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL" + sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" + chmod +x "$FOLDER/$UNINSTALL" +else + echo "Warning: $FOLDER/uninstall.sh does not exist." + echo "critical Variables have not been set in uninstall.sh." +fi echo "All done. Go to $SITE for further instructions." diff --git a/uninstall.sh b/uninstall.sh index 4d8b52c..048cbb1 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -1,40 +1,55 @@ #!/bin/bash +# This script performs the uninstallation of the M269 course environment +# and will do the following: +# - Seeks user confirmation before proceeding. +# - Deletes the M269 virtual environment. +# - Erases course-specific aliases from the shell config file. +# - Removes files related to `allowed` from the M269 folder. +# - Optionally clears M269 styling from Jupyter's custom.css. + +# Note: this script expects FOLDER and SHELL_CONFIG_FILE to be set during the +# installation process (inserted with sed via install.sh). + +COURSE=m269-23j +VENV=~/venvs/$COURSE +CSS_FILE=~/.jupyter/custom/custom.css +COURSE_YEAR=23 +SITE=https://dsa-ou.github.io/m269-installer + +# Check FOLDER and SHELL_CONFIG_FILE have been set i.e inserted via install.sh +if [[ -z "$FOLDER" ]] || [[ -z "$SHELL_CONFIG_FILE" ]]; then + echo "Error: critical variables have not been set" + exit 1 +fi + BOLD="\033[1m" NORMAL="\033[0m" - confirm() { local message="$1" echo -en "${BOLD}:: $message [y/N] ${NORMAL}" read -r response case "$response" in [yY]) - return 0 # true + return 0 ;; *) - return 1 # false + return 1 ;; esac } # Confirm uninstallation -echo "Warning: After uninstallation, you will need a Jupyter environment to open and run your notebooks." +echo "Warning: This script will make the following changes: " +echo " - Remove the virtual environment in $VENV." +echo " - Remove the aliases 'nb', 'allowed' and '$COURSE' from $SHELL_CONFIG_FILE" +echo " - Optionally removes M269 custom styling from $CSS_FILE" +echo "As a result, Jupyter notebooks will no longer be readable or executable unless another Jupyter environment exists." if ! confirm "Proceed with uninstallation?"; then - exit 1 -fi - -# Assume variables VENV, COURSE, FOLDER, SHELL_CONFIG_FILE, -# will be in the M269 config file -if [ -e .m269rc ]; then - source .m269rc -else - echo "Failed to source the M269 configuration file " - exit 1 + exit 0 fi -CSS_FILE=~/.jupyter/custom/custom.css -COURSE_YEAR="${COURSE:5:2}" -# Remove the virtual environment +# Verify and remove the virtual environment. remove_venv() { test -n "$VENV" || return 1 test -d "$VENV" || return 1 @@ -44,34 +59,30 @@ remove_venv() { echo "Removing the virtual environment..." rm -r "$VENV" } -if confirm "Remove the $COURSE virtual environment?"; then - remove_venv || echo "Warning: failed to remove the virtual environment." -fi +remove_venv || { echo "Error: failed to remove the virtual environment."; exit 1; } # Remove allowed.py and m269.json from M269 folder -if confirm "Remove allowed.py and m269.json from $FOLDER ?"; then - for file in "allowed.py" "m269.json"; do - target="$FOLDER/$file" - if [ -e "$target" ]; then - echo "Removing $file from $FOLDER..." - rm "$target" - else - echo "Warning: $target does not exist." - fi - done -fi +for file in "allowed.py" "m269.json"; do + target="$FOLDER/$file" + if [[ -f "$target" ]]; then + echo "Removing $file from $FOLDER..." + rm "$target" + else + echo "Warning: $target does not exist." + fi +done # Remove aliases from the configuration file -if confirm "Remove shortcut commands from $SHELL_CONFIG_FILE ?"; then - ALIASES=("alias nb" "alias allowed" "alias $COURSE") - if [ -e $SHELL_CONFIG_FILE ]; then - echo "Removing shortcut commands from $SHELL_CONFIG_FILE..." - cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup - for alias in "${ALIASES[@]}"; do - # Delete lines that start with $alias and contain current course "code" - sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" - done - fi +ALIASES=("alias nb" "alias allowed" "alias $COURSE") +if [[ -f "$SHELL_CONFIG_FILE" ]]; then + echo "Removing shortcut commands from $SHELL_CONFIG_FILE..." + cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup + for alias in "${ALIASES[@]}"; do + # Delete lines that start with $alias and contain current course "code" + sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" + done +else + echo "Warning: $SHELL_CONFIG_FILE does not exist." fi # Remove lines from ~/.jupyter/custom/custom.css @@ -79,12 +90,13 @@ if confirm "Remove M269 custom styling from $CSS_FILE ?"; then # Special characters need to be escaped for use in regex START_DELIM="\/\* Start of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" END_DELIM="\/\* End of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" - if [ -e $CSS_FILE ]; then + if [[ -f "$CSS_FILE" ]]; then cp "$CSS_FILE" "$CSS_FILE".backup echo "Removing M269 custom styling from $CSS_FILE..." sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" + else + echo "Warning: $CSS_FILE does not exist." fi - fi -echo "The uninstallation process has now completed." +echo "All done. To reinstall please visit $SITE and follow the provided instructions." From d7b673aa6639399314dc5faab97554b3a3bba3ce Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 24 Aug 2023 20:21:12 +0100 Subject: [PATCH 3/6] Update install.sh: make sure uninstall.sh is in the M269 folder; add UNINSTALL to FILES --- install.sh | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/install.sh b/install.sh index 1bb0149..2cf7b66 100755 --- a/install.sh +++ b/install.sh @@ -18,10 +18,10 @@ DOC="See $SITE for details." CSS=custom.css REQS=requirements.txt CHECK="allowed.py m269.json" -FILES="$CSS $REQS $CHECK" +UNINSTALL=uninstall.sh +FILES="$CSS $REQS $CHECK $UNINSTALL" COURSE=m269-23j VENV=~/venvs/$COURSE -UNINSTALL=uninstall.sh # find out under which shell this script is running parent_shell=$(ps -o command $PPID) @@ -90,6 +90,11 @@ then do # WARNING: CHANGE URL BACK TO MAIN BRANCH BEFORE MERGING!!! curl -LO https://github.com/dsa-ou/m269-installer/raw/14-create-uninstallation-scripts/$file + if [ $? -ne 0 ] + then + echo "Failed to download $file" + exit 1 + fi done mkdir -p ~/.jupyter/custom # don't overwrite existing CSS file @@ -120,6 +125,11 @@ else cp -a $CSS ~/.jupyter/custom fi cp -a $CHECK $UNINSTALL "$FOLDER" + if [ $? -ne 0 ] + then + echo "Failed to copy $CHECK and $UNINSTALL" + exit 1 + fi fi echo "Creating Python environment $VENV... (this will take a bit)" @@ -163,15 +173,9 @@ else fi # Set variables in uninstall.sh -if [[ -f "$FOLDER/$UNINSTALL" ]] -then - echo "Setting variables into uninstall.sh ..." - sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL" - sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" - chmod +x "$FOLDER/$UNINSTALL" -else - echo "Warning: $FOLDER/uninstall.sh does not exist." - echo "critical Variables have not been set in uninstall.sh." -fi +echo "Setting variables into uninstall.sh ..." +sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL" +sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" +chmod +x "$FOLDER/$UNINSTALL" echo "All done. Go to $SITE for further instructions." From d3566d5c12561b91d91319a0d93a1e2613fc1c2a Mon Sep 17 00:00:00 2001 From: michael Date: Fri, 25 Aug 2023 15:31:33 +0100 Subject: [PATCH 4/6] Add different syntax for sed on macOS --- install.sh | 12 ++++++++++-- uninstall.sh | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/install.sh b/install.sh index 2cf7b66..186b67c 100755 --- a/install.sh +++ b/install.sh @@ -173,9 +173,17 @@ else fi # Set variables in uninstall.sh +# Mac os uses BSD version of sed so needs different syntax. echo "Setting variables into uninstall.sh ..." -sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL" -sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" +if [[ "$OSTYPE" == "linux-gnu"* ]]; then + sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL" + sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" +elif [[ "$OSTYPE" == "darwin"* ]]; then + sed -i "" -e "14i\\" -e "FOLDER=$FOLDER" "$FOLDER/$UNINSTALL" + sed -i "" -e "15i\\" -e "SHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" +else + echo "Warning: unknown OS, unable to set variables into uninstall.sh" +fi chmod +x "$FOLDER/$UNINSTALL" echo "All done. Go to $SITE for further instructions." diff --git a/uninstall.sh b/uninstall.sh index 048cbb1..acf1f91 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -79,7 +79,13 @@ if [[ -f "$SHELL_CONFIG_FILE" ]]; then cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup for alias in "${ALIASES[@]}"; do # Delete lines that start with $alias and contain current course "code" - sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" + elif [[ "$OSTYPE" == "darwin"* ]]; then + sed -i "" "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" + else + echo "Warning: unknown OS, skipping alias removal." + fi done else echo "Warning: $SHELL_CONFIG_FILE does not exist." @@ -87,13 +93,18 @@ fi # Remove lines from ~/.jupyter/custom/custom.css if confirm "Remove M269 custom styling from $CSS_FILE ?"; then - # Special characters need to be escaped for use in regex START_DELIM="\/\* Start of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" END_DELIM="\/\* End of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" if [[ -f "$CSS_FILE" ]]; then cp "$CSS_FILE" "$CSS_FILE".backup echo "Removing M269 custom styling from $CSS_FILE..." - sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" + elif [[ "$OSTYPE" == "darwin"* ]]; then + sed -i "" "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" + else + echo "Warning: unknown OS, skipping custom styling removal." + fi else echo "Warning: $CSS_FILE does not exist." fi From 686fa7dad97a150ed23e033148dc078579469f60 Mon Sep 17 00:00:00 2001 From: michael Date: Fri, 25 Aug 2023 21:21:19 +0100 Subject: [PATCH 5/6] Fix nb alias removal: alias no longer contained course code --- install.sh | 8 +++++--- uninstall.sh | 26 ++++++++++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/install.sh b/install.sh index 186b67c..efbd027 100755 --- a/install.sh +++ b/install.sh @@ -173,16 +173,18 @@ else fi # Set variables in uninstall.sh -# Mac os uses BSD version of sed so needs different syntax. -echo "Setting variables into uninstall.sh ..." +# Mac and Linux use different version of sed, so need different syntax. if [[ "$OSTYPE" == "linux-gnu"* ]]; then + echo "Setting variables in uninstall.sh ..." sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL" sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" elif [[ "$OSTYPE" == "darwin"* ]]; then + echo "Setting variables in uninstall.sh ..." sed -i "" -e "14i\\" -e "FOLDER=$FOLDER" "$FOLDER/$UNINSTALL" sed -i "" -e "15i\\" -e "SHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" else - echo "Warning: unknown OS, unable to set variables into uninstall.sh" + echo "Warning: unknown OS or OSTYPE environment variable has been changed." + echo "unable to set variables in uninstall.sh..." fi chmod +x "$FOLDER/$UNINSTALL" diff --git a/uninstall.sh b/uninstall.sh index acf1f91..3d275ea 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -14,7 +14,7 @@ COURSE=m269-23j VENV=~/venvs/$COURSE CSS_FILE=~/.jupyter/custom/custom.css -COURSE_YEAR=23 +YEAR=23 SITE=https://dsa-ou.github.io/m269-installer # Check FOLDER and SHELL_CONFIG_FILE have been set i.e inserted via install.sh @@ -72,29 +72,34 @@ for file in "allowed.py" "m269.json"; do fi done -# Remove aliases from the configuration file ALIASES=("alias nb" "alias allowed" "alias $COURSE") +NB="jupyter notebook &" +COURSE_PATTERN="[Mm]269-$YEAR[Jj]" + +# Remove aliases from the configuration file if [[ -f "$SHELL_CONFIG_FILE" ]]; then echo "Removing shortcut commands from $SHELL_CONFIG_FILE..." cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup for alias in "${ALIASES[@]}"; do - # Delete lines that start with $alias and contain current course "code" + # Pattern is: Start with $alias, and contains $COURSE_PATTERN or $NB in rest of line. + alias_pattern="/^$alias.*\(\($COURSE_PATTERN\)\|\($NB\)\)/d" if [[ "$OSTYPE" == "linux-gnu"* ]]; then - sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" + sed -i "$alias_pattern" "$SHELL_CONFIG_FILE" elif [[ "$OSTYPE" == "darwin"* ]]; then - sed -i "" "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE" + sed -i "" "$alias_pattern" "$SHELL_CONFIG_FILE" else - echo "Warning: unknown OS, skipping alias removal." + echo "Warning: unknown OS or OSTYPE environment variable has been changed." + echo "skipping removal of shortcut commands..." fi done else echo "Warning: $SHELL_CONFIG_FILE does not exist." fi -# Remove lines from ~/.jupyter/custom/custom.css +# Optionally Remove custom M269 styling if confirm "Remove M269 custom styling from $CSS_FILE ?"; then - START_DELIM="\/\* Start of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" - END_DELIM="\/\* End of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/" + START_DELIM="\/\* Start of $COURSE_PATTERN notebook styling. \*\/" + END_DELIM="\/\* End of $COURSE_PATTERN notebook styling. \*\/" if [[ -f "$CSS_FILE" ]]; then cp "$CSS_FILE" "$CSS_FILE".backup echo "Removing M269 custom styling from $CSS_FILE..." @@ -103,7 +108,8 @@ if confirm "Remove M269 custom styling from $CSS_FILE ?"; then elif [[ "$OSTYPE" == "darwin"* ]]; then sed -i "" "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" else - echo "Warning: unknown OS, skipping custom styling removal." + echo "Warning: unknown OS or the OSTYPE environment variable has been changed." + echo "skipping removal of custom styling..." fi else echo "Warning: $CSS_FILE does not exist." From 250f59402cd9a4f90df94a1b2af65aba7a2735aa Mon Sep 17 00:00:00 2001 From: michael Date: Sat, 26 Aug 2023 13:12:07 +0100 Subject: [PATCH 6/6] Update sed commands to be POSIX-compliant --- install.sh | 23 ++++++++++++----------- uninstall.sh | 20 ++++---------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/install.sh b/install.sh index efbd027..f46f9e6 100755 --- a/install.sh +++ b/install.sh @@ -173,19 +173,20 @@ else fi # Set variables in uninstall.sh -# Mac and Linux use different version of sed, so need different syntax. -if [[ "$OSTYPE" == "linux-gnu"* ]]; then +if [[ -f "$FOLDER/$UNINSTALL" ]]; then echo "Setting variables in uninstall.sh ..." - sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL" - sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" -elif [[ "$OSTYPE" == "darwin"* ]]; then - echo "Setting variables in uninstall.sh ..." - sed -i "" -e "14i\\" -e "FOLDER=$FOLDER" "$FOLDER/$UNINSTALL" - sed -i "" -e "15i\\" -e "SHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" + # Insert FOLDER variable at line 14 + sed "14i\\ +FOLDER=$FOLDER" "$FOLDER/$UNINSTALL" > "$FOLDER/$UNINSTALL.tmp" + mv "$FOLDER/$UNINSTALL.tmp" "$FOLDER/$UNINSTALL" + # Insert SHELL_CONFIG_FILE variable at line 15 + sed "15i\\ +SHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" > "$FOLDER/$UNINSTALL.tmp" + mv "$FOLDER/$UNINSTALL.tmp" "$FOLDER/$UNINSTALL" + chmod +x "$FOLDER/$UNINSTALL" else - echo "Warning: unknown OS or OSTYPE environment variable has been changed." - echo "unable to set variables in uninstall.sh..." + echo "Warning: $FOLDER/$UNINSTALL does not exist." + echo "Unable to set variables in uninstall.sh..." fi -chmod +x "$FOLDER/$UNINSTALL" echo "All done. Go to $SITE for further instructions." diff --git a/uninstall.sh b/uninstall.sh index 3d275ea..fb26a72 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -83,14 +83,8 @@ if [[ -f "$SHELL_CONFIG_FILE" ]]; then for alias in "${ALIASES[@]}"; do # Pattern is: Start with $alias, and contains $COURSE_PATTERN or $NB in rest of line. alias_pattern="/^$alias.*\(\($COURSE_PATTERN\)\|\($NB\)\)/d" - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - sed -i "$alias_pattern" "$SHELL_CONFIG_FILE" - elif [[ "$OSTYPE" == "darwin"* ]]; then - sed -i "" "$alias_pattern" "$SHELL_CONFIG_FILE" - else - echo "Warning: unknown OS or OSTYPE environment variable has been changed." - echo "skipping removal of shortcut commands..." - fi + sed "$alias_pattern" "$SHELL_CONFIG_FILE" > "$SHELL_CONFIG_FILE.tmp" + mv "$SHELL_CONFIG_FILE.tmp" "$SHELL_CONFIG_FILE" done else echo "Warning: $SHELL_CONFIG_FILE does not exist." @@ -103,14 +97,8 @@ if confirm "Remove M269 custom styling from $CSS_FILE ?"; then if [[ -f "$CSS_FILE" ]]; then cp "$CSS_FILE" "$CSS_FILE".backup echo "Removing M269 custom styling from $CSS_FILE..." - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" - elif [[ "$OSTYPE" == "darwin"* ]]; then - sed -i "" "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" - else - echo "Warning: unknown OS or the OSTYPE environment variable has been changed." - echo "skipping removal of custom styling..." - fi + sed "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" > "$CSS_FILE.tmp" + mv "$CSS_FILE.tmp" "$CSS_FILE" else echo "Warning: $CSS_FILE does not exist." fi