BASH:星号作为参数

时间:2014-04-07 12:54:29

标签: bash shell variable-expansion

我写了一个小BASH脚本,它采用简单的配置并抓取预配置的第三方存储库,并从中下载某些包。

我目前遇到的问题是,当我在配置中使用星号时,它会占用我在执行脚本时所使用的任何目录,并将我目录中的所有文件扩展为参数。我理解单引号和双引号之间的区别,但似乎无论我做什么,我都无法使它工作。也许有人可以帮我一把。这是脚本:

#!/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

示例配置(/opt/configs/repos/.repos/configs/mysql:

GEN_NAME=MySQL
UPSTREAM=mysql
PACKAGES=*

PACKAGES选项设置为*以外的任何内容时,我对包的下载没有任何问题,所以我不会发布任何repo配置(因为我知道)回购工作)。

我当前的目录我是从

运行的
# ls
mysql  salt  sysutils  zabbix

当我运行脚本时:

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

我想要的输出是:

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/ *

此次运行的调试输出在此处:

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

提前致谢 - 这真的让我感到厌恶...

2 个答案:

答案 0 :(得分:2)

尝试将完整命令嵌入到单个变量中很少是个好主意。至少,将参数分离到一个数组中,并自己指定命令名称。此外,if循环中的RELEASE语句似乎没有必要,因为两个分支之间的唯一区别是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

这确保PACKAGES(以及所有参数,实际上)的值在任何使用的地方都保持适当的引用。

答案 1 :(得分:1)

在您的脚本中而不是:

echo ${CMD}

您需要使用:

echo "${CMD}"

避免shell展开*

*不加引号打印时,它会扩展到当前目录中的文件和目录。