bash 脚本基本字符串比较

发布于 2024-12-13 09:42:54 字数 810 浏览 4 评论 0原文

我有一个基本的 bash 脚本问题。我预计该脚本的输出是

: y 乙 x c y

但相反,我得到

: x 乙 x c x

#!/bin/bash                                                                     
for foo in 'a' 'b' 'c'; do
    echo $foo;                                         
    if [ "$foo"=="b" ]; then                                                                        
        echo x;                                                                 
    else                                                                        
        echo y;                                                                 
    fi                                                                                                                                                        
done;    

我错过了什么?

I have a basic bash scripting question. The output of this script, I expected to be:

a
y
b
x
c
y

but instead, I get:

a
x
b
x
c
x

#!/bin/bash                                                                     
for foo in 'a' 'b' 'c'; do
    echo $foo;                                         
    if [ "$foo"=="b" ]; then                                                                        
        echo x;                                                                 
    else                                                                        
        echo y;                                                                 
    fi                                                                                                                                                        
done;    

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰火雁神 2024-12-20 09:42:54

试试这个脚本:

#!/bin/bash

for foo in 'a' 'b' 'c'; do
  echo "$foo";

  if [ "$foo" = 'b' ]; then
    echo 'x';
  else
    echo 'y';
  fi
done;

在 bash 中使用 = 进行字符串比较。另外,引用您的 echo 字符串。

Try this script:

#!/bin/bash

for foo in 'a' 'b' 'c'; do
  echo "$foo";

  if [ "$foo" = 'b' ]; then
    echo 'x';
  else
    echo 'y';
  fi
done;

You use = for string comparisons in bash. Also, quote your echo'd strings.

伪装你 2024-12-20 09:42:54

您需要在 == 运算符周围添加空格,否则它将被解析为单个标记 a==b (扩展后,用于第一次迭代)。这将被传递给 test 内置函数(其中 [ 是一个替代名称)。由于单个参数 a==b 是一个非空字符串原子,因此它成功并以状态 0 退出,并且 then 分支被执行。

You need to add spaces around the == operator, otherwise it gets parsed as the single token a==b (after expansion, for the first iteration). This gets passed to the test builtin (for which [ is an alternative name). Since the single argument a==b is a non-empty string atom, it succeeds and exits with a status of 0, and the then branch gets executed.

迷雾森÷林ヴ 2024-12-20 09:42:54

看看以下内容:

#!/bin/bash

for foo in 'a' 'b' 'c'; do

    echo $foo;

    if [ $foo == "b" ]; then        # Make a note of the spaces
        echo "x";
    else
        echo "y";
    fi
done;

问候,

Rohan Dsouza

Take a look at the following:

#!/bin/bash

for foo in 'a' 'b' 'c'; do

    echo $foo;

    if [ $foo == "b" ]; then        # Make a note of the spaces
        echo "x";
    else
        echo "y";
    fi
done;

Regards,

Rohan Dsouza

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文