我正在尝试为我的.bashrc
文件编写以下代码以备份.bash_history
,但我无法弄清楚结尾,这是无效的代码:
if [ $name -ot "$name-$minus$old" ]
then
printf "%s\n" ".bash_history will be cleared soon, backing up....!"
cp ~/.bash_history "$new_name"
fi
如何防止每次启动shell/terminal
且满足(wc -l < ~/.bash_history) >= 99000
条件时都无法创建新文件?
该代码应该执行以下操作:
如果我的.bash_history
文件距离定义的HISTSIZE=100000
和HISTFILESIZE=100000
行的大小不超过1000行,请备份该文件。
.bash_history
是否为99000行或更多。 ~/.bash_history.old
是否存在,是否不使用该文件名。i
比文件名已使用的数字大一位。(文件名-4.old)cp
到new_name
的文件if (( $(wc -l < ~/.bash_history) >= 99000 ))
then
name=~/.bash_history
old=.old
if [[ ! -e ~/.bash_history.old ]]
then
printf "%s\n" ".bash_history will be cleared soon, backing up....!"
cp $name ~/.bash_history.old
else
i=0
if [[ -e $name$old ]]
then
while [[ -e "$name-$i$old" ]]
do
let i++
done
fi
if [[ "$i" -ge 20 ]]
then
printf "%s\n" "You need to arhive your history files they are mounting up!!!"
fi
new_name=$name-$i$old
minus=$(( i - 1 ))
if [ $name -ot "$name-$minus$old" ]
then
printf "%s\n" ".bash_history will be cleared soon, backing up....!"
cp ~/.bash_history "$new_name"
fi
fi
fi
这是shellcheck.net
的结果:
Line 16:
let i++
^-- SC2219: Instead of 'let expr', prefer (( expr )) .
########################333############# backup history
if (( $(wc -l < ~/.bash_history) <= 99900 ))
then
name=~/.bash_history
old=.old
if [[ ! -e "$name""$old" ]]
then
printf "%s\n" "##################################################" ".bash_history will be cleared soon, backing up....!" "##################################################"
cp "$name" "$name""$old"
else
incre=0
if [[ -e "$name""$old" ]]
then
while [[ -e "$name"-"$incre""$old" ]]
do
(( incre++ ))
done
fi
if [[ "$incre" -ge 20 ]]
then
printf "%s\n" "********************************************************" "You need to arhive your history files they are mounting up!!!" "**************************************************************"
fi
# collect both times in seconds-since-the-epoch
twelve_days_ago=$(date -d 'now - 12 days' +%s)
file_time=$(date -r "$name" +%s)
new_name="$name"-"$incre""$old"
increMinusOne=$(( incre - 1 ))
#answer=""
#printf "%s\n" "Do you want to backup 'bash_history' now?"
#read -r answer
#if [[ "$answer" == "y" ]] || [[ "$answer" == "Y" ]]
#minus=$(( i - 1 ))
if [ -e "$name-$increMinusOne$old" ]
then
if [ $name -ot "$name-$increMinusOne$old" ]
then
printf "%s\n" "##################################################" ".bash_history will be cleared soon, backing up....!" "##################################################"
cp "$name" "$name-$increMinusOne$old"
fi
elif [ -e "new_name" ]
then
new_time=$(date -r "$new_name" +%s)
if [ "$name" -nt "$new_name" ] && (( $(wc -l < "$name") > $(wc -l < "$new_name") ))
then
printf "%s\n" "##################################################" ".bash_history will be cleared soon, backing up....!" "##################################################"
cp "$name" "$new_name"
# ...and then just use integer math:
elif (( new_time <= twelve_days_ago ))
then
echo "$new_name is older than 12 days"
printf "%s\n" "##################################################" ".bash_history will be cleared soon, backing up....!" "##################################################"
cp "$name" "$new_name"
fi
else
printf "%s\n" "##################################################" ".bash_history will be cleared soon, backing up....!" "##################################################"
cp "$name" "$new_name"
fi
fi
fi