shell bash中的字符串比较不起作用

xyhw6mcr  于 2023-04-12  发布在  Shell
关注(0)|答案(3)|浏览(188)

你好,我是新的打击脚本。只是写了这个简单的程序,但它是抛出错误。

#!/bin/bash
os=`uname -o`
echo $os
if ["$os"=="GNU/Linux"] ; then
    echo "Linux"
else
    echo "Windows"
fi

在这两种情况下都使用==或-eq,我得到了以下错误,并且它正在打印else条件。

./ostype.sh:line 3:[GNU/Linux==GNU/Linux]:无此文件或目录
Windows

Bash版本:GNU bash,version 3.2.48(1)-release(x86_64-suse-linux-gnu)

sxissh06

sxissh061#

尝试

if [ "$os" = "GNU/Linux" ]

注意空格和单个=
[实际上是一个程序,其余的都是参数!

js81xvg6

js81xvg62#

使用=进行字符串比较。请参见:http://tldp.org/LDP/abs/html/comparison-ops.html
此外,方括号和比较运算符(即

if [ "$os" = "GNU/Linux" ]; then 
  ^ ^     ^ ^           ^  
  | |     | |           |
   \-\-----\-\-----------\-- (need spaces here)

相关问题