1

I have to create a code, which is below this, and when the user enters their name, password, and then a password confirmation, I need to send their details to a .txt file with the time and date inside the file, any hints would be appreciated!

#!/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

Before I get the "just google it" thing, I have looked how to send specific commands to the file, but I need the date/time accessed sent to the .txt

3
  • You won't get "just Google it" here, but there is an expectation that you've done some research. The date command will give you the current date and time
    – roaima
    Nov 17 '20 at 23:27
  • I'm trying to get the date and time for the script being run if thats possible? Like when someone does bash pass.sh it sends the time accessed to a file
    – Blue Moon
    Nov 17 '20 at 23:30
  • 1
    oof I had the way you recommened in the wrong spot, I have it sending the date to a .txt file now.
    – Blue Moon
    Nov 17 '20 at 23:34
1

You could echo the date output to the file:

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

You can replace errors.log with any file name of your choosing.

If instead of logging errors (non-matching passwords) you wish to log correct (matching) passwords then move the echo command to the end (after the done line), like I did now with some more echos, now to a different file.

5
  • And instead of sending it to errors.log I could send it to a .txt file of my choosing?
    – Blue Moon
    Nov 17 '20 at 23:28
  • @BlueMoon Yes indeed you can.
    – Ned64
    Nov 17 '20 at 23:29
  • Added more possible outputs, select one or more and play with them!
    – Ned64
    Nov 17 '20 at 23:35
  • Thank you so much, I have been re-reading my netacad chapters and it tell you how to output certain things to a file, but not a script, I was losing my mind
    – Blue Moon
    Nov 17 '20 at 23:37
  • @BlueMoon You are welcome. If the answer still is valid for your question (I believe it is) then please accept it and vote up so others who find this will know.
    – Ned64
    Nov 17 '20 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.