Help Me Troubleshoot My Bash Script!

0
7
Asked By CuriousCactus42 On

I've been trying to run my bash script, but the terminal starts acting up. Even after changing the permissions, it's still not working right. Could you help me figure out what's going wrong? Here's what my script looks like:

```bash
#!/bin/bash
echo "Updating..."
set -e
LOG_FILE="/var/log/apt/history.log"
sudo apt update >> "$LOG_FILE" 2>&1
sudo apt upgrade -y >> "$LOG_FILE" 2>&1
sudo apt autoremove -y >> "$LOG_FILE" 2>&1
sudo apt clean -y >> "$LOG_FILE" 2>&1
EXIT_STATUS=$?
if [ $EXIT_STATUS -eq 0 ]; then
echo "Done!" >> "$LOG_FILE"
else
echo "An error occurred..." >> "$LOG_FILE"
fi
```

2 Answers

Answered By ScriptSleuth88 On

You might want to check this out with a tool like ShellCheck! It looks like there are a few issues with your script regarding how you’re redirecting the output and using `sudo`. Basically, running `sudo` doesn't affect redirects, so you should consider using `| sudo tee -a "$LOG_FILE"` instead.

Answered By NerdyNinja75 On

Hey, you're writing to the apt history log, which is not a good idea. That log is meant for apt's own processes. You might want to create a new log file for your script instead. Also, have you looked into using `unattended-upgrades`? It could handle updates automatically for you!

EasyBreezy88 -

Totally agree! Unattended-upgrades can definitely simplify things for you.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.