shell脚本编程【1.8】
- 11、shell脚本编程
-
- 11.1.初识脚本
-
- 11.1.0.语法,shell脚本规范,配置.vimrc自动生成注释
- 11.1.1.第一个脚本程序
- 11.1.2.脚本排错处理
- 11.1.3.系统运行参数脚本
- 11.1.4.删库跑路之命令rm的安全实现
- 11.1.5.鸡兔同笼
- 11.1.6.自动监测创建sh文件生成一些配置
- 11.1.7.备份文件bak.sh
- 11.1.8.远程主机上执行脚本命令
- 11.1.9.内存空间和内存地址
- 11.1.10.脚本安全
- 11.1.11.随机颜色
- 11.1.12.算术运算
- 11.1.13.关于()开启子进程 和 {}
- 11.1.14.判断当前系统的版本
- 11.1.15.开心一笑,危险操作:danger
- 11.1.16.网络状态判断
- 11.1.17.磁盘分区利用率思路,磁盘空间的判断
- 11.1.18.读取用户输入姓名,性别,年龄
- 11.1.19.IP地址正确表示
- 11.1.20.判断文件是否存在
- 11.1.21.显示系统信息systeminfo.sh,获取主机系统信息,包括CPU型号,内存大小,硬盘大小,操作系统版本
- 11.1.22.阿里云主机测试
- 11.1.23.获取一天后的日期
- 11.1.24.判断条件分支语法
- 11.1.25.9X9乘法表
- 11.1.26.有若干只兔和鸡,兔和鸡加起来一共有100条腿,请写一个简单的shell算出兔和鸡各多少只可能组合(假设所有的兔和鸡的腿都是健全的,且兔和鸡至少为1只)
- 11.1.27.1+2+3+.....+100
- 11.1.28.编写脚本backup.sh使用tar打包备份/etc 目录到/backup,文件名类似etc_2021-10-01.tar.gz(其中2021-10-01为当前日期)。如何在每次开机后自动执行?
- 11.1.29.
- endl
11、shell脚本编程
11.1.初识脚本
11.1.0.语法,shell脚本规范,配置.vimrc自动生成注释
set nu
set shiftwidth=4
set ts=4
set expandtab
set ignorecase
autocmd BufNewFile *.sh exec "call SetTitle()"
func SetTitle()
if expand("%:e")== 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#")
call setline(3,"#**********************************************************")
call setline(4,"#Author: dange")
call setline(5,"#QQ 123456")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: www.baidu.com")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#*********************************************************")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
# 语法检查
bash -n test.sh
# 调试并运行
bash -x test.sh
set -ue
set -u 限制使用没声明的变量
set -e 遇到错误终止
11.1.1.第一个脚本程序
- 语法检查
bash -n hello.sh
- 命令错误
- 逻辑错误
bash -x hello.sh
#!/bin/bash
#
#********************************************************************************
#Author: dange
#QQ: 2636775731
#Date: 2024-09-01
#FileName: hello.sh
#URL:
#Description: The test script
#Copyright(C): 2024 All right reserved
#********************************************************************************
# 经典写法
echo "hello,world!"
# 流行写法
echo "Hello,world!"
echo -e "cpu型号:\c"
lscpu | sed -nr "s/^Model name: +(.*)/\1/p"
head -n1 /proc/meminfo | tr -s " "
echo -e "硬盘空间:\c"
lsblk /dev/sda | grep "^sda" | tr -s " " | cut -d" " -f4
echo -n "操作系统型号:"
sed -nr 's/^VERSION="(.*)"/\1/p' /etc/os-release
11.1.2.脚本排错处理
11.1.3.系统运行参数脚本
#!/bin/bash
COLOR="\E[1;$[RANDOM%7+31]m"
END="\E[0m"
. /etc/os-release
echo -e "$COLOR---------------------------Host systeminfo------------------$END"
echo -e "主机名: $COLOR`hostname`$END"
echo -e "ipv4地址: $COLOR`ifconfig eth0 | sed -nr '2s/^.*inet +([0-9.]+) +netmask.*/\1/p'`$END"
echo -e "操作系统版本: $COLOR$VERSION$END"
echo -e "内核版本: $COLOR`uname -r`$END"
echo -e "cpu型号: $COLOR`cat /proc/cpuinfo | sed -nr '/^model name.*: +(Intel.*Hz)/s@^model name.*: +(Intel.*Hz)@\1@p' | head -n1`$END"
echo -e "内存大小: $COLOR`cat /proc/meminfo | sed -nr '/^MemTotal.*/s@^MemTotal.* +([0-9]+ kB)@\1@p'`$END"
echo -e "硬盘大小: $COLOR`lsblk | sed -nr '/^sda.*/s#^sda.* +([0-9]+G).*#\1#p'`$END"
#!