我在執行數組的代碼和if
語句塊有問題,以檢查文件是否處於活動狀態。爲了進行調試,我使用-x
運行腳本,並且在比較[ … ]
中的兩個值時缺少該值。我已經通過搜索網站的數量來找到答案,但一直沒有能夠這樣做。如果該文件處於活動使用狀態或未被使用,將會刪除哪些內容,以便對比該塊的問題或修復情況?Bash - 如果語句沒有提取值
#!/bin/bash
#---- parameters check----#
if ([ "$2" -le "0" ])
then
echo 'Invalid number of days...'
echo ''
echo 'The number of days must be greater then 0'
exit 1;
fi
#----variables----#
Days="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
FILEFOLDER="$1"
logfile=~/"log_purge_$timestamp.txt"
#----foldercheck----#
if [ -d $FILEFOLDER ]
then
echo "File Folder Directory $FILEFOLDER is set." | tee "$logfile"
else
echo "File Folder Directory $FILEFOLDER does not exist! Process will
Exit."
exit 1;
fi
#----Find if the file is In Use through Array and IF---#
files_check=($(find "$FILEFOLDER" -type f -mtime +"$Days" -print))
for f_c in "${files_check[@]}"
do
if [ "${f_c}" == "$(lsof | grep "$f_c")" ]
then
echo "$f_c is in use rite now,quiting the process"
exit 1
fi
done
echo "No file is in Use right now. Safe to remove"
#---Total Number of Files & Purge ---#
files_num=$(find "$FILEFOLDER" -type f -mtime +"$Days" | wc -l)
echo "Total number of files" $files_num | tee -a "$logfile"
find "$FILEFOLDER" -type f -mtime +"$Days" -print | sed "s|$FILEFOLDER||g" | tee -a $logfile
echo "Purging the files now ....."
find "$FILEFOLDER" -type f -mtime +"$Days" -exec rm {} \;`
刪除'(「'''''$ 2」-le「0」]'' – BroSlow
''''如果[cond1(exp)cond2] ??? – iking15
我想這裏可能並不重要,它只會運行在一個應該返回測試結果的子shell中。但一般情況是,只要執行'[$ 2 -le 0]'或'(($ 2 <0))' – BroSlow