gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:25 25 4
gpt4 key购买 nike

我制作了一个备份脚本,使用 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." 一次,然后再次询问凭据。我该怎么做?

最佳答案

vars 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

关于linux - 如何处理 shell 登录脚本中的错误密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com