1111# ##################################################################################################
1212# Global Variables & Constants
1313# ##################################################################################################
14- # Exit the script immediately if any command fails
15- set -e
14+ # Exit the script immediately if any command fails or if an unset variable is used
15+ set -eu
1616
1717readonly FILE_NAME=" update-all.sh"
1818readonly UPDATE_SCRIPT_SOURCE_URL=" https://raw.githubusercontent.com/andmpel/MacOS-All-In-One-Update-Script/HEAD/${FILE_NAME} "
@@ -72,7 +72,7 @@ print_err() {
7272# Description: Update shell configuration files
7373update_rc () {
7474 _rc=" "
75- case $ ADJUSTED_ID in
75+ case " ${ ADJUSTED_ID} " in
7676 darwin)
7777 _rc=" ${HOME} /.zshrc"
7878 ;;
@@ -82,7 +82,7 @@ update_rc() {
8282 ;;
8383 esac
8484
85- # Check if `alias update='sudo sh ${HOME}/.update.sh'` is already defined, if not then append it
85+ # Check if `update` function is already defined, if not then append it
8686 if [ -f " ${_rc} " ]; then
8787 if ! awk ' /^update\(\) {/,/^}/' " ${_rc} " | grep -q ' curl' ; then
8888 println " ==> Updating ${_rc} for ${ADJUSTED_ID} ..."
@@ -93,7 +93,10 @@ update_rc() {
9393 println " ==> Profile not found. ${_rc} does not exist."
9494 println " ==> Creating the file ${_rc} ... Please note that this may not work as expected."
9595 # Create the rc file
96- touch " ${_rc} "
96+ if ! touch " ${_rc} " ; then
97+ print_err " Error: Failed to create ${_rc} ."
98+ exit 1
99+ fi
97100 # Append the sourcing block to the newly created rc file
98101 println " ${UPDATE_SOURCE_STR} " >> " ${_rc} "
99102 fi
@@ -109,9 +112,21 @@ update_rc() {
109112# Main Script
110113# ##################################################################################################
111114
115+ # Check for required commands
116+ for cmd in awk cat chmod curl mkdir touch uname; do
117+ if ! command -v " ${cmd} " > /dev/null 2>&1 ; then
118+ print_err " Error: Required command '%s' not found in PATH.\n" " ${cmd} "
119+ exit 1
120+ fi
121+ done
122+
123+ if [ " $( id -u) " -eq 0 ]; then
124+ print_err " Warning: Running as root is not recommended."
125+ fi
126+
112127OS=$( uname)
113128
114- case ${OS} in
129+ case " ${OS} " in
115130Darwin)
116131 ADJUSTED_ID=" darwin"
117132 ;;
0 commit comments