English 中文(简体)
b 基本插图比较
原标题:bash scripting basic string comparison

我有一个基本的标语问题。 我预计这一文字的输出将:

a y b x c y

但是,我发现:

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;    

我失踪了什么?

最佳回答

您需要在<条码>=操作员周围添加空间,否则就等于“条码>a=b(在扩大后,为第一个代号)。 这已转至<条码>测试<>编码>(<条码>为替代名称)。 由于单一论点(a=b)是一种非排他性原子,其继承和退出地位为0,而then Branch已执行。

问题回答

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;

页: 1 另外,请参看<代码>echo。

探讨以下问题:

#!/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





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签