如何在shell中的登录脚本中处理错误的密码?

时间:2019-04-30 08:27:57

标签: linux shell while-loop samba

我制作了一个备份脚本,该脚本使用smbclient命令将文件上传到文件共享Windows服务器。我的脚本可以正常工作,但是如果用户键入了错误的用户名或密码,脚本将停止。我希望它再次要求登录名和密码。

我试图这样做:

#!/bin/bash

unset user
unset pass
testLoginValue=false

while [[ "$testLoginValue" = false ]]; do
        while [ -z ${user} ]; do
                read -p "Username: " user
        done
        while [ -z ${pass} ]; do
                read -sp "Password: " pass
        done

        testLogin=$(smbclient '\\my.windows.server.ip\folder\' $pass -U domain\\$user -c 'help') # This connect to the server and get the result of the "help" command

        if [[ $testLogin == "session setup failed: NT_STATUS_LOGON_FAILURE" ]]; then
                echo "Username or password incorrect."
        else
                echo "Successful connection."
                testLoginValue=true
        fi
done

echo "the script will continue..."

问题在于,使用此代码,脚本将循环回显"Username or password incorrect."而不会停止。我希望它只回显一次"Username or password incorrect.",一次要求提供凭据。我该怎么办?

1 个答案:

答案 0 :(得分:1)

变量userpass必须再次为unset。从

开始
#!/bin/bash
testLoginValue="false"

while [[ "${testLoginValue}" = "false" ]]; do
   unset user
   unset pass
   while [ -z "${user}" ]; do
      read -p "Username: " user
   done
   while [ -z "${pass}" ]; do
     read -sp "Password: " pass
   done

离题:也许将循环更改为

while :; do
   ...
   if [[ "${somevar}" = "Successful connection." ]]; then
      break
   fi
done