shell脚本基础

shell脚本
一、运行shell脚本
脚本是一种解释性语言,需要解释器先编译再执行
指定解释器运行 sh /mnt/xx.sh
直接运行 /mnt/xx.sh
vim /mnt/xx.sh
#!/bin/bash
echo hello world
运行方式
在当前的bash下打开脚本中指定的解释器并运行命令
ctrl+z
ps f
fg
二、脚本检测
sh -x /mnt/xx.sh
命令
动作
vim /mnt/xx.sh
#!/bin/bash -x
date;cal
三、脚本注释
创建新的以.sh结尾的脚本文件自动添加注释
vim /etc/vimrc
“map ms:call WESTOS()'s”
autocmd BufNewFile *.sh exec “:call WESTOS()”
function WESTOS()
call append (0,"##################################")
call append (1,"# Auther: ftf #")
call append (2,"# CreateTime: “.strftime(”%Y-%m-%d %H:%M:%S").("#"))
call append (3,"# Version: 1.0 #")
call append (4,"# Mail: ftf@qq.com #")
call append (5,"# Description: #")
call append (6,"# #")
call append (7,"##################################")
call append (8," “)
call append (9,”#!/bin/bash")
endfunction
四、脚本组成
一个脚本由变量 语句和命令组成
一些常用的脚本命令
diff 比较
[root@foundation134 mnt]# diff westos westos.orig
2d1
< 123
diff打补丁
yum install patch
diff -u westos westos.new > westos.patch 比较不同并生成补丁文件westos.patch
patch -b westos westos.patch 将补丁文件写进westos中
westos westos.new westos.orig westos.patch westos.orig为原文件的备份
cut 截取
tail /etc/passwd | cut -d : -f 1 以:为符号截取第一段
tail /etc/passwd | cut -b 1-4 截取第1至4个字节
tail /etc/passwd | cut -b 1,4 截取第1和第4字节
tail /etc/passwd | cut -c 1-4 截取第1至第4字符
tail /etc/passwd | cut -b 1,4 截取第1至第4字符
脚本显示ip
#!/bin/bash
ifconfig KaTeX parse error: Expected 'EOF', got '#' at position 73: … 脚本显示当前登录用户 #̲!/bin/bash c…A" == “$B” echo yes||echo no
[-e “fiel”]是否存在 [-L “file”]是否软链接 [-b “file”]是否块设备 [-c “file”]是否字符设备
[-f “file”]是否文件 [-S “fiel”]是否套接字 [-d “file”]是否目录 [-z “file”]是否存在 [-z “file”]是否不存在

##shell脚本获取本机ip
[kiosk@asimov ~]$ ifconfig
在这里插入图片描述
[kiosk@asimov ~]$ ifconfig virbr0 | grep netmask
在这里插入图片描述

##写shell脚本主要就是通过指定分隔符提取要的数据
[kiosk@asimov ~]$ vim ip_show.sh
在这里插入图片描述
[kiosk@asimov ~]$ sh -x ip_show.sh

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值