2

I am using bash to call tool written in java (gatk) and I need to pass multiple arguments from array as input arguments. I tried it this way, but it seems not working. Could you please help me, how to solve it?

Code:

java $GATK \
    -T GenotypeGVCFs  \
    -R $ref \
    -o output.vcf \
    for foo in array
    do
        --variant  $foo \
    done

What i want to be called:

java $GATK \
    -T GenotypeGVCFs  \
    -R $ref \
    -o output.vcf \
    for foo in array
    do
        --variant  file1 \
        --variant  file2 \
        --variant  file3 ...etc
    done

edit: sorry for misunderstandings

array=("file1","file2","file3"...)

Thanks

3
  • What's the contents of array? Where do you get file 1,2,3? Commented Jun 20, 2017 at 12:09
  • 2
    You can't just throw a for inside another command. It doesn't evaluate to an expression. You get an actual for foo in array do... in your command line. Commented Jun 20, 2017 at 12:13
  • edited - array=("file1","file2","file3"...) @RealSkeptic - yea I know this doesnt work, so I am looking for solution how to pass into command multiple arguments from array :) Commented Jun 20, 2017 at 12:34

4 Answers 4

5

I assume that what you actually want is that if array contains a b c, to have the command

java $GATK \
    -T GenotypeGVCFs  \
    -R $ref \
    -o output.vcf \
    --variant a --variant b --variant c

If that is so, you can prepare a second array:

array=("file 1" "file 2" "file 3")
declare -a fullarray
for i in "${array[@]}"
do
    fullarray+=( --variant "$i" )
done

And then

java $GATK \
    -T GenotypeGVCFs  \
    -R $ref \
    -o output.vcf \
    "${fullarray[@]}"

This will also make sure that if any of the names in array contains a space, it will still be passed as a proper parameter and not split into two (assuming that you didn't mess it up when you added it to the array).

Sign up to request clarification or add additional context in comments.

3 Comments

quoting fullarray like that will pass "--variant a" as a single argument instead of as "--variant" and "a" separately, which will probably not work as intended
@wich Why do you think that? It expands each parameter in the array as a separate word.
Ah indeed, my mistake, I thought you were changing the strings, not the arrays.
0

With echo and $():

array=(file1 file2 file3)
java $GATK \
    -T GenotypeGVCFs  \
    -R $ref \
    -o output.vcf \
    $(for foo in ${array[*]}
    do
        echo -n " --variant  $foo"
    done)

1 Comment

No; this negates any advantage using an array in the first place would provide.
0

You can do this with the following:

java $GATK \
    -T GenotypeGVCFs  \
    -R $ref \
    -o output.vcf \
    ${array[*]/#/ --variant }

2 Comments

What about possible spaces in the file names?
@RealSkeptic OP didn't bother about spaces so I didn't either
0

@RealSkeptic's answer is the best. I'd write, for readability:

array=( "file 1" "file 2" "file 3" )
args=(
    "$GATK"
    -T GenotypeGVCFs 
    -R "$ref"
    -o output.vcf
)
for foo in "${array[@]}"; do args+=( --variant "$foo" ); done

java "${args[@]}"

Comments

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.