分享

linux中的shell脚本编程基本知识

 拿破仑小子 2018-06-14

shell 也是操作系统中的一个软件它包在 linux 内核的外面,为用户和内核之间的交互提供了一个接口。

系统中的命令用 shell 去解释shell 接收系统回应的输出并显示其到屏幕中

bash = GNU Bourne-Again Shell

shell 脚本

脚本是一种解释型语言

用 shell 脚本保存执行动作

用脚本判定命令的执行条件

用脚本来实现动作的批量执行

如何创建新 shell 脚本

vim script.sh 用 vim 编写脚本

#!/bin/bash 脚本使用的解释器,通常用幻数 “#!” 指定

AUTHOR 脚本作者

DATE 脚本创作时间

MAIL 脚本作者联系方式

VERSION 脚本的版本

**脚本编写以.sh结尾

1.自动编辑脚本的说明信息

vim /etc/vimrc ###写进最后面**写入:'map ms:call WESTOS()'s ###按F4可以填充进去,但现在被注释了,最前面的双引号,标注释autocmd BufNewFile *.sh exec ':call WESTOS()' ###所有新建的以.sh结尾的文件都会自动填充func WESTOS () call append(0,'###############################') call append(1,'# Author: xbw'.(' #')) ###表示建立者 call append(2,'# Version: '.(' #')) ###脚本的版本信息 call append(3,'# Mail: '.(' #')) ###建立者邮箱 call append(4,'# Date: '.strftime('%Y-%m-%d').(' #')) ###建立时间 call append(5,'# Description: '.(' #')) ###脚本描述 call append(6,'# '.(' #')) call append(7,'###############################') call append(8,'') call append(9,'#!/bin/bash')endfunc其中call,表示调用、

linux中的shell脚本编程基本知识

效果图

linux中的shell脚本编程基本知识

2.diff比较和patch打补丁

diff 命令是用来比较两个文件或目录的不同

vim westos 写入:hello world westosvim westos1写入:hello world xbwdiff westos westos1 ###比较这两个文件

linux中的shell脚本编程基本知识

diff -u westos westos1 ###-u,以合并的方式来显示文件内容的不同diff -u westos westos1 > westos.path ###把比较结果重定向到 westos.pathls cat westos.path ###查看此文件

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

PATCH:用于文件不同文件打布丁

yum install patch -ypatch westos westos.path ####不会保留原文件lscat westoscat westos1

linux中的shell脚本编程基本知识

patch -b westos westos.path ###-b,加上后会保留原文件内容到westos.origcat westoscat westos1cat westos.orig

linux中的shell脚本编程基本知识

2.比较目录mkdir linuxmkdir unixlstouch linux/filediff -r linux unix

linux中的shell脚本编程基本知识

3.cut 命令多用与字符截取

cut -d指定分隔符cut -f 1,7|1-7 指定截取的列(1,7表示1列和7列、1-7表示1到7列)cut -c 1,4|1-4 指定截取的字符位置cut -d : -f 1 passwd

linux中的shell脚本编程基本知识

cut -d : -f 1-3 passwd

linux中的shell脚本编程基本知识

cut -d : -f 1,3 passwd

linux中的shell脚本编程基本知识

cut -d : -f 3- passwd

linux中的shell脚本编程基本知识

cut -c 1,3 passwd

linux中的shell脚本编程基本知识

1.写一个脚本,执行后显示本机ip,脚本名为ip_show.shvim ip_show.shsh ip_show.sh ###查看结果

以下两种写法都可以实现

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

2.写一个脚本,ping主机,主机开着就显示is up,没有或关着就显示is downvim check_ip.shsh check_ip.sh 172.25.254.111 ###验证结果

linux中的shell脚本编程基本知识

4.排序sort

vim xbwsort -n xbw ##纯数字排序sort -rn xbw ##倒序sort -u xbw ##去掉重复数字sort -o ###输出到指定文件中sort -t ###指定分隔符sort -k ###指定要排序的列sort -t : -k 2 -rn xbw ###将xbw中冒号分割符第2列排倒序

linux中的shell脚本编程基本知识

sort -t : -k 1 -n xbw ###将xbw中冒号分割符第1列排序

linux中的shell脚本编程基本知识

sort -t : -k 1 -n xbw -o sort ###将排序结果放入sort文件中cat sort

linux中的shell脚本编程基本知识

sort -n xbw | uniq -c ###每行显示一次并统计重复次数

linux中的shell脚本编程基本知识

sort -n xbw | uniq -d ###显示重复的行

linux中的shell脚本编程基本知识

sort -n xbw | uniq -u ###显示唯一的行

linux中的shell脚本编程基本知识

1.写一个脚本,找出/mnt里最大的文件vim find_big.shsh find_big.shtest 命令

下图中这两种写法都可以实现

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

test 命令

test 命令和 [] 等同test '$A' == '$B' 等同 [ '$A' == '$B' ][ '$A' = '$B' ] ###等于[ '$A' != '$B' ] ###不等于[ '$A' -eq '$B' ] ###等于[ '$A' -ne '$B' ] ###不等于[ '$A' -le '$B' ] ###小于等于[ '$A' -lt '$B' ] ###小于['$A' -ge '$B' ] ###大于等于['$A' -gt '$B' ] ###大于['$A' -ne '$B' -a '$A' -gt '$B' ] ###既满足不等于也满足大于(与的关系)['$A' -ne '$B' -o '$A' -gt '$B' ] ###满足等于或大于(或的关系)[-z '$A' ] ###检测是否为空[-n '$A' ] ###检测是否不为空&& ###表示结果为真|| ###表示结果为假

linux中的shell脚本编程基本知识

1.写一个脚本,判断主机是否开启,若没有写主机ip,则提示加上主机ipvim check_ip.shsh check_ip.sh ###给提示加上ipsh check_ip.sh 172.25.254.111 ###显示upsh check_ip.sh 172.25.254.333 ###显示down

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

2.写一个脚本,判断一个数在0到10之间,若没写数字,提示给个数字vim num_check.shsh num_check.sh ##提示给数字sh num_check.sh 8 ###显示结果正确sh num_check.sh 20 ###显示结果错误

linux中的shell脚本编程基本知识

['file1' -ef 'file2' ] ###判断两个文件是否节点一致,是否为硬链接touch fileln /mnt/file /mnt/file1 ###作链接ls -li file*[ '/mnt/file' -ef '/mnt/file1' ] && echo yes || echo no[ '/mnt/file' -ef '/etc/passwd' ] && echo yes || echo no

linux中的shell脚本编程基本知识

['file1' -nt 'file2' ] ###判断file1是否比file2新建立['file1' -ot 'file2' ] ###判断file1是否比file2迟建立[ '/mnt/file' -ot '/mnt/file2' ] && echo yes || echo no

linux中的shell脚本编程基本知识

[-e 'file' ] ###测试文件是否存在[-f 'file' ] ###测试文件是否为普通文件[-L 'file' ] ###测试文件是否为软链接 [-S 'file' ] ###测试文件是否为套接字[-b 'file' ] ###测试文件是否为块设备[-d 'file' ] ###测试文件是否为目录[-c 'file' ] ###测试文件是否为字符设备1.vim file.sh ####为做实验先写一个脚本进行验证写入:#!/bin/bash[ '$1' '/mnt/file' ] && echo yes || echo notouch filesh file.sh -esh file.sh -f

linux中的shell脚本编程基本知识

ln -s /mnt/file llsh file.sh -L

linux中的shell脚本编程基本知识

rm -fr fileyum install mariadb-server.x86_64 -ylssystemctl start mariadbrsync -D /var/lib/mysql/mysql.sock /mnt/filellsh file.sh -S

linux中的shell脚本编程基本知识

rm -fr filersync -D /dev/vdb /mnt/filellsh file.sh -b

linux中的shell脚本编程基本知识

rsync -D /dev/pts/1 /mnt/filellsh file.sh -c

linux中的shell脚本编程基本知识

rm -fr filemkdir filesh file.sh -d

linux中的shell脚本编程基本知识

1.写一个脚本,查看文件是否存在,存在的话属于什么文件类型vim file_check.shsh file_check.sh 文件名 ####验证

linux中的shell脚本编程基本知识

验证普通文件和块设备

linux中的shell脚本编程基本知识

验证套接字和目录

linux中的shell脚本编程基本知识

验证软链接

linux中的shell脚本编程基本知识

tr大小写转换

vim hello.sh写入:#!/bin/bash[ '$1' = 'hello' ] &&{ echo yes}||{ echo no}sh test.sh hello ####小写可以识别sh test.sh HELLO ###大写就不会识别,会报错

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

tr 'a-z' 'A-Z' < hello.sh="" tr="" 'a-z'="" 'a-z'=""><>

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

写入:#!/bin/bashWORD=$(echo $1 | tr 'A-Z' 'a-z')[ '$WORD' = 'hello' ] &&{ echo yes}||{ echo no}此脚本就会自动将大写转换为可识别的小写

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

1.写一个脚本,判断一个用户是否存在,存在就不管,若没有就建立并设置密码vim user_create.shsh user_create.sh user 123sh user_create.sh xbw 234下图两种写法都可以实现脚本功能

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

linux中的shell脚本编程基本知识

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多