I am fairly new to bash (I have about 3 months of experience), and now I've written my first real application (sort of).
It's a function inside of ~/.bash_profile
, but it has an installer. It is called loginfo and it keeps track of when the terminal was used by the user, and allows them to view this data.
It comes with an installer, and a built-in uninstaller.
install.sh
#!/bin/bash
width=$(tput cols)
printSolid(){
printf "%${width}s\n" " " | tr " " "$1"
}
printCenter(){
textsize=${#1}
padding=$((($width+$textsize)/2))
printf "%${padding}s\n" "$1"
}
install_loginfo(){
touch ~/.bash_profile
if grep -q "#loginfo-->" ~/.bash_profile
then
echo "[ER] Already installed 'loginfo'."
else
loginfofile=$(echo "$(dirname $0)/loginfo.txt")
test ! -e $loginfofile && (echo "[ER] Unable to locate '$loginfofile'")
cat $loginfofile >> ~/.bash_profile
test $? -eq 0 && (echo "[OK] Successfully appended '$loginfofile' to '~/.bash_profile'")
test $? -ne 0 && (echo "[ER] Something went wrong appending '$loginfofile' to '~/.bash_profile'")
fi
}
printSolid "#"
printCenter "Installer for 'loginfo'"
printCenter "By 545H4"
printSolid "#"
echo "Would you like to install 'loginfo'? (y/n)"
while true
do
read -n 1 -r -s confirm
case $confirm in
y | Y)
install_loginfo
break
;;
n | N)
echo "'loginfo' was not installed."
break
;;
*) echo "Please enter a valid option (y/n)"
;;
esac
done
echo "You have got to restart your terminal for any change to take place."
exit
loginfo.txt
#loginfo-->
currLogin=$(date '+%d/%m/%Y %H:%M:%S')
echo "$currLogin" >> terminallogins.txt
deltLoginfo(){
while true
do
echo "Remove 'loginfo'? (y/n)"
read -n 1 -r -s confirm
case $confirm in
y | Y)
echo "$(sed '/^#loginfo-->/,/^#<--loginfo/d' ~/.bash_profile)" > ~/.bash_profile
test $? -eq 0 && (echo "[OK] Successfully removed 'loginfo' from '~/.bash_profile'")
test $? -ne 0 && (echo "[ER] Failed to remove 'loginfo' from '~/.bash_profile'")
touch ~/terminallogins.txt
rm ~/terminallogins.txt
test $? -eq 0 && (echo "[OK] Successfully removed '~/terminallogins.txt'")
test $? -ne 0 && (echo "[ER] Failed to remove '~/terminallogins.txt'")
echo "You have got to restart your terminal for any change to take place."
break
;;
n | N)
echo "'loginfo' wasn't removed."
break
;;
*)
echo
;;
esac
done
}
loginfo(){
case $1 in
last) echo "$(tail -2 ~/terminallogins.txt)" | head -n 1;;
curr) echo "$currLogin";;
list) cat ~/terminallogins.txt;;
delt) deltLoginfo;;
*) echo "Usage: loginfo <last/curr/list/delt>";;
esac
}
#<--loginfo
Inside of the repository install.sh and loginfo.txt are in the same folder, I am new to GitHub as well but using two files seemed the most easy to manage.