-1

I have this code in Elastix2.5 (CentOS):

for variable in $(while read line; do myarray[ $index]="$line"; index=$(($index+1)); echo "$line"; done < prueba);

This extract the values for each line from "prueba" file.

Prueba file contents passwords like this:

Admin1234
Hello543
Chicken5444

Dino6759
3434Cars4

Adminis5555

But, $variable only get values from lines where there are letters, I need that it get NULL values from blank lines. How can I do it?

juanute
  • 11
  • 1
  • 5

2 Answers2

0

Your problem is use of a for loop with a command substitution ($(...)); let's look at this simple example:

$ for v in $(echo 'line_1'; echo ''; echo 'line_3'); do echo "$v"; done
line_1
line_3

Note how the empty string produced by the 2nd echo command is effectively discarded. Analogously, any empty lines produced by your while loop are discarded.

The solution is to avoid for loops altogether for parsing command output:

In your case, simply use only the while loop for iterating over the input file:

while read -r line; do 
  myarray[index++]="$line"
done < prueba
printf '%s\n' "${myarray[@]}"
  • -r was added to ensure that read doesn't modify the input (doesn't try to interpret \-prefixed sequences) - this is good practice in general.

  • Note how incrementing the index was moved directly into the array subscript (index++).

  • printf '%s\n' "${myarray[@]}" prints all array elements after the file's been read, demonstrating that empty lines were read as well.

mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

You can use is_null function.

is_null($a)

http://php.net/manual/en/function.is-null.php

manowar_manowar
  • 1,215
  • 2
  • 16
  • 32