shell脚本编程【1.8】

shell脚本编程【1.8】

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"

在这里插入图片描述

#!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

良辰美景好时光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值