-1

I have a problem with writing bash script. The problem is in comparison of strings. When I launch it, there's no errors. However in result, it is always changing the variable client. So if for an example we have two lines in file

apple A
orange D

and if I give the who=A I expect to see in result apple, or if at D - orange

But no matter of what I choose A or D it is always giving me the result - orange

No matter of the strings, it always change the variable client, like ignoring the comparison. Please help.

while read line
do
    IFS=" "
    set -- $line
    echo $2" "$who":"$1
    if [[ "$2"="$who" ]]
    then
        echo "change"
        client=$1
    fi
done < $file

echo $client

So now I changed the code as in one of the comment below, but now the caparison always false therefore the variable client is always empty

    while read -r line
do
    #IFS=" "
    #set -- $line
    #echo $2" "$who":"$1
    #if [[ "$2" = "$who" ]]
    a="${line% *}"
    l="${line#* }"
    if [[ "$l" == "$who" ]]
    then
        echo "hi"
        client="$a"
    fi
done < $file
ZeroVash
  • 546
  • 4
  • 20

3 Answers3

1

Change if [[ "$2"="$who" ]] to

if [[ "$2" = "$who" ]]

spaces around =

Example (for clarification):

who=A
while read line
do

    IFS=" "
    set -- $line
    echo $2" "$who":"$1
    if [[ "$2" = "$who" ]]
    then
        echo "change"
        client=$1
    fi
done < file #this is the file I used for testing

echo $client

Output:

A A:apple
change
D A:orange
apple

For who=D:

A D:apple
D D:orange
change
orange
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • This should work, are you sure there's no other problem in your code. It gives correct result in my case.. – Jahid May 18 '15 at 05:53
  • I changed my code and it is still doesn't work, just look my edit code in the original post – ZeroVash May 18 '15 at 13:07
  • your new code works on me, output is apple for A and Orange for D. You sure you haven't forgotten to put `who=A` or `who=D` in the code? I don't see that assignment in your code... – Jahid May 18 '15 at 14:49
  • 1
    Yes I find a mistake it is was in argument, thanks for your help – ZeroVash May 18 '15 at 16:24
1

If you have data in a file with each line like apple D and you want to read the file and separate then items, the parameter expansion/substring extraction is the correct way to process the line. For example (note $who is taken from your problem statement):

while read -r line
do
    fruit="${line% *}"       # remove from end to space
    letter="${line#* }"      # remove from start to space
    if [[ "$letter" == "$who" ]]
    then
        echo "change"
        client="$fruit"
    fi
done < $file

Short Example

Here is a quick example of splitting the words with parameter expansion/substring extraction:

#!/bin/bash

while read -r line
do
    fruit="${line% *}"
    letter="${line#* }"
    echo "fruit: $fruit  letter: $letter"
done

exit 0

input

$ cat dat/apple.txt
Apple A
Orange D

output

$ bash apple.sh <dat/apple.txt
fruit: Apple  letter: A
fruit: Orange  letter: D
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

You do need spaces around that = operator.

However, I think you're facing yet another issue as you're trying to change the value of the client variable from inside the while loop (which executes in a subshell). I don't think that will work; see this quesion for details.

Community
  • 1
  • 1
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92