1

I wrote a little BASH script that takes a simple config and grabs pre-configured 3rd party repositories, and downloads certain packages from them.

The problem I am currently having is that when I use an asterisk inside the config, it takes whatever directory I am in when executing the script, and expands all the files in my directory as arguments. I understand the difference between single quotes and double quotes, but it seems like no matter what I do, I can't get it to work. Maybe someone could give me a hand on this one. Here's the script:

#!/bin/bash

CONFIGS="/opt/configs/repos/.repos/configs"
REPOS="/opt/configs/repos"
MASTER_REPO=extras
SYNCAPPS=(mysql)

for APP in ${SYNCAPPS[@]};do
    . $CONFIGS/$APP
    echo "$APP"
    for RELEASE in {5..6};do
        if [ $RELEASE == "5" ];then
            for ARCH in {x86_64,i386};do
                CMD="yumdownloader --disablerepo='*' --enablerepo=$UPSTREAM-$RELEASE-$ARCH -c $REPOS/.repos/$UPSTREAM-$RELEASE.repo --destdir=$REPOS/$MASTER_REPO/$RELEASE/$GEN_NAME/ $PACKAGES"
                $CMD
                echo ${CMD}
            done
        elif [ $RELEASE == "6" ];then
            for ARCH in {x86_64,i386};do
                                CMD="yumdownloader --disablerepo='*' --enablerepo=$UPSTREAM-$RELEASE-$ARCH -c $REPOS/.repos/$UPSTREAM-$RELEASE.repo --destdir=$REPOS/$MASTER_REPO/$RELEASE/$GEN_NAME/ $PACKAGES"
                $CMD
                echo ${CMD}
                        done
        fi
    done
done

Sample Config (/opt/configs/repos/.repos/configs/mysql:

GEN_NAME=MySQL
UPSTREAM=mysql
PACKAGES=*

I have no issues with the download of the packages when the PACKAGES option is set to anything other than *, so I won't post any of the repo config (since I know the repos work).

My current directory I'm running this from:

# ls
mysql  salt  sysutils  zabbix

When I run the script:

# ./myscript.sh
mysql
yumdownloader --disablerepo='*' --enablerepo=mysql-5-x86_64 -c /opt/data/build/repos/.repos/mysql-5.repo --destdir=/opt/data/build/repos/extras/5/MySQL/ mysql salt sysutils zabbix
yumdownloader --disablerepo='*' --enablerepo=mysql-5-i386 -c /opt/data/build/repos/.repos/mysql-5.repo --destdir=/opt/data/build/repos/extras/5/MySQL/ mysql salt sysutils zabbix
yumdownloader --disablerepo='*' --enablerepo=mysql-6-x86_64 -c /opt/data/build/repos/.repos/mysql-6.repo --destdir=/opt/data/build/repos/extras/6/MySQL/ mysql salt sysutils zabbix
yumdownloader --disablerepo='*' --enablerepo=mysql-6-i386 -c /opt/data/build/repos/.repos/mysql-6.repo --destdir=/opt/data/build/repos/extras/6/MySQL/ mysql salt sysutils zabbix

What I want the output to be is:

mysql
yumdownloader --disablerepo='*' --enablerepo=mysql-5-x86_64 -c /opt/data/build/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ *
yumdownloader --disablerepo='*' --enablerepo=mysql-5-i386 -c /opt/data/build/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ *
yumdownloader --disablerepo='*' --enablerepo=mysql-6-x86_64 -c /opt/data/build/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ *
yumdownloader --disablerepo='*' --enablerepo=mysql-6-i386 -c /opt/data/build/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ *

The debug output for this run is here:

mysql
+ for RELEASE in '{5..6}'
+ '[' 5 == 5 ']'
+ for ARCH in '{x86_64,i386}'
+ CMD='yumdownloader --disablerepo='\''*'\'' --enablerepo=mysql-5-x86_64 -c /opt/configs/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ *'
+ echo yumdownloader '--disablerepo='\''*'\''' --enablerepo=mysql-5-x86_64 -c /opt/configs/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ mysql salt sysutils zabbix
yumdownloader --disablerepo='*' --enablerepo=mysql-5-x86_64 -c /opt/configs/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ mysql salt sysutils zabbix
+ for ARCH in '{x86_64,i386}'
+ CMD='yumdownloader --disablerepo='\''*'\'' --enablerepo=mysql-5-i386 -c /opt/configs/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ *'
+ echo yumdownloader '--disablerepo='\''*'\''' --enablerepo=mysql-5-i386 -c /opt/configs/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ mysql salt sysutils zabbix
yumdownloader --disablerepo='*' --enablerepo=mysql-5-i386 -c /opt/configs/repos/.repos/mysql-5.repo --destdir=/opt/configs/repos/extras/5/MySQL/ mysql salt sysutils zabbix
+ for RELEASE in '{5..6}'
+ '[' 6 == 5 ']'
+ '[' 6 == 6 ']'
+ for ARCH in '{x86_64,i386}'
+ CMD='yumdownloader --disablerepo='\''*'\'' --enablerepo=mysql-6-x86_64 -c /opt/configs/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ *'
+ echo yumdownloader '--disablerepo='\''*'\''' --enablerepo=mysql-6-x86_64 -c /opt/configs/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ mysql salt sysutils zabbix
yumdownloader --disablerepo='*' --enablerepo=mysql-6-x86_64 -c /opt/configs/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ mysql salt sysutils zabbix
+ for ARCH in '{x86_64,i386}'
+ CMD='yumdownloader --disablerepo='\''*'\'' --enablerepo=mysql-6-i386 -c /opt/configs/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ *'
+ echo yumdownloader '--disablerepo='\''*'\''' --enablerepo=mysql-6-i386 -c /opt/configs/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ mysql salt sysutils zabbix
yumdownloader --disablerepo='*' --enablerepo=mysql-6-i386 -c /opt/configs/repos/.repos/mysql-6.repo --destdir=/opt/configs/repos/extras/6/MySQL/ mysql salt sysutils zabbix

Thanks in advance - this has really thrown me for a loop...

2

2 Answers 2

2

Trying to embed a full command into a single variable is rarely a good idea. At the very least, separate the arguments into an array, and specify the command name by itself. Also, the if statement inside your RELEASE loop seems unnecessary, because the only difference between the two branches is the value of RELEASE.

CONFIGS="/opt/configs/repos/.repos/configs"
REPOS="/opt/configs/repos"
MASTER_REPO=extras
SYNCAPPS=(mysql)

for APP in "${SYNCAPPS[@]}"; do
    . "$CONFIGS/$APP"
    echo "$APP"
    for RELEASE in 5 6; do
        for ARCH in x86_64 i386; do
            arguments=( "--disablerepo=*"
                        "--enablerepo=$UPSTREAM-$RELEASE-$ARCH"
                        "-c"
                        "$REPOS/.repos/$UPSTREAM-$RELEASE.repo"
                        "--"
                        "destdir=$REPOS/$MASTR_REPO/$RELEASE/$GEN_NAME/"
                        "$PACKAGES"
                       )
            echo "yumdownloader ${arguments[@]}"
            yumdownloader "${arguments[@]}"
        done
    done
done

This ensures that the value of PACKAGES (and all the parameters, really) remains properly quoted wherever used.

5
  • in regards to you commend on the if statement; this is just initial stage of the script. I have release specific items that will be going into it that I will need the if statement for. Apr 7, 2014 at 13:26
  • 1
    +1 for the fundamental solution; perhaps say "into a variable containing a single string" - as opposed to an array variable; (they're both single variables).
    – mklement0
    Apr 7, 2014 at 13:57
  • 1
    @mklement0 True; in this case, a simple command can still be included in a single array and executed with "${CMD[@]}". But as soon as you try anything more complex--FOO=bar cmdname arg1, cmd1 | cmd2, etc.--you're back to the problem of a single variable being insufficient. Best to stick with storing arguments only in an array, and leaving command names and higher syntax either hard-coded or wrapped in functions.
    – chepner
    Apr 7, 2014 at 14:02
  • Whew: you caught that while I could still edit the comment :)
    – chepner
    Apr 7, 2014 at 14:05
  • Thanks for this solution. When I tried using this solution (awesome solution by the way; very well thought out, yet simple), it ended up screwing up my regular repos, almost like I couldn't have one (just asterisks) or the other (multiple items listed in a variable), so what I ended up doing in my config was setting the variable as an array list (e.g. PACKAGES=(htop iftop cdpr)) rather than PACKAGES="htop iftop cdpr". When doing this, it satisfied both asterisks and itemized lists, while still being able to use this elegant solution. thanks again! Apr 8, 2014 at 12:25
1

In your script instead of:

echo ${CMD}

You need to use:

echo "${CMD}"

To avoid shell expanding *.

When * is printed unquoted it expands to files and directories present in current directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.