#!/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의 출력에서 대문자 T로 시작하여 "다음..."을 작성합니다. 에서 본 또 다른 철자 실수 grep linx-image
. apt list --upgradable
귀하의 시스템에서 해당 기능이 사용 가능한지 확인해 보시기 바랍니다 . 나는 대화형 명령이 아니기 때문에 출력을 건드리지 않는다고 가정합니다.