This script does a lot of tasks before the pc shuts down.
This script shutdowns a pc. It begins to find the current date then it finds the time when the pc was turned on to calculate the days until end of the year and when I will go to retirement then find out the log name - when the log names equals to sven (my name) then write Sven otherwise the login name
Then find the current date and time for a greeting, dependent on the time. For example, when it is 13 (1pm) in Germany, then the greeting should be "Good day Sven. Now it is 13.00. The pc was turned on at 11.00 (11am)".
Next step is to move all PDF files to the Folder PDF (I don't like so many PDF files in the home directory), the same with the "Download" folder. When you have downloaded a PDF file, and you forget to move it. The following step is to delete the RSS files. When you want to add some RSS files for your RSS-Feed Reader. In some cases, you click on it and instead of the XML file, the RSS will be downloaded.
Next, the Trash folder will be deleted after a inactive question if you want to do it. In this case, you should install the program trash-empty. The script will check whether the program is installed or not.
Next to delete the cache of the Python modules. The last step is to ask the user whether there is a reboot or a shutdown. If you don't enter r or s then the script will break and end it.
#!/bin/bash
clear
#current date
datum=$(date --utc --date "$1" +%x)
#when the pc was turned on
einschaltuhrzeit=$(uptime -s | awk '{print $2}')
#counting the day until finish of the current year
restlichentage=$(((`date +%s`-`date -d "Dec 31 2025" +%s`+1)/86400))
echo "To the end of the year are $restlichentage days"
echo
#counting the day until finish my planned work days
echo "To my last day of work" $(((`date +%s`-`date -d "Jul 01 2045" +%s`+1)/86400)) "days"
echo
b=$(if [ "$LOGNAME" == "sven" ]; then
echo "Sven"
else
# If the login name is not sven, then use the login name with big letters.
echo "${LOGNAME^}"
fi)
# 2. Time with conditions
a=$(date +"%H")
c=$(date +"%T")
# 3. Time-dependent greeting
if [ "$a" -ge 6 ] && [ "$a" -lt 12 ]; then
echo "Good morning" "$b" "Now it is" "$datum $c". The pc was turned on at "$einschaltuhrzeit"
elif [ "$a" -ge 12 ] && [ "$a" -lt 18 ]; then
echo "Good day" "$b" "Now it is" "$datum $c". The pc was turned on at "$einschaltuhrzeit"
elif [ "$a" -ge 18 ] && [ "$a" -lt 20 ]; then
echo "Good afternoon" "$b" "Now it is" "$datum $c". The pc was turned on at "$einschaltuhrzeit"
else
echo "Good night" "$b" "Now it is" "$c"
fi
echo "---------------------------------------------------"
#moving PDF files from the home directory to the folder PDF where all PDF are saved
echo "Moving PDF's file from /home/sven to PDF/"
find . -maxdepth 1 -type f -iname '*.pdf*' -exec mv -t PDF/ {} +
#moving PDF files from the download folder to the folder PDF where all PDF are saved
echo "Moving the PDF files to the folder Downloads in /home/sven/Downloads"
echo "---------------------------------------------------"
shopt -s nullglob
pdfs_dl=( Downloads/*.pdf )
shopt -u nullglob
if [ ${#pdfs_dl[@]} -eq 0 ]; then
echo "There are no PDF files anymore in Downloads"
else
mv "${pdfs_dl[@]}" PDF/
echo "The PDF files were moved to the folder PDF"
fi
#deleting all rss files in the download folder.
echo "--------------------------------------------------"
echo "Checking if there are rss files in the folder Download"
echo "--------------------------------------------------"
shopt -s nullglob
rss_dl=( Downloads/*.rss )
shopt -u nullglob
if [ ${#rss_dl[@]} -eq 0 ]; then
echo "There are no rss files anymore"
else
rm "${rss_dl[@]}"
echo "The downloaded RSS files are deleted"
fi
if [ ! -f *.pdf ]; then
echo "No PDF files in the home directory"
else
mv *.pdf PDF/
echo "PDF files were moved to the folder PDF"
fi
echo
echo "-------------------------------"
echo "Deleting the content in trash"
echo "-------------------------------"
#count the files in .local/share/trash/files
# Sicherer und genauer: globbing (*), wobei versteckte Dateien (mit .) ignoriert werden.
filesread=$(ls -A ~/.local/share/Trash/files/ 2>/dev/null | wc -l)
# Option: find -maxdepth 1 -type f | wc -l
echo "In the folder Trash/files are $filesread files."
echo
read -p "Do you want to delete the content of trash (y/n)? " deletetrash
# 2. Überprüfung der Benutzereingabe (Korrektur der Variablen 'action' zu 'deletetrash')
if [[ "$deletetrash" =~ ^[Yy]$ ]]; then
# 3. Überprüfung, ob trash-cli installiert ist (Korrektur der Syntax)
if ! command -v trash-empty &> /dev/null; then
echo "Error: The program trash-cli is not installed. Please install it."
exit 1
fi
# Leeren des Papierkorbs
echo "Deleting the trash according to the FreeDesktop Standard..."
trash-empty
# 4. Überprüfung des Erfolgs des Löschvorgangs (Robustheit)
if [ $? -eq 0 ]; then
echo "The content in trash was successfully deleted."
else
echo "Error: Trash-empty failed to delete the content."
exit 1
fi
else
echo "Deletion aborted."
fi
echo "--------------------------------------------------"
echo "Deleting the pip cache of the python modules"
echo "--------------------------------------------------"
#deleting the pip cache.
echo "----------------------------------------------------"
echo "Deleting the pip cache"
pip cache purge
sleep 2
echo "--------------------------------------------------"
echo
echo
# if you want to reboot or shutdown the pc?
read -p "Do you want to reboot or shutdown your pc (r=reboot / s=shutdown)? (s will turn off the pc): " action
if [[ "$action" == "s" ]]; then
echo "Initiating immediate shutdown..."
shutdown now
# exit 0 is optional, as shutdown terminates the system anyway.
elif [[ "$action" == "r" ]]; then
echo "Initiating immediate reboot..."
reboot
# exit 0 is optional,
else
# If the input is neither “s” nor 'r'
echo "No shutdown or reboot command entered. Script continues."
# The script would continue or end normally here.
fi
Top comments (0)