0

I try to simply concat the arguments if they are not the same..... what is wrong with this simple code for concat???

#!/bin/sh
result=$1

shift
while [ "$#" -ne "0" ]
do

  next=$1

  if [ $result = $next ]
  then
        :
  else

     $result="$result $next"
  fi
shift
done

echo "$result"

if i run it :

$ ./foo a b c
./foo: a=a b: not found
./foo: a=a c: not found
a

why a=a b: not found??? where this came from???

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by omrsafetyo
0

> $result="$result $next"
Remove the first $ perhaps?
Like your other assignment earlier on.

0

The above from Salem's post needs to be done to fix your issue.. but you may also want to fix your if statement.

if [ "$result" = "$next" ]

The data in these variables are strings and need to be compared as such.

It probably isn't necessary, but you may want to say:

if [ "${result}" = "${next}" ]
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.