0

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
melpomene
  • 84,125
  • 8
  • 85
  • 148
derekg8881
  • 107
  • 7
  • assuming those 2 files are really in matching order, you're better to merge them, i.e. `paste server.txt user.txt > server_user.txt`, and then `while read server user ; do ...` done < server_user.txt` or similar. YOu can write all those `echo`s as one `printf "USE %s\ngo\nEXEC sp_change %s\n.....\ngo\n" "$server" "$user";` Good luck. – shellter Jun 20 '19 at 20:09
  • Instead of having two lists, Can you have one list with the server name and login name separated by a separator(some special character like | ^) and split it inside of the for loop to make use of it. [I'm referring to this.](https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash) And this avoids the nested loop. – Srinivas Mallisetty Jun 20 '19 at 20:12

1 Answers1

1

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148