2
\$\begingroup\$

How is the logic, efficiency and can it be improved?

This is a snippet from my .bashrc file; I just wrote it and I've tested it and it almost works. This was the first version review.

I feel I haven't fully gotten a grasp of what I've done at the end of the code, could you help me understand the logic and improve it; does it have to create a new file every time this evaluates~/.bash_history) >= 99900 to true, I'm sure no?

As it is now, it creates two files and then stops if I don't use any commands and update ~/.bash_history.

It runs every time when the shell/terminal starts and when there are 100 lines or less left in history (if (( $(wc -l < ~/.bash_history) >= 99900 ))).

The code is supposed to do this:

Backup my .bash_history file if it is 100 lines or less from the defined size of HISTSIZE=100000 and HISTFILESIZE=100000 lines, then backup the file.

  1. Check if .bash_history is 99900 lines or more.
  2. Check if ~/.bash_history.old exists; if it doesn't use that filename.
  3. Increment incre one digit larger than is already used by filenames.(filename-4.old)
  4. Check if there are more than 20 files already backed up and warn if there are.
  5. Set the new filename into a variable.
  6. Check if the last file with a digit in its filename is older than the original.
  7. copy the file to new_name

Here is the UPDATED code:

########################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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.