#!/bin/bash
### change from Static IP to DHCP
H=$(date +%H)
if (( 11 <= 10#$H && 10#$H < 18 )); then
echo -e "network:\n version: 2\n renderer: NetworkManager\n ethernets:\n enp0s3:\n dhcp4: yes" > /etc/netplan/01-network-manager-all.yaml
sudo netplan apply
### create the log file
var=$(ip a | grep -A 3 ens33 | grep "inet " | cut -d " " -f 6)
echo DHCP IP= $var > logfile
echo ########################################
### update and upgrade packages without user interaction
sudo apt-get update && apt-get --yes --force-yes upgrade > tmp.txt
### add the lists to the log file
echo "packages that were updated" >> logfile
awk '/^the following/{p=0} /NEW packages will be installed/{p=1} p' tmp.txt >> logfile
echo ########################################
echo "packages being cleaned" >> logfile
awk '/^the following/{p=0} /packages were automatically installed and are no longer required/{p=1} p' tmp.txt >> logfile
echo ########################################
### remove the not required packages
sudo apt autoremove
### chcek if reboot needed or not
cat tmp.txt | grep -A 20 "the following NEW packages will be installed:" |grep "grub-common-*" > tmp2.txt
cat tmp.txt | grep -A 20 "the following NEW packages will be installed:" |grep "grub-pc-*" >> tmp2.txt
cat tmp.txt | grep -A 20 "the following NEW packages will be installed:" |grep "linx-image-*" >> tmp2.txt
cat tmp.txt | grep -A 20 "the following NEW packages will be installed:" |grep "linux-headers-*" >> tmp2.txt
if [ -s tmp2.txt ]
then
echo "there's kernel/grub update happened" >> logfile
echo ########################################
reboot
else
echo "there's no kernel/grub update happened" >> logfile
echo ########################################
fi
### clean the tmp file
rm -f tmp.txt tmp2.txt
### last reboot time
uptime=$(uptime -s)
echo last reboot time= $uptime >> logfile
echo ########################################
fi
### back IP again to Static
echo -e "network:\n version: 2\n renderer: NetworkManager\n ethernets:\n enp0s3:\n addresses:\n - 192.168.100.100/24" > /etc/netplan/01-network-manager-all.yaml
sudo netplan apply
这是我尝试过的代码,但输出不正确,我不确定我的问题在哪里
答案1
你的脚本中有错别字。在 apt 的输出中写下“The following...”,以大写 T 开头。我在 中看到的另一个拼写错误grep linx-image
。我建议您查看一下apt list --upgradable
系统上是否可用。我假设它的输出永远不会被触及,因为它不是一个交互式命令。