I need to put together a script that will take the contents of two different files (database name and database owner) and put them in two variables within a line:
foreach x (`cat /local/hd3/dba/tools/build_db_scripts/dbs`)
foreach z (`cat /local/hd3/dba/tools/build_db_scripts/dbas`)
echo "sql $x -u$z <script.sql>> script.log" >> /local/hd3/dba/tools/build_db_scripts/run_this
end
chmod 777 /local/hd3/dba/tools/build_db_scripts/run_this
Running the above only gives me the first database name on every line with the owner for all of the other databases as the second parameter. I need a line for each database name/dba combination.
Thank you !
For starters: You appear to missing the last end statement
foreach ()
foreach ()
end
end
I tried that but it simply iterated the same thing for each database name as it did for just the first name.
does it have to be csh/tcsh?
#!/bin/ksh
awk '
FNR==NR {f1[$0];next}
{f2[$0]}
END {
for (f1I in f1)
for (f2I in f2)
print "sql " f1I " -u" f2I " <script.sql>> script.log"
}' /local/hd3/dba/tools/build_db_scripts/dbs /local/hd3/dba/tools/build_db_scripts/dbas > /local/hd3/dba/tools/build_db_scripts/run_this
chmod 777 /local/hd3/dba/tools/build_db_scripts/run_this
1 Like
I guess you want to read from the two files simultaneously?
With /bin/sh (bash, ksh, zsh, ash, dash, ...)
#!/bin/sh
while read x && read z <&3
do
echo "sql $x -u$z"
done < /local/hd3/dba/tools/build_db_scripts/dbs 3< /local/hd3/dba/tools/build_db_scripts/dbas
csh/tcsh cannot do that (need an external tool and would remain clumsy).
For example, with the paste
command
#!/bin/csh
set f=0
foreach i ( `paste /local/hd3/dba/tools/build_db_scripts/dbs /local/hd3/dba/tools/build_db_scripts/dbas` )
if ( "$f" == 0 ) then
set x=$i
else
echo "sql $x -u$i"
endif
@ f = 1 - $f
end
But it does not really know which value comes from which file. If a line contains a space it will mix up.
1 Like
I think the OP wanted all the permutations between file1 and file2....
I used the sh code offered by MadeInGermany and it is working perfectly.
Many thanks!