shell编程

Shell编程

shell初步入门

1)什么是shell
	shell是命令解释器,负责翻译我们输入的命令
		交互式:输入命令后,shell负责解析执行我们输入的命令,并把结果输出在屏幕上的过程
        非交互式:读取存储在文本中的shell命令,读取到文本结尾,shell结束
2)什么是shell脚本
	把可执行的命令统一放入到一个文本中,称为shell脚本 包含了 判断语句 循环语句 数组等  		
3)脚本语言的种类
	编译型 c c++
	解释型 python ruby
	脚本型 python shell javascript
	其他语言: PHP html go
: 编译型语法是最快的吗 看优化
面试题: Linux中默认的shell解释器是 bash

shell和py的区别
shell处理底层的能力较强 所有的服务都是shell编写 一键优化 一键安装 一键统计(awk)
py主要作用可以写界面 自动化管理平台CMDB 功能需求
4)书写脚本的规范
    a.脚本存放在固定目录  统一管理
    b.脚本使用.sh结尾  让我们能识别是shell脚本 有语法检测
    c.脚本命名  见名知其意
    d.脚本开头使用解释器  #!/bin/bash
    e.脚本内的注释最好不用英文(可以用)
    f.脚本内的成对符号一次性书写完毕  语法写完再写内容
5)写第一个脚本
使用vim编辑test.sh 在屏幕上输出 Hello World!
    [root@shell ~]# cat test.sh 
    #!/bin/bash
    echo "Hello World"
    [root@shell ~]# sh test.sh 
    Hello World

父shell和子shell的区别

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

执行脚本常用的三种方式

1.使用sh或bash运行脚本(开启了一个子shell运行里面的脚本)
    [root@shell ~]# sh test.sh 
    Hello World
    [root@shell ~]# bash test.sh 
    Hello World
2.使用全路径的方式运行脚本(前提是要给脚本文件执行权限)
    [root@shell ~]# /root/test.sh   
    -bash: /root/test.sh: Permission denied    #直接执行没有权限
    [root@shell ~]# chmod +x test.sh   #赋予x权限
    [root@shell ~]# ll
    -rwxr-xr-x  1 root root   31 Oct 15 17:03 test.sh
    [root@shell ~]# /root/test.sh  
    Hello World
3.使用 . 或source在父进程中执行脚本
    [root@shell ~]# . test.sh
    Hello World
    [root@shell ~]# source test.sh
    Hello World
    
#不常用的执行方式
	[root@shell ~]# echo pwd|bash
	/root
	[root@shell ~]# sh < test.sh
	Hello World!
	/bin/bash

查看执行脚本的过程
sh -x 脚本名

Shell常用的基础变量

查看变量

查看所有的变量,包括环境变量和普通变量
	命令:set
只查看全局变量
	命令:env
取消定义的变量	
	命令:unset 变量名
调用变量
	命令:${变量名}
		 $变量名
显示变量值的长度:
	命令:${#变量名}
1.什么是变量?
    使用一个固定的值代表一个不固定的值
    变量的名=变量的值
2.变量的分类
	环境变量(全局变量)  所有shell都生效
	普通变量(局部变量)  只针对当前的shell生效  自定义的变量

3.按生命周期划分变量
    临时生效  只对在当前shell中生效  关闭则失效  使用export直接定义即可
             不加export 只对当前的shell生效
             加export 对当前窗口的父shell和子shell生效
    永久生效  对当前系统所有shell生效  写入/etc/profile
4.环境变量配置文件执行的顺序
    1. /etc/profile
    读取家目录下所有的环境变量文件
    2. .bash_profile 
    3. .bashrc
    4. /etc/bashrc
5.定义环境变量
	环境变量的名称定义:  字母数字下划线开头的组合 见名知其意 等号两端不能有空格  不允许数字开头
        oldboy_age=18    # 全小写
        OLDBOY_AGE=25	 # 全大写 系统环境变量都大写
        oldboy_Age=30    # 小驼峰语法
        Oldboy_Age=35    # 大驼峰语法
	变量值的定义:  数字 字符串 和命令
		字符串定义:变量值中间不允许有空格 如果有需要加引号
6.Shell脚本中特殊位置变量
    $0 ==> 显示执行脚本的名称;如果执行脚本包含了路径,那么就包括脚本的路径
    $n ==> 获取当前执行脚本的第n个参数值,n=1..9, 如果n大于9,则用大括号括起来,例:${10}(参数写在命令行)
    $# ==> 显示当前执行脚本后面接的参数的总个数
    $? ==> 检查上一个命令是否执行成功,(0为成功;非0为失败)
    $$ ==> 获取当前执行脚本的进程号(pid)
    $! ==> 获取上一个在后台运行脚本的进程号
    $_ ==> 获取当前命令行的最后一个参数 类似于esc .
    $* ==> 获取脚本传参的所有参数 在循环体中 不加双引号和$@相同 加双引号 把所有的参数当做一个参数	
    $@ ==> 获取脚本传参的所有参数 在循环体中 不加双引号和$*相同 加双引号 把所有的参数当做独立的

$0 ==> 显示执行脚本的名称;如果执行脚本包含了路径,那么就包括脚本的路径
    [root@shell ~]# cat test.sh 
    #!/bin/bash
    echo "Hello World"
    echo $0
    [root@shell ~]# sh  test.sh 
    Hello World
    test.sh
    
$n ==> 获取当前执行脚本的第n个参数值,n=1..9, 如果n大于9,则用大括号括起来,例:${10}
       参数写在命令行,把命令行中的参数传递到脚本中
    [root@shell ~]# vim test.sh 
    #!/bin/bash
    echo "Hello World"
    echo $1
    [root@shell ~]# sh test.sh 
    Hello World
    [root@shell ~]# sh test.sh  hehe 
    Hello World
    hehe
    
    [root@shell ~]# vim test.sh 
    #!/bin/bash
    echo "Hello World"
    echo $1 $3
    [root@shell ~]# sh test.sh  hehe 123 gg
    Hello World
    hehe gg

$# ==> 显示当前执行脚本后面接的参数的总个数
    [root@shell ~]# cat  test.sh 
    #!/bin/bash
    echo "Hello World"
    echo $1 $3 ${12}
    echo $#
    [root@shell ~]# sh test.sh  {a..z}
    Hello World
    a c l  #显示第1个,第3个和第12个参数
    26     #参数总个数26个
 
$$ ==> 获取当前执行脚本的进程号(pid)
    [root@shell ~]# cat test.sh 
    #!/bin/bash
    echo "Hello World"
    echo 当前执行脚本的进程号是:$$
    [root@shell ~]# sh  test.sh 
    Hello World
    当前执行脚本的进程号是:7821

脚本传参的三种方式

直接传参

[root@shell ~]# cat test.sh
#!/bin/bash
echo name=$1 
echo age=$2
[root@shell ~]# sh test.sh gaoxinyv 25 
name=gaoxinyv
age=25

赋值传参

[root@shell ~]# cat test.sh
#!/bin/bash
name=$1 
age=$2
echo $name $age
[root@shell ~]# sh test.sh gaoxinyv 25 
gaoxinyv 25

read传参

read语法:
	命令:read 参数 变量名 回车
	     变量值 #输入变量的值
               -p  用户提示
               -t  时间限制
[root@shell ~]# read name   #写一个变量名
gaoxinyv                    #给这个变量赋值
[root@shell ~]# echo $name  #调用这个变量
gaoxinyv
[root@shell ~]# read name age  #可同时给两个变量赋值
高新宇  25
[root@shell ~]# echo $name 
高新宇
[root@shell ~]# echo $age 
25
[root@shell ~]# read -p "请输入你的姓名:" name  # -p 给用户一个提示 
请输入你的姓名:gaoxinyv
[root@shell ~]# echo $name
gaoxinyv

[root@shell ~]# cat  test.sh
#!/bin/bash
read -p "请输入你的姓名:" name   #read定义一个变量
echo "你输入的姓名是:$name"      #调用变量
[root@shell ~]# sh  test.sh
请输入你的姓名:高新宇
你输入的姓名是:高新宇

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

变量子串的删除及替换

[root@shell ~]# test='I am gaoxinyv'
[root@shell ~]# echo $test
I am gaoxinyv
[root@shell ~]# echo $test|awk '{print $2}'
am
#变量切片
[root@shell ~]# echo ${test:2:2}  #截取变量中第二个字符后的两个字符(包括空格)
am
这个功能类似于 cut 命令 -c 参数的功能
[root@shell ~]# echo $test|cut -c3-4 #输出变量内容,截取第3-4个位置字符
am

变量子串的删除

#:从前往后匹配
%:从后往前匹配
[root@shell ~]# echo ${url}
www.baidu.com
[root@shell ~]# echo ${url#www.}   #删除变量中的www.
baidu.com
[root@shell ~]# echo ${url#*.}    #删除变量中第一个点前面的所有内容
baidu.com
[root@shell ~]# echo ${url##*.}   #删除变量中最后一个点前面的所有内容    ##:两个#是贪婪匹配
com

[root@shell ~]# echo ${url%.com}  #删除变量中的.com
www.baidu
[root@shell ~]# echo ${url%.*}    #删除变量中最后一个点后的所有内容
www.baidu
[root@shell ~]# echo ${url%%.*}   #删除变量中第一个点后面的所有内容
www 

变量子串的替换

[root@shell ~]# echo ${url}
www.baidu.com
[root@shell ~]# echo ${url}|sed 's#w#W#g'
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

Shell数值运算

expr

expr    只能做整数运算,不支持小数运算
	命令格式: expr 整数1 空格 运算符 空格 整数2
[root@shell ~]# expr 1 + 2
3
[root@shell ~]# expr 1 - 2
-1
[root@shell ~]# expr 1 * 2
expr: syntax error   #语法错误
[root@shell ~]# expr 1 \* 2
2   
[root@shell ~]# expr 1 / 2
0    #不能显示小数
[root@shell ~]# expr 4 / 2
2
#统计字符串长度
[root@shell ~]# expr length gaoxinyv 
8
#相当于echo功能,但只能输出连续的字符串,不能识别变量
[root@shell ~]# expr 12  
12

$[ ]

$[ ]   只支持整数运算,不支持小数运算
	命令格式:$[整数 运算符号 整数] 
[root@shell ~]# echo $[1+2]
3
[root@shell ~]# echo $[1+2.1]
-bash: 1+2.1: syntax error: invalid arithmetic operator (error token is ".1")
[root@shell ~]# echo $[1-2]
-1
[root@shell ~]# echo $[1*2]
2
[root@shell ~]# echo $[1/2]
0  #不能显示小数
[root@shell ~]# echo $[4/2]
2
[root@shell ~]# echo 100*10=$[100*10]
100*10=1000
[root@shell ~]# num1=100
[root@shell ~]# num2=10
[root@shell ~]# echo $num1*$num2
100*10
[root@shell ~]# echo $num1*$num2=$[num1*num2]
100*10=1000

$(( ))

$(( ))  只支持整数运算,不支持小数运算  执行效率最高
	命令格式:$((整数 运算符号 整数))
[root@shell ~]# echo $((1+2))
3
[root@shell ~]# echo $((1+1.5))
-bash: 1+1.5: syntax error: invalid arithmetic operator (error token is ".5")
[root@shell ~]# echo $((1-2))
-1
[root@shell ~]# echo $((1*2))
2
[root@shell ~]# echo $((1/2))
0  #不能显示小数
[root@shell ~]# echo $((4/2))
2

let

let   只支持整数运算,不支持小数运算
	语法格式:let 变量名=整数+整数
	输出结果:echo $变量名
[root@shell ~]# let num=1+2
[root@shell ~]# echo $num
3
[root@shell ~]# let num=1+2.5  #不支持小数
-bash: let: num=1+2.5: syntax error: invalid arithmetic operator (error token is ".5")
[root@shell ~]# let num=1-2
[root@shell ~]# echo $num
-1
[root@shell ~]# let num=1*2
[root@shell ~]# echo $num
2
[root@shell ~]# let num=1/2
[root@shell ~]# echo $num
0

[root@shell ~]# let i++       [root@shell ~]# let i=i+1
[root@shell ~]# echo $i       [root@shell ~]# echo $i
1                             1
[root@shell ~]# let i++       [root@shell ~]# let i=i+1
[root@shell ~]# echo $i       [root@shell ~]# echo $i
2                             2
[root@shell ~]# let i++       [root@shell ~]# let i=i+1
[root@shell ~]# echo $i       [root@shell ~]# echo $i
3                             3

bc

bc   支持整数和小数运算  需要安装 yum -y install bc
	语法格式:echo 数字 运算符号 数字|bc
[root@shell ~]# echo 1+2|bc 
3
[root@shell ~]# echo 1-2|bc 
-1
[root@shell ~]# echo 1*2|bc 
2
[root@shell ~]# echo 1*1.5|bc 
1.5
[root@shell ~]# echo 1/2|bc 
0
[root@shell ~]# echo 4/2|bc 
2
[root@shell ~]# echo 4/1.5|bc 
2

其他运算

awk python    支持整数和小数运算
	awk语法: awk 'BEGIN{print 10*10}'
[root@shell ~]# awk 'BEGIN{print 10*10}'
100
[root@shell ~]# awk 'BEGIN{print 10-10}'
0
[root@shell ~]# awk 'BEGIN{print 10/10}'
1
[root@shell ~]# awk 'BEGIN{print 10+10}'
20
[root@shell ~]# awk 'BEGIN{print 10^10}'
10000000000

[root@shell ~]# echo 10 10|awk '{print $1*$2}' 
100
[root@shell ~]# echo 10 10|awk '{print $1^$2}' 
10000000000

[root@shell ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 10*10
100
>>> 10-10
0
>>> 10-1.5
8.5
>>> 10/10
1

案例

案例1:编写计算器
直接传参的方式
[root@shell ~]# vim  count.sh 
#!/bin/bash
[ $# -ne 2 ] && echo "请输入两个数字!" && exit
echo $1+$2=$(($1+$2))
echo $1-$2=$(($1-$2))
echo $1*$2=$(($1*$2))
echo $1/$2=$(($1/$2))
[root@shell ~]# sh count.sh 10 5
10+5=15
10-5=5
10*5=50
10/5=2
[root@shell ~]# sh count.sh 10 
请输入两个数字!
[root@shell ~]# sh count.sh 10 5 5
请输入两个数字!

赋值传参的方式
[root@shell ~]# cat count.sh 
#!/bin/bash
[ $# -ne 2 ] && echo "请输入两个数字!" && exit
num1=$1
num2=$2
echo $num1+$num2=
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值