shell编程第一天

@TOCshell编程

什么是shell

为什么要使用shell,即shell的作用

  1. 安装操作系统,手工方式安装,自动化安装操作系统,
    kictstart 底层shell脚本
    cobbler 底层shell脚本
  2. 初始化操作系统( SSH优化,关闭SElinux ,防火墙放行需要的端口[80、433、22、修改、YUM源、时间同步、系统最大描述符、内核参数优化、字符集优化、禁止开机自动启动、修改主机名称、])
    3.安装服务 (nginx、PHP、MySQL、rsync针对不同的版本写入shell脚本自动安装 )
  3. 配置服务
  4. 启动服务 所有的服务底层的启动方式都是使用的shell脚本
    6.日志统计 查看程序运行的情况统计我们需要的数据
    日志切割:定时任务+脚本
    统计数据:定时任务+脚本—> 通过邮件发送给管理员
    ELK 日志统计界面 py开发日志界面 py界面----> 数据库 <----数据 日志展示
  5. 监控 监控服务 服务端口是否存在、服务是否存在、服务器的硬件资源使用情况 状态 日志 网络

学习shell编程所用到的知识,必须熟练掌握

a. vim编辑器 课下快捷键
b. xshell crt
c. shell基础命令60个左右 回顾基础命令
d. 三剑客命令 grep sed awk

如何学习shell编程

a. 能读懂shell脚本—> 模仿—> 自己修改 —> 自己写 —> 熟练运用(基础命令) 重复性
b. 有编程能力(写的越多 编程能力越高)
c. 找一本适合自己的教材 或者 完善的文档 (脚本功能框架)
e. 多练多操作
f. 切记拿来直接使用,语句理解后在使用

shell初步入门

什么是shell

shell是命令解释器 负责翻译我们输入的命令 输入的ls pwd命令都是shell解释器给我们运行的
交互式 我们输入命令,shell负责解释执行我们输入的命令,并且把结果输出到屏幕的过程称为交互式
用户签退exit 关闭xshell shell终止
非交互式 不和我们交互,读取存在到文本中的shell命令,读取到文件结尾,shell结束

什么是shell脚本

把可执行的命令统一放入到一个文本中,称为shell脚本 包含了 判断语句 循环语句 数组等

脚本语言的种类

编译型 c c++
解释型 python ruby
脚本型 python shell javascript
其他语言: PHP html go
问: 编译型语法是最快的吗 看优化
面试题: Linux中默认的shell解释器是 bash
shell和py的区别
shell处理底层的能力较强 所有的服务都是shell编写 一键优化 一键安装 一键统计(awk)
py主要作用可以写界面 自动化管理平台CMDB 功能需求

书写脚本的规范

a. 脚本存放固定的目录 统一管理 /server/scripts
b. 脚本使用.sh结尾 让我们能识别是shell脚本
c. 脚本命名 见名知其意 start_nginx.sh stop_nginx.sh
d. 脚本内的开头使用解释器 #!/bin/bash
e. 脚本内的注释最好不用中文(可以用)
f. 脚本内的成对的符号一次性书写完毕 语法书写完在写内容

写第一个脚本

使用vim编辑test.sh 在屏幕上输出 Hellow World!
[root@shell ~]# cat test.sh
#!/bin/sh
echo “Hello World!”

执行脚本常用的三种方式:

父shell和子shell的区别 子shell可以继承父shell的变量 /etc/profile 变量 所有的子shell都可使用
使用bash或sh执行shell脚本 都是在子shell中运行的

  1. 使用sh或者bash方式运行 开启了一个子shell运行里面的内容
    [root@shell ~]# sh test.sh
    Hello World!

  2. 使用全路径方式执行脚本
    [root@shell ~]# /root/test.sh
    -bash: /root/test.sh: Permission denied
    [root@shell ~]# ./test.sh
    -bash: ./test.sh: Permission denied
    增加执行权限chmod
    [root@shell ~]# chmod +x test.sh
    [root@shell ~]# ll
    total 4
    -rwxr-xr-x 1 root root 123 Oct 15 10:49 test.sh
    [root@shell ~]# /root/test.sh
    Hello World!
    /bin/bash
    [root@shell ~]# ./test.sh
    Hello World!
    /bin/bash

  3. 使用. 或者source 在父进程中执行shell脚本
    [root@shell ~]# . test.sh
    Hello World!
    /bin/bash
    [root@shell ~]# echo $name
    oldboy
    不常用的执行方式
    [root@shell ~]# echo pwd|bash
    /root
    [root@shell ~]# sh < test.sh
    Hello World!
    /bin/bash

shell常用的基础变量

什么是变量

系统中的变量: $PATH $LANG $BASH $PS1
x=1 y=x+1 y=2
x和y都是变量的名称 等号后面的是变量的值
name=oldboy
使用一个固定的值代表不固定的值(数字 字符串 命令)

环境变量分类

环境变量 (全局变量) 国法 所有的shell都生效
普通变量 (局部变量) 家规 只针对当前的shell生效 自定义的变量
查看系统环境变量 env

按照生命周期划分

临时生效 只是在当前shell中生效 关闭则失效 使用export直接定义即可
永久生效 对当前系统所有shell生效 写入/etc/profile
不加export 只对当前的shell生效
加export 对当前的窗口的父shell和子shell生效

环境变量 配置文件执行的顺序

  1. /etc/profile
    读取家目录下所有的环境变量文件
  2. .bash_profile
  3. .bashrc
  4. /etc/bashrc

定义环境变量

环境变量的名称定义: 字母数字下划线开头的组合 见名知其意 等号两端不能有空格 不允许数字开头
oldboy_age=18 # 全小写
OLDBOY_AGE=25 # 全大写 系统环境变量都大写
oldboy_Age=30 # 小驼峰语法
Oldboy_Age=35 # 大驼峰语法
变量值的定义: 数字 字符串 和命令
三种值:
字符串定义 变量值中间不允许有空格 如果有需要加引号
[root@shell ~]# test=‘I am lizhenya’
[root@shell ~]# oldboy_age=45
[root@shell ~]# test=pwd # 定义命令
[root@shell ~]# echo KaTeX parse error: Expected 'EOF', got '#' at position 34: … [root@shell ~]#̲ test=(ls) # 定义命令
[root@shell ~]# echo $test
[root@shell ~]# ls
test.sh

PS: 注意赋值反引号还是双引号
[root@shell ~]# date
Thu Oct 15 11:46:55 CST 2020
[root@shell ~]# time=date
[root@shell ~]# echo $time
Thu Oct 15 11:47:00 CST 2020
[root@shell ~]# echo $time
Thu Oct 15 11:47:00 CST 2020

[root@shell ~]# time=“date +%F+%M+%S”

shell脚本中特殊的位置变量

$0 # 表示脚本的名称 如果全路径执行则带全路径 可以使用basename只获取名字

   ------------------
   $0
   [root@shell ~]# cat test.sh
	#!/bin/sh
	#print   hello world
	#Author  oldboy
	#date    20202020
	#version v1.0
	echo "Hello World!"
	echo $0
	[root@shell ~]# sh test.sh 
	Hello World!
	test.sh
    -------------------

$n # 代表了脚本的第n的参数 n为数字 如果是0呢 则为脚本名称 从1开始 从$9以后需要加{} 表示整体
-------------------
$# # 表示脚本传参的个数
使用方法 判断传参的个数

	[root@shell ~]# cat test.sh 
	[ $# -ne 2 ] && echo "请输入两个参数" && exit
	expr $1 + $2
	
	[ $# -ne 2 ] && echo "请输入两个参数" && exit
	echo $1
	echo $2
	-------------------

$? # 判断上一条命令的结果是否正确 0为正确 非0失败

	[root@shell ~]# cat ping.sh 
	ping -c2 -W1  www.baiduaaaaa.com &>/dev/null
	[ $? -ne 0 ] && echo "ping不通" || echo "通了"
	-------------------
	


  $$      # 获取当前脚本的PID
	[root@shell ~]# cat test.sh 
	#!/bin/sh
	#print   hello world
	#Author  oldboy
	#date    20202020
	#version v1.0
	echo $$ > /tmp/nging.pid
	sleep 300
	--------------------

$! # 获取上一个在后台运行脚本的PID号 排错使用

$_ # 获取当前命令行的最后一个参数 类似于esc .

$*		# 获取脚本传参的所有参数 在循环体中 不加双引号和$@相同 加双引号 把所有的参数当做一个参数	
$@      # 获取脚本传参的素有参数 在循环体中 不加双引号和$*相同 加双引号 把所有的参数当做独立的

小结: 常用的位置参数

	      $0  $# $n $?

脚本传参的三种方式

直接传参

[root@shell ~]# cat test.sh 
echo name=$1
echo age=$2
[root@shell ~]# sh test.sh  oldboy 18
name=oldboy
age=18

赋值传参

[root@shell ~]# cat test.sh
#!/bin/sh
name=$1
age=$2
echo -e "$name\n$age"
[root@shell ~]# sh test.sh oldboy 20
oldboy
20

read传参read 变量名称 进行赋值

read -p "用户提示" 变量名称

[root@shell ~]# cat read.sh 
#!/bin/bash
read -p "请输入你的姓名: " name
echo "你输入的姓名是 $name"


[root@shell ~]# cat read.sh 
#!/bin/bash
read -p "请输入你的姓名和年龄: " name age
echo "你输入的姓名是: $name 年龄是: $age"
[root@shell ~]# sh read.sh
请输入你的姓名和年龄: oldboy 20
你输入的姓名是: oldboy 年龄是: 20

[root@shell ~]# cat read.sh
#!/bin/bash
read -p "请输入你的姓名: " name 
read -p "请输入你的年龄: " age
echo "你输入的姓名是: $name 年龄是: $age"
[root@shell ~]# sh read.sh
请输入你的姓名: oldboy
请输入你的年龄: 20
你输入的姓名是: oldboy 年龄是: 20

案例: 使用read传参的方式 修改主机名称为shell 并且修改eth0网卡IP地址为88

[root@shell ~]# cat hostname.sh 
#!/bin/bash
eth0_cfg='/etc/sysconfig/network-scripts/ifcfg-eth0'
old_ip=`ifconfig eth0|awk 'NR==2{print $2}'|awk -F. '{print $NF}'`

read -p "please input hostname: " name
read -p "please input New IP: " IP
hostnamectl set-hostname $name
sed -i "s#$old_ip#$IP#g" $eth0_cfg
grep $IP $eth0_cfg


[root@shell ~]# cat ping.sh 
read -p "Please Input URL: " url
ping -c2 -W1  $url &>/dev/null
[ $? -ne 0 ] && echo "ping不通" || echo "通了"

重复赋值

[root@shell ~]# cat dir.sh
#!/bin/sh
dir1=/etc
dir2=/tmp
read -p "请输入需要备份的目录: " dir2
echo $dir1
echo $dir2
[root@shell ~]# sh dir.sh
请输入需要备份的目录: /opt
/etc
/opt

变量的子串及删除替换

[root@shell ~]# test='I am oldboy'
[root@shell ~]# echo $test
I am oldboy
[root@shell ~]# echo $test|awk '{print $2}'
am
[root@shell ~]# echo $test|cut -c3-4
am

变量切片

[root@shell ~]# echo ${test:2:2}
am

[root@shell ~]# echo ${#test}
11

统计字符的长度

[root@shell ~]# echo $test|wc -L
11

[root@shell ~]# expr length "$test"
11

[root@shell ~]# echo $test|awk '{print length}'
11

统计出字符串小于3的单词 笔试题

I am lzhenya teacher I am 18

[root@shell ~]# cat for.sh
for i in I am lzhenya teacher I am 18
do
		[ ${#i} -lt 3 ] && echo $i
done
[root@shell ~]# sh for.sh
I
am
I
am
18

[root@shell ~]# echo I am lzhenya teacher I am 18|xargs -n1|awk '{if(length<3)print}'
I
am
I
am
18

[root@shell ~]# echo I am lzhenya teacher I am 18|awk '{for(i=1;i<=NF;i++)if(length($i)<3)print $i}'
I
am
I
am
18

子串的删除替换

[root@shell ~]# echo ${url}
www.baidu.com	
[root@shell ~]# echo ${url}|awk -F "w." '{print $3 }'
baidu.com

[root@shell ~]# echo ${url}
www.baidu.com
[root@shell ~]# echo ${url#www.}
baidu.com
[root@shell ~]# echo ${url#*.}
baidu.com
[root@shell ~]# echo ${url#*.*.}
com

贪婪匹配

[root@shell ~]# echo ${url##*.}
com

[root@shell ~]# echo ${url%.com}
www.baidu
[root@shell ~]# echo ${url%.*}
www.baidu
[root@shell ~]# echo ${url%.*.*}
www
[root@shell ~]# echo ${url%%.*}
www

[root@shell ~]# test=%66
[root@shell ~]# echo $test
%66
[root@shell ~]# echo ${test#%}
66

[root@shell ~]# echo ${test##}
#66
[root@shell ~]# echo ${test#\#}
66

变量替换

[root@shell ~]# echo $url
www.baidu.com
[root@shell ~]# echo $url|sed 's#www#WWW#g'
WWW.baidu.com
[root@shell ~]# echo $url
www.baidu.com
[root@shell ~]# echo ${url/w/W}
Www.baidu.com
[root@shell ~]# echo ${url//w/W}
WWW.baidu.com
	
[root@shell ~]# echo ${url/baidu/sina}
www.sina.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值