0

I'm sorry in advance that the question may be confusing.

I'm working with a command-line interface of a remote program, fw that manages medical images. The program has their own documentation of commands that are similar in manner to bash commands (ls in this example, in line 3 of my for loop).

IDs is an array of scan IDs, which I use to access the paths within the program, fw, with the previously mentioned ls command. The echo line is just for my testing purposes. I would like to add each $line to an array called ls. Problem is, spacing in the output with back `` from fw ls has spaces, and I'm trying to make it one string, since echo ${#ls[@]} gives me a much larger number than what the true number of scans are (843 vs 3 for this example, 843 is from my actual debugging). The true number of what I'm working with is 67 scans to ls instead of 3, but I only included three 'fake' outputs of line to show you what the output looks like.

Debugging:

>>> echo ${line[@]}

admin Dec 3 15:37 2010-12-03 16:02:11
admin Mar 6 09:23 2014-03-06 09:31:16
admin Jul 23 11:12 2016-07-23 11:26:10

>>> echo ${#ls[@]}
843

### I should be getting 
>>> echo ${#ls[@]}
3

How do I modify my line for appending to ls in my for loop?

for idx in ${IDs[@]}
do
   line=`fw ls "myuniversity/test/${IDs[$idx]}/"`
   echo $line
   ls+=("$line")
done
florence-y
  • 751
  • 3
  • 8
  • 18
  • `idx` is the value you want, not an index into `IDs`. `line=$(fw ls "myuniversity/test/$idx")`. – chepner Nov 07 '19 at 16:08
  • You are capturing the values just fine. Your error is failing to quote the arguments to `echo`. This is a very common FAQ. – tripleee Nov 07 '19 at 16:11
  • You should also quote `"${IDs[@]}"`. The line `ls+=("$line")` should be fine. – chepner Nov 07 '19 at 16:11
  • @tripleee `echo $line` doesn't explain the claimed output of `echo ${#ls[@]}`, though. – chepner Nov 07 '19 at 16:12
  • We can only speculate that they are doing something like `ls=($(command))` and expecting he result to be an array of lines, but getting an array of tokens. There is also no way the output could be split across three lines like that if they `echo` the value without quotes. – tripleee Nov 07 '19 at 16:15
  • Speculate? It's in the question: `ls+=$("line")`. I agree the question needs some work, but closing this as a duplicate is premature. – chepner Nov 07 '19 at 16:21

0 Answers0