Linux Bash: Shutdown script
In one of my previous blog entry I have presented a start script, but this was not my favourite. I have changed into a shutdown script because I think it is more useful. After a reboot or shutdown, you have en empty free home directory from PDF files.
I directly save the PDF files into the home directory $Home.
The new thing is the system will ask you whether you want to reboot or shutdown the system. If you don’t enter r for reboot or s for shutdown, then the script will terminate.
It could use if you only want to move and delete the files.
#!/bin/bash
clear
#current date
datum=$(date --utc --date "$1" +%x)
#when the pc was turned on
einschaltuhrzeit=$(uptime -s | awk '{print $2}')
#message for the user
echo "Today is $datum. The pc was turned on at $einschaltuhrzeit "
#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"
sleep 2
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 "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 ist optional, da shutdown das System sowieso beendet.
elif [[ "$action" == "r" ]]; then
echo "Initiating immediate reboot..."
reboot
# exit 0 ist optional.
else
# Wenn die Eingabe weder 's' noch 'r' ist
echo "No shutdown or reboot command entered. Script continues."
# Das Skript würde hier normal fortgesetzt oder beendet werden.
fi