Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

If I have the following bash variable:

$ echo "${pos}"

201
719
744
205
354

...the following produces...

!#bin/bash
(
    IFS=: 
    awk -v str2="$pos" -v sep="[$IFS]" '
        BEGIN {
            m = split(str2, b, sep)
            for (i=1; i<=m; ++i) {print b[i]}
        }
    '
)

-----------------
$ ./myscript.sh

201
719
744
205
354

...but then doing...

(
    IFS=: 
    awk -v str2="$pos" -v sep="[$IFS]" '
        BEGIN {
            m = split(str2, b, sep)
            for (i=1; i<=m; ++i) {print b[i]+10}
        }
    '
)

------
./myscript.sh

211

...so the addition is working, but not printing results for all elements. Why not?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

It is your IFS=: not set properly. So split() does not fill the array with values but fills it it with one value the str2, so in your for loop you print b[i] but actually you print once b[1] which is your entire str2 and because it has new lines you think it prints members of array b, but if you check m the retrn value of split() it is 1. Remove IFS=: and your script should work properly.

share|improve this answer
    
thank you so much. this worked. the fact that b[1] appears to be b[i] was what was confusing me and thank you for pointing this out. –  brucezepplin Mar 10 at 10:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.