Shell脚本的基本知识

一.什么是shell

脚本中命令的解释器

二.shell脚本的意义

1.记录命令执行的过程和执行逻辑,以便以后重复执行
2.脚本可以批量处理主机
3.脚本可以定时处理主机

三.如何创建shell脚本

1.脚本的幻数

#!/bin/bash	##幻数

2.vim的自动补齐

(1)vim配置文件

vim /etc/vimrc 	//vim全局配置文件,对所有的用户生效
vim ~/.vimrc 	//vim局部配置文件,对当前用户生效
 set nu ts=2 et ai  //显示行号;ts=2,每个tab是2个空格;eq将空格分开成独立的空格;ai自动tab

(2)vim自动添加脚本首部

vim ~/.vimrc
  1 set nu ts=2 et ai
  2 
  3 autocmd BufNewFile *.sh,*.script call WESTOSTITLE()
  4 func WESTOSTITLE()
  5         call append(0,"###############################################")
  6         call append(1,"# Author:        hk")
  7         call append(2,"# Version:       ")
  8         call append(3,"# Create_Time:   ".strftime("%Y/%m/%d"))
  9         call append(4,"# Mail:          1602286508@qq.com")
 10         call append(5,"# Info:  ")
 11         call append(6,"# ")
 12         call append(7,"################################################")
 13         call append(8,"")
 14         call append(9,"#!/bin/bash")
 15 endfunc
        

这个时候只要是.sh .script结尾的脚本开始编辑,这些信息会自动补齐

四.如何执行shell脚本

1.手动在环境中开启指定解释器

sh script.sh

2.直接在当前环境中运行shell中的指令不开启新的shell

source script.sh	
. script.sh
source /mnt/test.sh

3.开启脚本中指定的shell并使用此shell环境运行脚本中的指令

chmod +x script.sh
/xxx/xxx/script.sh
./script.sh

五.如何对脚本进行调试

sh -x /mnt/westos.sh
[root@westosD100 etc]# sh -x /mnt/test.sh 
+ ping -c1 -w1 172.25.254.100	//脚本中的命令
+ echo 172.25.254.100 is up	//脚本中的命令
172.25.254.100 is up		//脚本的运行结果

+ ##运行指令
不带+ ##命令运行的输出

六.脚本练习

1.ip_show.sh 网卡 显示当前的IP

(1)脚本

vim ip_show.sh
 10 #!/bin/bash
 11 [ -z "$1" ] && {
 12   echo "Error : Please input interface following script!!!"
 13   exit
 14 }
 15 
 16 ifconfig $1 &> /dev/null || {
 17   echo $1 is not find
 18   exit
 19 } && {
 20   ifconfig $1 | awk '/\<inet\>/{print $2}'
 21 }

(2)测试

[root@westosD100 ~]# sh ip_show.sh ens3
172.25.254.100
[root@westosD100 ~]# sh ip_show.sh ens4
ens4 is not find

2.host_messages.sh 显示当前主机的名称,ip登陆当前主机的用户

(1)要求及脚本

hostname: xxxxx
ipaddress: xxxx.xxxx.xxx.xxx
username: root

vim host_messsage.sh
 10 #!/bin/bash
 11 echo "hostname:     `hostname`"
 12 echo "ipaddress:    `ifconfig ens3|awk '/inet\>/{print $2}'`"
 13 echo "username:      $USER"     //13 who am i | cut -d ' ' -f 1  这个也可以

(2)测试

[root@westosD100 mnt]# sh host_messsage.sh 
hostname:     westosD100.westos.org
ipaddress:    172.25.254.100
username:      root

3.clear_log.sh 执行次脚本后可以清空日志

(1)脚本

vim clear_log.sh
 10 #!/bin/bash
 11 [ "$USER" != "root" ] && {
 12   echo "Error: Please run script with root!"
 13   exit
 14 }
 15 
 16 [ ! -e "/var/log/messages" ] && {
 17   echo "Error:not found log file!"
 18   exit
 19 }
 20 
 21 > /var/log/message && {
 22   echo /var/message is cleaned !!
 23 }

(2)测试

[root@westosD100 mnt]# sh clear_log.sh 
/var/message is cleaned !!
[westos@westosD100 mnt]$ sh clear_log.sh 
Error: Please run script with root!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值