简体   繁体   中英

bash script stuck in single line

I have below command for bash script:

SQLSERVER=( "localhost" "testserver") 
DATABASE="master" 
USERNAME="sa"
PASSWORD="test@12345"
for item in "${SQLSERVER[@]}"
do
    sqlcmd -S $item -d $DATABASE -U $USERNAME -P $PASSWORD -I -h-1  -Q "set nocount on;SELECT name as username FROM SYSUSERS WHERE NAME LIKE '%test%'" >> $item.txt
    while IFS= read -r line; do
        if [[ ! -z $line ]]
         then
         #echo "Text read from file: $line"
         sqlcmd -S $item -d $DATABASE -U $USERNAME -P $PASSWORD -I -b -h-1  -Q "set nocount on;drop user $line"
         fi
    done < $item.txt
done 

script is stuck at sqlcmd -S $item -d $DATABASE -U $USERNAME -P $PASSWORD -I -b -h-1 -Q "set nocount on;drop user $line" . and this is going to in loop, it's goes infinity. how to get out from this if command.

I got the error as below:

Msg 15151, Level 16, State 1, Server 66752ccaf826, Line 1
Cannot drop the user 'test2', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Server 66752ccaf826, Line 1
Cannot drop the user 'test2', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Server 66752ccaf826, Line 1
Cannot drop the user 'test2', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Server 66752ccaf826, Line 1
Cannot drop the user 'test2', because it does not exist or you do not have permission.

For create User:

CREATE USER test1 WITHOUT LOGIN;
CREATE USER test2 WITHOUT LOGIN;

Assuming you want the while loop to end with the end-of-file of "$item.txt", then you need to change

if [[ ! -z $line ]]

to

if [[ -z $line ]] ; then break ; fi

and remove the other " fi ".

As for the command

sqlcmd -S $item -d $DATABASE -U $USERNAME -P $PASSWORD -I -b -h-1  -Q "set nocount on;drop user $line"

you need to resolve the issue reported, from the shell command line , before attempting to do that within the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM