2

I am submitting a series of batch jobs in a bash script.

The batch script (script.sh) is as follows:

samtools merge $1 $2

$1 is the output and it is only one single file. So, it is no problem.

$2 is the input but it is not only one file, it is several files and it is different in every directory. The bash script is as follows:

for i in directory1 directory2 
do
cd $i
d=$(echo *specific_prefix.txt)
echo $d
sbatch script.sh ${i}.output $d
cd ..
done

Running as above, the batch script only takes the first element of $d as its variable. Is there a way for a bash variable ($2) to take varying number of variables, for example for it to be a list whose length can be determined in another variable?

Edit:

I have the sbatch job script (merge.sh) as:

#!/bin/bash -l

arg1="$1"
shift 1
arg2=("$@")

samtools merge arg1 arg2

And I run it using a bash script:

#!/bin/bash -l

for i in directory
do
cd $i
d=$(echo *RG.sort.dedup.bam)
sbatch merge.sh ${i}.output $d
cd ..
done 

The error is: samtools merge: fail to open "arg2": No such file or directory

What could be the error in the script? Thanks!

2
  • 1
    Sorry don't know what is samtools. Also you need to use samtools merge "$arg1" "${arg2[@]}. Also I don't know what is sbatch before merge.sh Commented Aug 1, 2018 at 8:32
  • 1
    I made the changes you suggested and it is working now! Thank you very much! samtools is just the name of a programme in bioinformatics and sbatch is a command we use to submit batch jobs into our computer cluster. Thank you again! Commented Aug 1, 2018 at 8:44

1 Answer 1

2

You can use shift command after storing fixed arguments and use $@ as below script. You can use a shell array to store variable number of arguments starting from 3rd position.

# store first 2 arguments in variables
arg1="$1"
arg2="$2"

# using shift 2 discard first 2 arguments
shift 2

# now store all remaining arguments in an array
arg3=("$@")

# examine our variables
declare -p arg1 arg2 arg3

Now run it as:

./script.sh merge abc foo bar baz
declare -- arg1="merge"
declare -- arg2="abc"
declare -a arg3='([0]="foo" [1]="bar" [2]="baz")'
Sign up to request clarification or add additional context in comments.

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.