Reputation: 2214
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!
Upvotes: 2
Views: 128
Reputation: 784958
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")'
Upvotes: 2