ian_informatics
ian_informatics

Reputation: 51

Create a new file each time through a bash for loop

I'm trying to create multiple shell scripts from a file with variable name. I have a bash for loop that creates the necessary lines but I want a new file each time through the loop. The script I currently have puts everything into one file. The input file (test.txt) has each variable on a separate line:

a
b
c

Here is the code I currently have:

#!/bin/bash
num=0
echo $num
for x in $(<test.txt)
do
    echo \#\!/bin/bash
    echo \#SBATCH -N 1
    echo \#SBATCH -t 6:00:00
    echo \#SBATCH --job-name=${x}
    echo \. \~/\.profile
    echo time java -jar trimmomatic.jar PE -threads 20 ${x}_R1.fastq ${x}_R2.fastq
    num=$((num+1))

done  > trim_${num}.sh
echo $num
exit

This would write three loops with a,b,c variables to trim_0.sh. I want the a loop to be in trim_0.sh, the b loop to be in trim_1.sh, and the c loop to be in trim_2.sh.

Upvotes: 0

Views: 2582

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74595

You can wrap the body of your loop in a block, like this:

while read -r x
do
    {
        echo '#!/bin/bash'
        echo '#SBATCH -N 1'
        echo '#SBATCH -t 6:00:00'
        echo "#SBATCH --job-name=$x"

        echo '. ~/.profile'
        echo "time java -jar /lustre/software/bioinfo/trimmomatic/0.32/trimmomatic.jar PE -threads 20 ${x}_R1.fastq ${x}_R2.fastq"
    } > "trim_$((num++)).sh"
done < test.txt

I've used quotes around each echo (single quotes where there aren't any shell variables involved, double quotes where there are) and removed your backslash escapes, which I don't think were necessary.

I've also used a while read loop, rather than a for loop to read the file. This makes no difference in your simple case but as pointed out in the comments, is the correct approach.

Alternatively, this may also be a good opportunity to use a heredoc:

while read -r x
do
    cat <<EOF >"trim_$((num++)).sh"
#!/bin/bash
#SBATCH -N 1
#SBATCH -t 6:00:00
#SBATCH --job-name=${x}
. ~/.profile
time java -jar /lustre/software/bioinfo/trimmomatic/0.32/trimmomatic.jar PE -threads 20 ${x}_R1.fastq ${x}_R2.fastq
EOF
done < test.txt

Upvotes: 1

Related Questions