functions 函数#
以update这个脚本为基础编改
作用
- 减少重复代码
#!/bin/bash
release_file=/etc/os-release
logfile=/var/log/updater.log
errorlog=/var/log/updater_errors.log
check_exit_status(){
if [ $? -ne 0 ]
then
echo "An error occured,please check the $errorlog file."
fi
}
if grep -q "Arch" $release_file
then
sudo pacman -Syu 1>>$logfile 2>>$errorlog
check_exit_status
fi
if grep -q "Ubuntu" $release_file || grep -q "Debian" $release_file
then
sudo apt update 1>>$logfile 2>>$errorlog
check_exit_status
#默认yes
sudo apt dist-upgrade -y 1>>$logfile 2>>$errorlog
check_exit_status
fiCaseStatements#
脚本#
╭─ ~/shellTest ly@vmmin 22:32:52
╰─❯ cat ./13myscript_cls.sh
#!/bin/bash
finished=0
while [ $finished -ne 1 ]
do
echo "What is your favorite Linux distribution?"
echo "1 - Arch"
echo "2 - CentOS"
echo "3 - Debian"
echo "4 - Mint"
echo "5 - Something else.."
echo "6 - exit"
read distro;
case $distro in
1) echo "Arch is xxx";;
2) echo "CentOS is xbxxx";;
3) echo "Debian is bbbxx";;
4) echo "Mint is xxxxsss";;
5) echo "Something els.xxxxx";;
6) finished=1
echo "now will exit"
;;
*) echo "you didn't enter an xxxx choice."
esac
done
echo "Thanks for using this script."脚本执行#
╭─ ~/shellTest ly@vmmin 22:32:11
╰─❯ ./13myscript_cls.sh
What is your favorite Linux distribution?
1 - Arch
2 - CentOS
3 - Debian
4 - Mint
5 - Something else..
6 - exit
3
Debian is bbbxx
What is your favorite Linux distribution?
1 - Arch
2 - CentOS
3 - Debian
4 - Mint
5 - Something else..
6 - exit
u
you didn't enter an xxxx choice.
What is your favorite Linux distribution?
1 - Arch
2 - CentOS
3 - Debian
4 - Mint
5 - Something else..
6 - exit
6
now will exit
Thanks for using this script. ScheduleJobs#
作用#
脚本在特定时间运行































