[英]How To Create Simple Log File From Script
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.我正在尝试创建一个名为“users.txt”的简单日志文件,其中包含输入的用户名、全名和主目录。 I also want to include the time that the script was initially running.
我还想包括脚本最初运行的时间。
Any suggestions?有什么建议?
The script name is called catbash.sh脚本名称叫做 catbash.sh
I have tried things such as catbash.sh > user.txt我尝试过诸如 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.您必须从 sh 脚本内部进行日志记录。 For ex, you could
echo
the info that you want on the terminal (which you are already doing) and then append | tee -a $LOGFILE
例如,您可以在终端上
echo
显您想要的信息(您已经在做),然后附加| 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). | tee -a $LOGFILE
(在顶部定义 LOGFILE,因此如果您需要不同的文件名/位置,则只需更改一次)。
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.
例如,一个更简单的记录器 func 可能需要 2 个参数 - 消息、文件名。 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.根据您问题中当前的信息,这是我能给出的最佳指示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.