0

我是 bash 的初学者,在这里我真的需要一些帮助。脚本的条件部分由于某种原因没有执行。这是我的代码

#!/bin/bash
#Checking Os
os=$(lsb_release -si)
echo $os
if [ $os == "CentOs" ]; then
echo 'success'
fi

现在部分直到 echo $os 工作正常,结果是

中心操作系统

但是“成功”没有得到输出:(

知道为什么如果部分没有被执行。语法有什么问题。我没有任何错误

4

2 回答 2

1

Linux 区分大小写。在我的 CentOS 安装上运行“lsb_release -a”会导致“CentOS”——注意尾随的“S”是大写的。更改您的代码以测试正确大写的字符串,它应该可以工作。

于 2013-08-04T11:43:43.110 回答
1

希望这可以帮助:

#!/bin/bash
#Checking Os
os=$(lsb_release -si)

# !quote variables when passing them to commands like echo!
echo "$os"

# A space between ] and ; Quote "$os". Note: = instead of == is OK as well.
# Note that it's not CentOs, it's CentOS
if [ "$os" == "CentOS" ] ; then
  echo 'success'
fi

终极参考:高级 Bash 脚本指南

于 2013-08-04T11:06:18.077 回答