0

I am creating a bash script to copy a set of source files to different destination directories. I want to automate this process more, especially for tiny repeated changes, and want to cut out the compilation wait time of Magento 2 (while also using a single set of files that are not in a git repo).

Question

How do I correctly add to bash Arrays, specifically paths with slashes?

Context

Magento 2 uses app/design/frontend/Vendor/Theme/* for overridden theme files which then compiles into pub/static/frontend/{language}/*. This is usually sufficient if you have a single file and then compile to a single website.

This process becomes more cumbersome if you just want to quickly upload a small fix/update to a Javascript file; just to recompile again (waiting ~3+ minutes); so while testing it may be faster just to replace the compiled file on the different folder locations (app/design/frontend and pub/static/frontend/)

  • There are multiple sites (eg. site1, site2)
  • There are multiple languages (eg. en_GB, en_US)
  • There are multiple themes (eg. Vendor1/Theme1, Vendor2/Theme2)

Error

line 11: app/design/frontend/Vendor/Theme_Name: division by 0 (error token is "design/frontend/Vendor/Theme_Name")

--- line 11 code: ---
COPY_TO_FOLDER_LOCATIONS=("${TEMP_COPY_FOLDER_LOCATIONS[@]}" "${TEMP_COPY_FOLDER_LOCATIONS[indexLocation]}")

I also tried:

COPY_TO_FOLDER_LOCATIONS+=("${TEMP_COPY_FOLDER_LOCATIONS[indexLocation]}" )

From the error it is clear that the bash interprets the code where I want to add an item INTO the array as arithmetic (division by 0) (the error token shows path without app),

Bash script

ROOT_FOLDER="$PWD"
# Copy to these locations
THEME_NAME="Vendor/Theme_Name"
declare -a LANGUAGES=("en_GB" "en_US")

declare -a COPY_TO_ROOT_LOCATIONS=("/var/www/html/site1" "/var/www/html/site2")
declare -a TEMP_COPY_TO_FOLDER_LOCATIONS=("app/design/frontend/$THEME_NAME" "pub/static/frontend/{language}/$THEME_NAME")
declare -a COPY_TO_FOLDER_LOCATIONS=()

for indexLocation in "${TEMP_COPY_TO_FOLDER_LOCATIONS}"; do
#### Once I get this sorted, here I will use getThemeLanguagePath() to get the path to add to
    COPY_TO_FOLDER_LOCATIONS=("${TEMP_COPY_FOLDER_LOCATIONS[@]}" "${TEMP_COPY_FOLDER_LOCATIONS[indexLocation]}")
done

for indexLocation in "${COPY_TO_FOLDER_LOCATIONS}"; do
    echo "$COPY_TO_FOLDER_LOCATIONS}"
    tText=getThemeLanguagePath "ABC" "$COPY_TO_FOLDER_LOCATIONS"
    echo "LOCATION : $tText"
done

readarray -d '' MOVE_FILES < <(find $ROOT_FOLDER -type f -name "*.move" -print0)

echo "${#MOVE_FILES[@]}"

function getThemeLanguagePath(){
    local language="$1"
    local path="$2"
    echo "$2/$1"
}

for indexLocation in "${!COPY_TO_ROOT_LOCATIONS[@]}"; do
    for indexFile in "${!MOVE_FILES[@]}"; do
        file="${MOVE_FILES[indexFile]}"
        source="$file"
        destination="$(basename -- $file)"
        #destination=$("${file##*/}")
        echo "$source"
        echo "$destination"
    done
done

Objective

  • I need to test changes across all themes quicker.
  • These things are core Javascript/CSS that needs to work on different themes, and can't be solved with creating a custom module

This is a rough draft which will be improved when I have solved the issues. I want to automate the process and create dynamic paths to copy the files from the source (.move files) into different paths

  • There are multiple sites (eg. site1, site2)
  • There are multiple languages (eg. en_GB, en_US)
  • There are multiple themes (eg. Vendor1/Theme1, Vendor2/Theme2)
CvRChameleon
  • 367
  • 1
  • 5
  • 29
  • [Shellcheck](https://www.shellcheck.net/) finds several serious problems with the code, particularly to do with use of arrays. If you fix the problems identified by [Shellcheck](https://www.shellcheck.net/) it will then be easier to tell if you've got more subtle problems. – pjh Mar 29 '22 at 18:10
  • 1
    The specific problem with line 11 is that `"${TEMP_COPY_FOLDER_LOCATIONS[indexLocation]}"` is using a variable containing a path (`indexLocation`) as an array index. Array indexes must be non-negative integers, or expressions that evaluate to non-negative integers. In this case Bash is trying to treat the path as an expression and assumes that the slashes are division operators. You get division by zero because the path components are taken to be uninitialized variables (which are automatically initialized to zero). – pjh Mar 29 '22 at 18:14
  • Note that using `ALL_UPPERCASE` variable names is dangerous because it risks clashing with common environment variables or Bash built-in variables (of which there are very many). See [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/4154375). – pjh Mar 29 '22 at 18:18
  • @pjh Thank you for the advice! That ShellCheck tool is useful thanks, most of the errors are expected at this point, as the script is unfinished because of this issue. Regarding naming, I am aware of environment variables, I have always tried to make names that are/should be `UNIQUE_UPPERCASE_NAMES`, I will use the naming advice on the link you provided. – CvRChameleon Mar 30 '22 at 07:42
  • Based on your description regarding line 11, I realised that it wasn't my intention to directly use array value as an `expression`. I wanted to use the array value as `string`, but my syntax was incorrect: I found the syntax `"${TEMP_COPY_TO_FOLDER_LOCATIONS}"` but I used it correctly lower on `${!COPY_TO_ROOT_LOCATIONS[@]}`. Sad that I couldn't see that yesterday :( – CvRChameleon Mar 30 '22 at 07:52

0 Answers0