如果Statement没有正确识别该repo文件存在

时间:2015-10-22 17:56:08

标签: linux bash if-statement yum

我有一个if语句,正在寻找/etc/yum.repos.d/中存在的repo文件:我的代码如下:

OptoolsRepo='rhel-7-server-7.0-rpms'   

     if [ -e /etc/yum.repos.d/$OptoolsRepo ]
     then 
        echo " "
            echo "Your Repo For OpenStack OpTools already exists."
          else
        #       function add-repo() {
        #               reponame='rhel-7-server-rpms'
        #               url='XXXXXXX/XXX/XXX/XXXX/'
                        touch /etc/yum.repos.d/$OptoolsRepo.repo
                        echo -e "[$OptoolsRepo]\nname=$OptoolsRepo\nbaseurl=${urlOptools}\nsselverify=0\nenabled=1\ngpgcheck=0\nmetadata_expire=12h"  >> /etc/yum.repos.d/$OptoolsRepo.repo
        #}
     fi

/etc/yum.repos.d/我看到以下内容:

[root@XXXXX ~]# ls -lah /etc/yum.repos.d/
total 52K
drwxr-xr-x  2 root root 4.0K Oct 22 13:42 .
drwxr-xr-x 91 root root 8.0K Oct 19 14:18 ..
-rw-r--r--  1 root root 4.0K Oct 21 14:09 .repo
-rw-r--r--  1 root root  254 Jun 29 03:59 rhel7-isv.repo
-rw-r--r--  1 root root  269 Jun 19 10:45 rhel7-security.repo
-rw-r--r--  1 root root  518 Oct 22 13:42 rhel-7-server-7.0-rpms

我不确定为什么会这样。我的“if”语句是否存在语法问题?如果有人可以提供帮助,爱会帮助你!

1 个答案:

答案 0 :(得分:0)

这只是语法错误,正确的方法是:

#!/bin/bash

OptoolsRepo='rhel-7-server-7.0-rpms'   

     if [ -e "/etc/yum.repos.d/"$OptoolsRepo ]; then

        echo " "
            echo "Your Repo For OpenStack OpTools already exists."
          else
        #       function add-repo() {
        #               reponame='rhel-7-server-rpms'
        #               url='XXXXXXX/XXX/XXX/XXXX/'
                        touch /etc/yum.repos.d/$OptoolsRepo.repo
                        echo -e "[$OptoolsRepo]\nname=$OptoolsRepo\nbaseurl=${urlOptools}\nsselverify=0\nenabled=1\ngpgcheck=0\nmetadata_expire=12h"  >> /etc/yum.repos.d/$OptoolsRepo.repo
        #}
     fi