Script to shut down the Linux PC – new version
I have created a Bash script in order to shut down the pc.
You can integrate this script into a menu in the bash. An example you can see here in my blog entry. .
The script
#!/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 "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