Batch code

Hi Friends,

I have a script like this

cat script

#!/bin/bash
#Name: name
#Task: name
#$ -e /path/to/error/logfile/err.log
#$ -o /path/to/output/logfile/out.log

I have a list of commands like this

cat commands

cat 1.txt > 2.txt
cat 23.bed > 789.alm
zcat 1.gz > 1.txt

My request is that

Now for each line in my command file, I would like to make a script file. So, that I get 3 script files with increasing err and out log file numbers like this.

cat script1

#!/bin/bash
#Name: name
#Task: name
#$ -e /path/to/error/logfile/err1.log
#$ -o /path/to/output/logfile/out1.log
cat 1.txt > 2.txt
cat script2

#!/bin/bash
#Name: name
#Task: name
#$ -e /path/to/error/logfile/err2.log
#$ -o /path/to/output/logfile/out2.log
cat 23.bed > 789.alm
cat script3

#!/bin/bash
#Name: name
#Task: name
#$ -e /path/to/error/logfile/err3.log
#$ -o /path/to/output/logfile/out3.log
zcat 1.gz > 1.txt

It is basically joining two text files with increasing err and out log file names.

The path to the err and out files is always the same.

Thanks in advance.

Try something like:

#!/bin/ksh
n=1
while read -r cmd
do      (cat script;printf "%s\n" "$cmd") > script$n
        n=$((n + 1))
done < commands

I used ksh, but this will work with any POSIX conforming shell.

Hi Don,

Thanks for your time.

It works. But it prints all commands into one single script instead of splitting it into multiple scripts. Any pointers are highly appreciated.

No, it does not. It will create as many output files as there are lines in the file commands . With the example you gave it creates three files: script1 , script2 , and script3 .

Instead of saving my script in a file, making it executable, and then executing the file; did you save my script in a file and run it using a different shell? If so, what shell did you use?