第一次编写脚本——为什么输出不写入文件?

EHJ*_*EHJ 0 updates command-line apt scripts log

我学会了使用带有 DOS 的计算机,因此我尝试近似批处理/.bat 文件来运行系统的所有更新并创建结果的日志文件。

#!/bin/bash
#update system in one step
cd ~
date > aupdate.log
echo Launching updates...
echo Beginning apt update
sudo apt update >> aupdate.log
sudo apt upgrade -y >> aupdate.log
echo Completing apt autoremove process
sudo apt autoremove -y >> aupdate.log
echo Beginning snap update
sudo snap refresh >> aupdate.log
echo Beginning flatpak update
flatpak update -y >> aupdate.log
echo Updates complete. Review aupdate.log for details.
Run Code Online (Sandbox Code Playgroud)

我走在正确的轨道上吗?到目前为止,我有两个问题需要解决。

  1. 没有创建日志文件
  2. 运行 apt > filename 时如何抑制有关重定向输出的警告(编辑:见下文)

编辑:这是我运行脚本时的结果:

Launching updates...
Beginning apt updates

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.


WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Completing apt autoremove process

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Beginning snap update
All snaps up to date.
Beginning flatpak update
Updates complete. Review aupdate.log for details.
Run Code Online (Sandbox Code Playgroud)

编辑:根据评论的一些早期反馈,我更改了脚本

#!/bin/bash
#update system in one step. Vers 1.1
date > ~/aupdate.log
echo Beginning update...
echo Beginning apt-get updates
apt-get update >> ~/aupdate.log
apt-get upgrade -y >> ~/aupdate.log
echo Completing apt autoremove process
apt-get autoremove -y >> ~/aupdate.log
echo Beginning snap update
snap refresh >> ~/aupdate.log 2>/dev/null
echo Beginning flatpak update
flatpak update -y >> ~/aupdate.log
echo Updates complete. Review aupdate.log for details.
Run Code Online (Sandbox Code Playgroud)

切换到apt-get解决警告并使用2>/dev/nullsnap行抑制了我刚刚注意到的快照输出。我也sudo从这些行中删除了,因为我以 su 身份运行脚本。但是仍然没有日志文件

编辑:这是我现在以及ls之后运行脚本时的结果。

eddie@mrpbell5:~$ sudo ./aupdate.sh
Beginning update...
Beginning apt-get updates
Completing apt autoremove process
Beginning snap update
Beginning flatpak update
Updates complete. Review aupdate.log for details.
eddie@mrpbell5:~$ ls ~ | grep log
eddie@mrpbell5:~$ ls -a
.           .bash_history  .cache      .config    Downloads  .local    Music     .profile  src                        .surfshark  Videos
..          .bash_logout   .cert       Desktop    .gnome     move      Pictures  Public    .ssh                       Templates   .wget-hsts
aupdate.sh  .bashrc        CiscoSpark  Documents  .gnupg     .mozilla  .pki      snap      .sudo_as_admin_successful  .var        .zoom
Run Code Online (Sandbox Code Playgroud)

use*_*186 7

sudo你成为root

我用 运行了你修改后的脚本sudo ./aupdate.sh

正如我所怀疑的那样,日志文件是在文件夹中创建的/root/。这是因为,当您执行命令(或脚本)时,sudo该命令是由特殊用户执行的root。用户的root“家”位于 的特殊文件夹中/root/

日志应该去哪里?

您可以将日志放在您的主文件夹中/home/ehj/。但是,由于这是系统更新活动的日志,您可能希望将其保留在/var/log/.

示例代码

我在您的脚本中添加了一个“环境变量”来保存有关日志文件的信息。

#!/bin/bash
# Location of log
LOG="/var/log/aupdate.log"
#update system in one step. Vers 1.1
date > "$LOG"
echo Beginning update...
echo Beginning apt-get updates
apt-get update >> "$LOG"
apt-get upgrade -y >> "$LOG"
echo Completing apt autoremove process
apt-get autoremove -y >> "$LOG"
echo Beginning snap update
snap refresh >> "$LOG" 2>/dev/null
echo Beginning flatpak update
flatpak update -y >> "$LOG"
echo Updates complete. Review "$LOG" for details.
Run Code Online (Sandbox Code Playgroud)

如果您愿意,可以在以您的主文件夹开头的行中编辑日志文件的位置LOG=...

其他选项

无人值守升级

unattended-upgrades应用程序通常会保持安全更新为最新。不过,它也可用于保持其他应用程序的更新。请参阅如何为任何存储库启用静默自动更新?了解如何设置。

它还unattended-upgrades保留其所做操作的日志,就像上面脚本创建的日志一样。

别名

另一种方法是使用该alias命令为命令列表创建别名。您必须创建一个名为/home/$USER/.bash_aliases以下内​​容的文件:

# My list of aliases
# Note: Since this is called by .bashrc 
# it seems this file does not need to be executable

alias update="sudo apt update && sudo apt -y upgrade && sudo apt -y --purge autoremove"
Run Code Online (Sandbox Code Playgroud)

这个名为的别名update不会保留更新日志,但会执行类似的操作。

希望这可以帮助