I'm trying to create a simple log file called "users.txt" that contains the username, full name, and home directory of the username entered. I also want to include the time that the script was initially running.
Any suggestions?
The script name is called catbash.sh
I have tried things such as catbash.sh > user.txt
But I have no idea how to get specific information etc.
clear
LOGFILE=/home/student/gg193/FileTypes/TextFiles/ShellScripts/user.txt
read -p "What is your username?" username
read -p "May I know your name please? " name surname
echo "$(date) $username $name $surname $LOGFILE" >> $LOGFILE
TIME=$(date "+%H")
if [ $TIME -ge 0 -a $TIME -lt 12 ]
then
echo "\nGood Morning, $surname"
elif [ $TIME -ge 12 -a $TIME -lt 18 ]
then
echo "\nGood afternoon $surname"
else
echo "\nGood evening $surname"
fi
echo "1. nonblank\n2. number\n3. ends\n4. nends\n"
while :
do
read INPUT_STRING
case $INPUT_STRING in
nonblank)
NonEmptyLine=$(cat catbash.sh | sed '/^\s*$/d' | wc -l)
echo "\nNumber of Non-Empty Lines are:" $NonEmptyLine
break
;;
number)
EmptyLine=$(grep -cvP '\S' catbash.sh)
echo "\nNumber of Empty Lines are:" $EmptyLine
break
;;
ends)
echo "================================================================================="
sed 's/$/$/' catbash.sh
echo "================================================================================="
break
;;
nends)
NonEmptyLine=$(cat catbash.sh | sed '/^\s*$/d' | wc -l)
echo "\nNumber of Non-Empty Lines are:" $NonEmptyLine
EmptyLine=$(grep -cvP '\S' catbash.sh)
echo "\nNumber of Empty Lines are:" $EmptyLine
echo "================================================================================="
sed 's/$/$/' catbash.sh
echo "================================================================================="
break
;;
*)
echo "\nSorry, I don't understand"
;;
esac
done
DURATION=$(ps -o etime= -p "$$")
echo "\nAmount of time that has passed since the script was initially executed:" $DURATION
echo "\nThanks for using catbash.sh!"
You have to do the logging from inside the sh script. For ex, you could echo
the info that you want on the terminal (which you are already doing) and then append | tee -a $LOGFILE
| tee -a $LOGFILE
(define LOGFILE at the top so you only have to change once if you need a different file name/location).
As further improvements you can look at defining your own logger function or even explore existing ones. For ex a simpler logger func could take 2 args - message, file name. The message itself could be composed of the user name, date/timestamp etc as you want.
With the info currently in your question, this is the best pointer I can give.
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.