derekg8881
derekg8881

Reputation: 109

How to create a for-loop with 2 list files

I am trying to create a for loop with two list files that will basically echo the script to change the dbowner of multiple databases. The list files contain multiple servers and the login name list contain multiple login names. But they are line separated in order to match each database with the login name.

This is what I have so far but it is obviously taking the first server name and looping it through each login name and then moves onto the next server name.

for servername in $(cat servername.list); do
   for loginname in $(cat loginname.list); do
    echo "USE $servername"
    echo "go"
    echo "EXEC sp_changedbowner '$loginname'"
    echo "go"
    echo "USE master"
    echo "go"
    echo ""
   done
done

I want the output to be this:

    USE server1
    go
    EXEC sp_changedbowner 'login1'
    go
    USE master
    go

    USE server2
    go
    EXEC sp_changedbowner 'login2'
    go
    USE master
    go

Upvotes: 0

Views: 80

Answers (1)

melpomene
melpomene

Reputation: 85767

You can do it like this:

while read -r; do
    server="$REPLY"
    read -r <&3 || break
    login="$REPLY"
    echo \
"USE $server
go
EXEC sp_changedbowner '$login'
go
USE master
go
"
done <servername.list 3<loginname.list

[Live demo]

Using input redirection you can open both files at the same time for the while loop. File descriptor 0 (standard input) is available, but 1 and 2 (standard output / standard error, respectively) are taken. The next free file descriptor is 3.

The loop then first reads a line from 0 (standard input, now connected to servername.list), then a line from 3 (now connected to loginname.list). read places the input in a variable called REPLY, which we copy over to server and login, respectively. These variables are used in the echo string to produce the desired output.

This repeats until one of the files runs out of lines.


By the way, you should not attempt to read lines with for.

Upvotes: 1

Related Questions