我正在编辑.bashrc文件以备份历史记录,但无法按预期运行

时间:2019-06-05 09:36:51

标签: linux bash shell sh

我正在尝试为我的.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=100000HISTFILESIZE=100000行的大小不超过1000行,请备份该文件。

  1. 检查.bash_history是否为99000行或更多。
  2. 检查~/.bash_history.old是否存在,是否不使用该文件名。
  3. 增量i比文件名已使用的数字大一位。(文件名-4.old)
  4. 检查是否已经备份了20个以上的文件,并发出警告。
  5. 将新文件名设置为变量。
  6. 检查文件名中最后一个包含数字的文件是否早于原始文件。
  7. cpnew_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 )) .

我正在学习,但到目前为止,我是一个非常cr脚的程序员。这是我想出的,请给我反馈和建议:

########################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

0 个答案:

没有答案