bash for loop examples 2200575 FINAL 1312e324ffeb4d58ac5cbb2707104b8a 848x461 - Iterate Program Arguments Inside Bash Script
Blog Tutorial

Iterate Program Arguments Inside Bash Script

The Problem

I have a bash script which inside the script will call a program which actually the fpm program. The example of fpm command is as below:

fpm -s dir \
    -t deb \
    -n $PHPNAME \
    -v $PHPVERSION \
    --iteration $ITERATION \
    --config-files etc/$PHPNAME/php.ini \
    -C . \
    -d libzip4 \
    -d aspell \
    -d libmcrypt4 \
    -d libtidy-0.99-0 \
    -d openssl \
    -d libpcre3 \
    -d bzip2 \
    -d curl \
    -d libjpeg-turbo8 \
    -d libpng12-0 \
    -d libfreetype6 \
    -d libgmp10 \
    -d libmhash2 \
    -d libxml2 \
    -d libexif12 \
    -d zip \
    -d gzip \
    -d libasan2 \
    -d readline-common \
    -d libpqxx-4.0 \
    -d libxext6 \
    -d libxpm4 \
    -d libsnmp30 \
    -d libicu55 \
    -d libgd3 \
    -d libodbc1 \
    --maintainer "<$EMAIL>" \
    --description "$PHPVERSION version $PHPVERSION for $PRODUCTNAME" \
    --url "https://$DOMAIN" \
    --verbose \
    --deb-no-default-config-files \
    --after-install ../$PHPNAME-after-install.sh \
    --before-remove ../$PHPNAME-before-remove.sh \
    --after-remove ../$PHPNAME-after-remove.sh \
    --after-upgrade ../$PHPNAME-after-upgrade.sh \
    --log info

This is the snippet of the script that I am using to compile PHP for RunCloud. The problem is that I want to create new deb package for this php and name it php-extensions. Downloading PHP extensions will automatically download other packages like bcmath, xslt and many more php modules. I don’t want to appends the dependencies to main php package as shown above, and I have my own reason. To do that, I need to add new bash line inside my bash automation script as follow:

fpm -s dir \
    -t deb \
    -n php-extensions \
    -v $PHPVERSION \
    --iteration $ITERATION \
    -C . \
    -d php71rc-bcmath \
    -d php71rc-cli \
    -d php71rc-common \
    ...
    --maintainer "<$EMAIL>" \
    --description "$PHPVERSION extensions version $PHPVERSION for $PRODUCTNAME" \
    --url "https://$DOMAIN" \
    --verbose \
        --deb-no-default-config-files \
    --log info

Notice that there are multiple -d arguments inside the script. -d argument is the problem that I am facing because I want to generate it dynamically based on already compiled php modules (php71rc-bcmath, php71rc-cli, etc, etc). I won’t be typing the modules name, but I create a variable to hold the module name by for loop inside the modules directory and attach it into the variable. The final variable will be as follows:

modules="php71rc-bcmath php71rc-cli ..."

Solution

The solution was provided by @akmalhisyam_.

modules="php71rc-bcmath php71rc-cli ..."

for i in ${modules[@]}; do
    args+="-d=${i} "
done

fpm -s dir \
    -t deb \
    -n php-extensions \
    -v $PHPVERSION \
    --iteration $ITERATION \
    -C . \
    -d php71rc \
    ${args} \
    --maintainer "<$EMAIL>" \
    --description "$PHPVERSION extensions version $PHPVERSION for $PRODUCTNAME" \
    --url "https://$DOMAIN" \
    --verbose \
    --deb-no-default-config-files \
    --log info

Happy coding!