0

I have a for loop in bash that operates in an array.

for i in 1..16; do some operation in some file; done

Now, for first 1, or few elements, the operation is not successful, the loop crash and does not iterate over the next elements. Is there any way to keep the iteration running until the last element, whether it fails or not in any step? The actual command looks like

for i in ${list3[@]}; do impute4 -g chr21.haps -h chr21.haplotypes.hap.gz -l chr21.legend.gz -m map_chr21.txt -o test$i.impute -int ${list1[@]}[$i] ${list2[@]}[$i] -Ne 20000 -buffer 1000 -seed 54321 & done

Here

{list3[@]}=0 1 2 3 4 ..16
{list1[@]}=0 3e6 6e6 9e6 12e6 ..48e6
{list2[@]}=3e6 6e6 9e6 12e6 15e6 ..51e6

The -int flag takes two values start end of a region. For first few elements the program crashes because there is no snps in those regions (0 3e6 or 3e6 6e6). so I need to move on to the next regions. How could I do that.

  • Does this answer your question? [Is there a TRY CATCH command in Bash](https://stackoverflow.com/questions/22009364/is-there-a-try-catch-command-in-bash) – tdserapio May 05 '21 at 11:55
  • 1
    It's impossible to answer this meaningfully without de code. – Roadowl May 05 '21 at 11:57
  • Hi @Roadowl, I have edited my question with actual code an more explanation. Hope it clarifies my issue now. – zillur rahman May 05 '21 at 12:45
  • 1
    I cannot parse `${list1[@]}[$i]`. Do you mean `${list1[$i]}` (the i'th element in list1)?. – Roadowl May 05 '21 at 12:50
  • Thanks! ${list1[$i]} does the iteration but getting no output. eventually it terminates without completing the task. – zillur rahman May 05 '21 at 13:09
  • Also if I use `&` the process runs in parallel but in background. So if program does not work for one element in array, it crashes. If I use `|| true` the loop works fine but one by one. Is there any way to run the loop parallel for all element in the array without using `&` – zillur rahman May 06 '21 at 17:08

1 Answers1

0

Is there a TRY CATCH command in Bash This article may help. I think you are looking for the try, catch statement. This catches the error, and you may simply deflect the error if you intend to do something else or continue on with the loop rather than stop when the error occurs.

tdserapio
  • 19
  • 1
  • 8
  • Thanks. The try catch statement might not be helpful here, because I am running the same command on an array. not sure though! – zillur rahman May 05 '21 at 12:42