-
-
Save scottsb/d688e96dfc3c1503cfed to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# Settings | |
SUFFIX=alt | |
MAGERUNCMD=magerun | |
# Sanity Checks | |
type $MAGERUNCMD >/dev/null 2>&1 || { | |
echo "ERROR: This script requires the command '$MAGERUNCMD' to be executable in the current path." | |
exit | |
} | |
[[ "$PWD" == *media/catalog/product ]] || { | |
echo "ERROR: This script should be run from the 'media/catalog/product' directory of your Magento install." | |
exit | |
} | |
# Directions | |
echo "It is highly recommended that you run the 'media:images:removeorphans' magerun extension before this." | |
echo '---' | |
if [ ! "$1" = '-f' ]; then | |
echo 'This script performs a dry run by default. To force changes pass flag -f.' | |
read -p "Press [Enter] key to continue with dry run or [Ctrl-C] to exit..." | |
else | |
echo 'WARNING: Forcing updates. Changes are destructive and cannot be undone.' | |
read -p "Press [Enter] key to continue with force mode or [Ctrl-C] to exit..." | |
fi | |
echo '---' | |
# Work Time | |
DUPLICATES=$(find . -path ./cache -prune -o -type f) | |
echo "$DUPLICATES" | sort -f | uniq -id | while read -r FILENAME; do | |
INDEX=1 | |
while read OLD_FILENAME; do | |
NEW_FILENAME=$(echo "$OLD_FILENAME" | tr '[:upper:]' '[:lower:]' | perl -pe "s/\.([^.]*)\$/_$SUFFIX$INDEX.\1/") | |
if [ ! -e "$NEW_FILENAME" ]; then | |
eval $MAGERUNCMD db:query $([ ! "$1" = '-f' ] && printf -- --only-command) \ | |
"'UPDATE mag_catalog_product_entity_media_gallery SET value = \"${NEW_FILENAME#.}\" WHERE BINARY value = \"${OLD_FILENAME#.}\";'" \ | |
&& eval $([ ! "$1" = '-f' ] && printf echo) mkdir -p "${NEW_FILENAME%/*}" \ | |
&& eval $([ ! "$1" = '-f' ] && printf echo) mv -n "$OLD_FILENAME" "$NEW_FILENAME" | |
else | |
echo "New name $NEW_FILENAME for $OLD_FILENAME already exists. Refusing to move or update DB." | |
false | |
fi | |
if [ $? -gt 0 ]; then | |
echo 'Terminating early on fatal error.' | |
[ "$1" = '-f' ] && echo 'Error above may need to be manually fixed.' | |
exit | |
fi | |
echo "Moved $OLD_FILENAME => $NEW_FILENAME." | |
((INDEX++)) | |
done < <(grep -i "$FILENAME" <<< "$DUPLICATES") | |
done |
Maybe is pretty awesome, definitely. Here I wanted something built-in and portable so others could use this.
I'm not sure where you're referring to putting commands in variables. Can you clarify?
MAGERUNCMD is a variable that holds a command. In almost every situation like this you get better control that is also less error prone by using a shell function.
In this case what I'm talking about is really the whole eval $MAGERUNCMD Blah blah
combined with the dry run scenario. If you define slightly different functions depending on the args and dry-runniness you want, you can avoid eval altogether and have a more readable abstraction, to boot.
The purpose of $MAGERUNCMD
is to allow you to change it easily to blah.phar
or whatever you call it. I don't think functions would solve that...
What you're describing now (a script that you want people to edit, i.e. by setting MAGERUNCMD to a different value inline) is really an antipattern. If you want to support multiple names for the same command, just enumerate which ones you support. There aren't that many likely names for this command - it's going to be one of {n98-,}magerun{,.phar}
.
- I'd recommend parameterizing that, or just picking the first executable on PATH that matches one of those names.
- I'd recommend not letting anyone modify the script for one-off executions, which is what you seem to be describing, IIUC.
-f
approach (details later).tr '[:upper:]' '[:lower:]'
can be accomplished via parameter expansion like"${foo,,}"
.perl -pe "s/\.([^.]*)\$/_$SUFFIX$INDEX.\1/")
can be accomplished via parameter expansion like"${foo%.*}_$SUFFIX$INDEX.${foo##*.}"
.-f
pattern using Maybe.