Frage

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...

War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top