怎么写shell判断系统是centos6还是centos7
时间: 2023-12-29 17:05:23 浏览: 23
可以通过查看 `/etc/redhat-release` 文件来判断系统是CentOS 6还是CentOS 7。该文件包含操作系统的版本信息。
以下是一些示例命令:
1. 判断CentOS 6:
```
if grep -q "CentOS release 6" /etc/redhat-release ; then
echo "This is CentOS 6"
else
echo "This is not CentOS 6"
fi
```
2. 判断CentOS 7:
```
if grep -q "CentOS Linux release 7" /etc/redhat-release ; then
echo "This is CentOS 7"
else
echo "This is not CentOS 7"
fi
```
这些命令会检查 `/etc/redhat-release` 文件中是否包含特定的版本字符串。如果文件中包含,则输出相应的信息。如果文件中不包含,则输出相应的错误信息。
相关问题
shell脚本如何判断系统是centos还是Ubuntu
在 shell 脚本中, 你可以使用 `lsb_release -a` 命令来判断系统是 CentOS 还是 Ubuntu.
下面是一个例子:
```
#!/bin/bash
system=`lsb_release -a 2> /dev/null | grep "Distributor ID:" | cut -d ":" -f2`
if [ "$system" == "CentOS" ]; then
echo "The system is CentOS."
elif [ "$system" == "Ubuntu" ]; then
echo "The system is Ubuntu."
else
echo "Cannot determine the system."
fi
```
在上面的脚本中, 首先使用 `lsb_release -a` 命令获取系统信息, 然后使用 `grep` 和 `cut` 命令来解析输出, 并提取出发行版本的名称(例如 CentOS 或 Ubuntu). 然后使用 `if` 语句来判断发行版本的名称, 并输出相应的消息.
centos7判断当前系统有没有这个用户 用shell编写
可以在Shell脚本中使用以下代码来判断CentOS 7中是否存在某个用户:
```bash
#!/bin/bash
username="your_username"
if id "$username" >/dev/null 2>&1; then
echo "User $username exists"
else
echo "User $username does not exist"
fi
```
将上述代码保存为一个Shell脚本文件,例如`check_user.sh`,然后在终端执行`sh check_user.sh`即可判断系统中是否存在指定的用户。其中,将`your_username`替换为要查询的用户名。