0

For some reason in the code below for loop treats args as array and wc -l can count the lines correctly but I can't get the $(#args[@]) to produce the correct count

function doSomthing() {
local i args a
args=$1;
a=("1" "2" "3" "4");
i=0
  echo wc =`wc -l <<< "$args"`;
  for arg in $args; do
    ((i++))
    echo "$i"
  done;
  echo i = $i
  echo a = ${#a[@]}
  echo args = ${#args[@]}
  echo $args
}

The output of this function is

$> doSomthing $'1\n2\n3\n4'
wc =       4
1
2
3
4
i = 4
a = 4
args = 1
1 2 3 4

2 Answers 2

4

args is not an array; it is simply a string that contains embedded newlines. That means, if you try to treat it as an array, it will appear as if you defined it as

args=( $'1\n2\n3\4' )

not

args=(1 2 3 4)
Sign up to request clarification or add additional context in comments.

Comments

0

Problem solved!

I just needed to put $1 inside parenthesis.
For some reason when I tried it before it did not work but now it does.


Here is the new code:

function doSomthing () {
    local i args a;
    args=( $1 );
    a=("1" "2" "3" "4");
    i=0;
    echo wc =`wc -l <<< "$args"`;
    for arg in $args; do
        ((i++));
        echo "$i";
    done;
    echo i = $i;
    echo a = ${#a[@]};
    echo args = ${#args[@]};
    echo ${args[@]}
}



And here is the new output:

$> doSomthing $'1\n2\n3\n4'
wc = 1
1
i = 1
a = 4
args = 4
1 2 3 4

Cheers ;-)

8 Comments

What do you expect as output if you run doSomething "*"? I rather suspect you might be surprised by what you get as output, if you run it from a non-empty directory.
I am not sure I understand your question. I just did and got the same output as before (which is what I expected) ls -1 1 2 3 4 doSomthing "*" wc = 1 1 i = 1 a = 4 args = 4 1 2 3 4 I cannot show the output properly in a comment, but it is the same as before. sorry.
If you want you can try viewing the following as html: <br> $> ls -1 <br> 1 <br> 2 <br> 3 <br> 4 <br> $> doSomthing "*" <br> wc = 1 <br> 1 <br> i = 1 <br> a = 4 <br> args = 4 <br> 1 2 3 4
Why do you expect that output to be 1 2 3 4, instead of being *? It was quoted, after all: That's supposed to make it literal. Ifyou're expanding the glob, not treating it as a single argument when it was passed in a way that makes it a single argument, that's a bug.
So, let's say you had a file called *** i am just one file *** .txt. Silly name, with a bunch of spaces in it, but those exist sometimes. Right now, you couldn't make your code treat that filename as just one argument, no matter how you quote and escape it. That's my point: The way you're handling your arguments, right now, is wrong. In doSomething "1 2 3 4" "5 6 7 8", you should have exactly two arguments, the first being 1 2 3 4, and the second being 5 6 7 8.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.