|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Check if both the old [$1] and new [$2] MySQL root user passwords have been provided as command line arguements. Exit with error message if not. |
| 4 | +if [[ -z $1 || -z $2 ]]; then |
| 5 | + printf '\nERROR: Usage is mamp-mysql.sh OLDPASSWORD NEWPASSWORD\n' |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +# Define the parent directory containing the files to be updated. |
| 10 | +SOURCE=/Applications/MAMP/bin |
| 11 | + |
| 12 | +# Define the names of the files to be updated excluding config.inc.php which is handled separately at the end of this script. |
| 13 | +declare -a files=("checkMysql.sh" "quickCheckMysqlUpgrade.sh" "repairMysql.sh" "stopMysql.sh" "upgradeMysql.sh") |
| 14 | + |
| 15 | +printf '\n' |
| 16 | +# Iterate through the list of file names. |
| 17 | +for f in "${files[@]}" |
| 18 | +do |
| 19 | + # Check if file exists. Display warning message and skip to next file if not. |
| 20 | + if [ -f "$SOURCE/$f" ]; then |
| 21 | + # Check if the text `-uroot -pOLDPASSWORD` exists in the file. Display warning message if not. |
| 22 | + grep -e "-u[ ]*root -p[ ]*$1" "$SOURCE/$f" > /dev/null |
| 23 | + if [ $? -eq 0 ]; then |
| 24 | + # Change the old password in the file to the new password making a backup (.bak) first. |
| 25 | + sed -i '.bak' -E "s/(-u[ ]*root -p[ ]*)$1/\1$2/g" "$SOURCE/$f" |
| 26 | + # Display success message. |
| 27 | + echo "Updated $SOURCE/$f with new password '$2'" |
| 28 | + else |
| 29 | + # Display warning message regarding old password. |
| 30 | + echo "WARNING: Could not find old password '$1' in $SOURCE/$f" |
| 31 | + fi |
| 32 | + else |
| 33 | + # Display warning message regarding file doesn't exist. |
| 34 | + echo "WARNING: The file $SOURCE/$f does not exist" |
| 35 | + fi |
| 36 | +done |
| 37 | + |
| 38 | +# Process the file config.inc.php checking first if it exists. Display warning message if not. |
| 39 | +if [ -f "$SOURCE/phpMyAdmin/config.inc.php" ]; then |
| 40 | + # Check if the text `'password'] ='OLDPASSWORD` exists in the file. Display warning message if not. |
| 41 | + grep -e "'password'\] *= '$1" "$SOURCE/phpMyAdmin/config.inc.php" > /dev/null |
| 42 | + if [ $? -eq 0 ]; then |
| 43 | + # Change the old password in the file to the new password making a backup (.bak) first. |
| 44 | + sed -i '.bak' -E "s/('password'\] *= ')$1/\1$2/g" "$SOURCE/phpMyAdmin/config.inc.php" |
| 45 | + # Display success message. |
| 46 | + echo "Updated $SOURCE/phpMyAdmin/config.inc.php with new password '$2'" |
| 47 | + else |
| 48 | + # Display warning message regarding old password. |
| 49 | + echo "WARNING: Could not find old password '$1' in $SOURCE/phpMyAdmin/config.inc.php" |
| 50 | + fi |
| 51 | +else |
| 52 | + # Display warning message regarding file doesn't exist. |
| 53 | + echo "WARNING: The file $SOURCE/phpMyAdmin/config.inc.php does not exist" |
| 54 | +fi |
| 55 | +printf '\n' |
0 commit comments