我必须创建一个代码,位于此下方,当用户输入他们的姓名、密码,然后输入密码确认时,我需要将他们的详细信息发送到 .txt 文件,并在文件中包含时间和日期、任何提示将不胜感激!
#!/bin/bash
read -p "Username: " username
while true; do
read -s -p "Please enter your password: " password
echo
read -s -p "Please re-enter your Password: " password2
echo
[ "$password" = "$password2" ] && break
echo "Please ensure your passwords match!"
done
在我得到“只需谷歌它”之前,我已经了解了如何将特定命令发送到文件,但我需要将访问的日期/时间发送到 .txt
答案1
您可以将date
输出回显到文件:
echo "script started at `date`" >> debug.log
read -p "Username: " username
echo "${username} entered at `date`" >> debug.log
while true; do
read -s -p "Please enter your password: " password
echo
read -s -p "Please re-enter your Password: " password2
echo
[ "$password" = "$password2" ] && break
echo "Please ensure your passwords match!"
echo "Invalid password entered at `date` for user ${username}" >> errors.log
done
echo "${username} entered a correct password at `date`" >> debug.log
您可以替换errors.log
为您选择的任何文件名。
如果您希望记录正确(匹配)的密码而不是记录错误(不匹配的密码),则将命令移至echo
末尾(该done
行之后),就像我现在使用更多echo
s 所做的那样,现在移至不同的文件。