RHEL Linux 版本依赖挂载 if elif else

RHEL Linux 版本依赖挂载 if elif else

我正在尝试按如下所示进行挂载,其中挂载点取决于版本:

1) 查找下面两个中的任何一个是否已安装。

2)找到 RHEL 版本,然后挂载以下之一

  • 安装 -o soft 123.45.678.9:/web /mnt3(RHEL 5)
  • mount -o vers=3 123.45.678.9:/web /mnt3(RHEL 6)

3) 如果失败,则输出 put 语句。

尝试了这两个代码

第一

mount > /tmp/mountlog;
if grep -ohw "soft,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 5 MOUNT \e[1;33mALREADY\e[00m AVAILABLE \e[1;33m-PASSED\e[00m ";
elif grep -ohw "nfsvers=3,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 6 MOUNT \e[1;33mALREADY\e[00m AVAILABLE \e[1;33m-PASSED\e[00m ";
elif grep -ohw "5.*" /tmp/red; mount -o soft 123.45.678.9:/web /mnt3 &>/dev/null; then mount > /tmp/mountlog; fi
if grep -ohw "soft,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 5 MOUNT AVAILABLE \e[1;33m-PASSED\e[00m ";
elif grep -ohw "6.*" /tmp/red; mount -o vers=3 123.45.678.9:/web /mnt3 &>/dev/null; then mount > /tmp/mountlog; 
if grep -ohw "nfsvers=3,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 6 MOUNT AVAILABLE \e[1;33m-PASSED\e[00m ";
else echo -e " MOUNT DEVICE NOT AVAILABLE Or NOT VALID RHEL VERSION 5 or 6. \e[00;31mFAILED\e[00m "; fi; fi | tee -a /tmp/log;

第二

mount > /tmp/mountlog;
if grep -ohw "soft,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 5 MOUNT \e[1;33mALREADY\e[00m AVAILABLE \e[1;33m-PASSED\e[00m ";
elif grep -ohw "nfsvers=3,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 6 MOUNT \e[1;33mALREADY\e[00m AVAILABLE \e[1;33m-PASSED\e[00m ";
elif grep -ohw "5.*" /tmp/red; mount -o soft 123.45.678.9:/web /mnt3 &>/dev/null; then mount > /tmp/mountlog; fi
if grep -ohw "soft,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 5 MOUNT AVAILABLE \e[1;33m-PASSED\e[00m ";
elif grep -ohw "6.*" /tmp/red; mount -o vers=3 123.45.678.9:/web /mnt3 &>/dev/null; then mount > /tmp/mountlog; fi
if grep -ohw "nfsvers=3,addr=123.45.678.9" /tmp/mountlog; then echo -e " RHEL 6 MOUNT AVAILABLE \e[1;33m-PASSED\e[00m ";
else echo -e " MOUNT DEVICE NOT AVAILABLE Or NOT VALID RHEL VERSION 5 or 6. \e[00;31mFAILED\e[00m "; fi | tee -a /tmp/log;

答案1

脚本中有几个错误;首先突出的是这一行:

elif grep -ohw "5.*" /tmp/red;

我没有看到任何内容写入 /tmp/red。此外,还有一个名为的文件,/etc/redhat-release其中包含版本;

您的 if 结构也有点不对;if grep -ohw "whatever"将返回 n 个计数;这将等于if soft,addr=123.45.678.9,这对 bash 来说真的没有意义!我会使用grep -c,它给出文件中字符串出现次数的数字计数;

IE

mount > /tmp/mountlog;

if [ $(grep -c "soft,addr=123.45.678.9" /tmp/mountlog) -gt 0 ]
        then echo -e " RHEL 5 MOUNT \e[1;33mALREADY\e[00m AVAILABLE \e[1;33m-PASSED\e[00m ";
elif [ $(grep -c  "nfsvers=3,addr=123.45.678.9" /tmp/mountlog) -gt 0 ]
        then echo -e " RHEL 6 MOUNT \e[1;33mALREADY\e[00m AVAILABLE \e[1;33m-PASSED\e[00m ";
elif [ $(grep -c "5.*" /etc/redhat-release) -gt 0 ];
        then mount -o soft 123.45.678.9:/web /mnt3 &>/dev/null;
elif [ $(grep -c "6.*" /etc/redhat-release) -gt 0 ];
        then mount -o vers=3 123.45.678.9:/web /mnt3
fi
mount > /tmp/mountlog;


if [ $(grep -c "soft,addr=123.45.678.9" /tmp/mountlog) -gt 0 ];
        then echo -e " RHEL 5 MOUNT AVAILABLE \e[1;33m-PASSED\e[00m ";
elif [ $( grep -c "nfsvers=3,addr=123.45.678.9" /tmp/mountlog) -gt 0 ];
        then echo -e " RHEL 6 MOUNT AVAILABLE \e[1;33m-PASSED\e[00m ";
else
echo -e " MOUNT DEVICE NOT AVAILABLE Or NOT VALID RHEL VERSION 5 or 6. \e[00;31mFAILED\e[00m ";
fi

相关内容