Dear Unix and Linux users,
Good evening to all.
I'm new to this community and thank you for having an wonderful forum.
Dear members i had to create almost some 300 shell script files for a particular task.
I tried something like this....
#!usr/bin/sh
fname=epdb_jobs
for x in `cat $fname`
do
if [ ! -s $x.epdb.com ]
then
echo Creating file...
cat > $x.epdb.com <<EOF
echo fname1=num_$x.txt
for y in `cat $fname1`
do
head -$y t_$x.mol2 | tail -n 200 | head -48 >>rlig_$x.mol2
done
stop
EOF
fi
echo "done $x"
done
By using this script i read the part of the filename from epdb_jobs file and then
i want a new file to be created such that 1RAA.epdb.com
this file should be like the one shown below
fname1=num_$x.txt
for y in `cat $fname1`
do
head -$y t_$x.mol2 | tail -n 200 | head -48 >>rlig_$x.mol2
done
I have to create 300 different files like this I don't know where i'm going wrong.
please help me.Thanks in advance.
Neha.
Within the here document any Shell special characters must be escaped with a reverse solidus character to stop the current Shell actioning them:
cat > $x.epdb.com <<EOF
echo fname1=num_\$x.txt
for y in \`cat \$fname1\`
do
head -\$y t_\$x.mol2 | tail -n 200 | head -48 >>rlig_\$x.mol2
done
stop
EOF
#!/usr/bin/sh
IFS="
"
fname=epdb_job
for x in `cat $fname`
do
if [ ! -s $x.epdb.com ]
then
echo Creating file...
cat > $x.epdb.com <<EOF
echo fname1=num_$x.txt
for y in \`cat \$fname1\`
do
head -\$y t_\$x.mol2 | tail -n 200 | head -48 >>rlig_\$x.mol2
done
EOF
fi
echo "done $x"
done
The problem was that the back-ticks needed escaping as it was trying to run that cat command. I've also escaped the variable inside the back-ticks as otherwise it's not printed.
The IFS="
"
Bit makes sure that the for x in `cat $fname` skips spaces and only acts on new lines, if you use spaces to separate entries then take out the IFS bit.