我知道你可以在运行脚本前后在终端中输入script nameOfLog.txt
和exit
来创建输出日志,但是我想将它写入实际脚本中,以便创建一个自动登录。我遇到了exec >>log_file 2>&1
行:在bash中创建详细的自我跟踪日志
代码将输出重定向到日志文件,用户不能再与其交互。我怎样才能创建一个日志,它只是基本上复制输出中的内容?
而且,是否有可能让它自动记录被复制的文件的进程?例如,如果/home/user/Deskop/file.sh中的文件被复制到/ home/bckup,是否也可以在日志中打印该文件,或者是否必须手动编写该文件?
是否有可能记录运行整个过程所需的时间,并计算已处理的文件和目录的数量,还是我将不得不手动编写该文件和目录?
我未来的自我感谢所有的帮助!
这里是我的全部代码:
#!/bin/bash
collect()
{
find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup #xargs handles files names with spaces. Also gives error of "cp: will not overwrite just-created" even if file didn't exist previously
}
echo "Starting log"
exec >>log_file 2>&1
timelimit=10
echo "Please enter the directory that you would like to collect.
If no input in 10 secs, default of /home will be selected"
read -t $timelimit directory
if [ ! -z "$directory" ] #if directory doesn't have a length of 0
then
echo -e "\nYou want to copy $directory." #-e is so the \n will work and it won't show up as part of the string
else
directory=/home/
echo "Time's up. Backup will be in $directory"
fi
if [ ! -d ~/bckup ]
then
echo "Directory does not exist, creating now"
mkdir ~/bckup
fi
collect
echo "Finished collecting"
exit 0