我知道你可以在運行腳本前後在終端中輸入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