In effort to be more effective and productive with terminals I have started building a bash script. It is for handling the bash history of concurrently running terminals, archiving old commands, and dynamically loading bash history profiles.
Most current development is going to be found at https://launchpad.net/bashory
First part is making the history dynamic and portable. I do this with 2 source files.
~./bashory/bashory
###
# Merging yesterday with today if recent.
# Vars need to be set could be converted to weeks or months or whatever.
bthen=$(date +%F --date="6 hours ago")
bnow=$(date +%F)
blater(){
date +%F
}
###
###
# Set history location based on day, but could esaily be changed to week or month.
bashHistory=~/.bashory/archive/$bnow.bash_history
###
###
# need to make sure a file exists right away.
touch $bashHistory
###
### doubles $HISTFILE contents
# The if statement could be removed for always or never load yesterday.
# Better yet changed to a flagable option.
#
# Yesterday is so long ago only load todays history minus 1 which gets loaded with bashory()
# Since yesterday was recent lets make sure a file exists for then
# Now only load yesterdays history
# Then add todays minus 1 which gets loaded with bashory2()
if [ $bnow = $bthen ]; then
cat $bashHistory > $HISTFILE
else
touch ~/.bashory/archive/$bthen.bash_history
cat ~/.bashory/archive/$bthen.bash_history > $HISTFILE
cat $bashHistory >> $HISTFILE
fi
###
. ~/.bashory/bastory.bashory
And ~/.bashory/bastory.bashory
###
# This file is seperated to make sure
# These function are correct during profile switching
###
###
# Ensure verify is turned back on
shopt -s histverify
###
###
# Main action is here.
#
# Reset var just in case it's tomorrow
# Just in case if it is tomorrow make sure the file exisits
# Append new command
# Add that same command to the current history
# Clear loaded histroy
# Reload new history
bashory(){
bashHistory=~/.bashory/archive/$(blater).bash_history
touch $bashHistory
history -a $bashHistory
tail -n1 $bashHistory >> $HISTFILE
history -cr $HISTFILE
}
###
###
# Complete change of bastory
# Rather than load only a history profile it will source another script
# The other script will load the required history and any other needs
# consideration of how the profile unloads is important
# This will always be done by calling bashory() from a sourced ~/.bashory/profiles/profile.bash_history
#
#
bastory() {
if [ -f ~/.bashory/profiles/$1.bastory ]; then
. ~/.bashory/profiles/$1.bastory
else
echo no profile found at
echo "~/.bashory/profiles/$1.bastory"
echo reverting back to bashory
bashory
fi
}
###
###
# Load it on the prompt
export PROMPT_COMMAND="bashory"
###
Then the second part is a basic profile made of upto 3 parts. the .bashory/profiles/sample.bastory
sample="Sample"
sampleHistory=0
sample(){
history -a $bashHistory
tail -n1 $bashHistory >> $HISTFILE
history -cr $HISTFILE
}
bastory(){
history -a $bashHistory
if [ -f ~/.bash/sample.bash_history ]; then
bashHistory=~/.bashory/profiles/sample.bash_history
history -a $bashHistory
tail -n1 $bashHistory >> $HISTFILE
history -cr $HISTFILE
else
bashHistory=~/.bashory/profiles/sample.bash_history
sampleHistory=1
cat << 'EOF' > $bashHistory
mount /cdrom
mount /dvd
umount /dvd
umount /dvd
EOF
cat $bashHistory > $HISTFILE
history -cr $HISTFILE
fi
export PROMPT_COMMAND="sample"
}
bashory() {
. ~/.bashory/profiles/sample.bashory
}
then the ~/.bashory/profiles/sample.bashory
unset sample
unset $sample
if [ "$sampleHistory" = 1 ]; then
echo "sampled without sample.bash_history so deleting it now"
rm $bashHistory
else
echo "sampled with sample.bash_history"
fi
echo reverting to normal
. ~/.bashory/bastory.bashory
And optionally the ~/.bashory/profiles/sample.bash_history
# As Example of basic common task (ie !1, !2, !3, !4)
mount /cdrom
umount /cdrom
mount /dvd
umount /dvd