一、什么是shell编程
1、Shell命令解释器
+ Bash # Redhat
+ dash # Ubuntu
+ csh tcsh # unix
2、 开发语言中程序代码的分类
- 编译型: 开发书写的代码 无法直接运行 需要编译(代码––>命令) C语言
- 解释型: 书写完成代码,通过命令执行 (shell脚本) shell 书写脚本 然后可以直接运行
3、书写与执行脚本习惯
#指定脚本默认使用的命令解释器 第1行(例如)
#!/bin/bash
#!/usr/bin/python
4、 如何执行编写完的脚本
首先:简单编写一个脚本
[root@shell /server/scripts] vim test.sh
#!/bin/bash
echo test
方法1:直接运行,需要加上x权限
[root@shell /server/scripts]# ll test.sh
-rw-r--r-- 1 root root 76 Aug 5 18:22 test.sh
[root@shell /server/scripts]# chmod +x test.sh
[root@shell /server/scripts]# ./test.sh
[root@shell /server/scripts]# /server/scripts/test.sh
方法2:sh运行
[root@shell /server/scripts]# bash test.sh
[root@shell /server/scripts]# ll `which sh bash`
-rwxr-xr-x. 1 root root 964544 Apr 11 2018 /usr/bin/bash
lrwxrwxrwx. 1 root root 4 Apr 29 19:16 /usr/bin/sh -> bash
方法3 source 或 . 替你在当前环境中,执行1次脚本
[root@shell /server/scripts]# . oldboy.sh
test
[root@shell /server/scripts]# source oldboy.sh
test
方法4:不推荐,重定向给sh
[root@shell /server/scripts]# sh < test.sh
test
二、Shell编程基础知识(习惯)
- 书写脚本第1行命令要指定解释器
- 不要使用中文和尽量不要使用拼音(反正我做不到)
- 不要使用中文符号
- 脚本书写记得添加注释信息,其确保其他人能看懂
- 脚本文件名尽量不要包含服务名称,脚本名称进来跟业务有关
三、sh与source (. )区别
书写一个简单的例子
[root@shell /server/scripts]# cat test.sh
#!/bin/bash
echo $TEST
[root@shell /server/scripts]# TEST=666
[root@shell /server/scripts]# sh show.sh
[root@shell /server/scripts]# source test.sh
666
为什么呢:请看图
四、变量
一、什么是变量
- 变量本质:内存中的一块区域,变量名,位置
- 存放经常用到的内容
- Linux下面的变量直接使用即可
- 数据类型(变量在内存中 所占多少空间 变量类型) 不同类型的变量存放不同类型的数据
- 变量赋值(例子)
[root@shell /server/scripts]# test='test 666'
[root@shell /server/scripts]# echo $test
test 666
二、变量分类
- 环境变量(全局变量)
- 普通变量(局部变量)
- 特殊变量
1、环境变量(全局变量 )
- 系统定义
- 大写字母
- 在系统中各个地方基本都是用
环境变量 | 含义 |
---|---|
LANG | 记录系统字符集,语言zh_CN.UTF-8 en_US.UTF-8 |
PS1 | 命令行格式 |
PATH | 命令路径执行命令,系统会在PATH路径中查找 |
UID | 记录用户的UID信息 |
HOSTNAME | 主机名 |
HISTCONTROL | 控制history命令是否记录以空格开头的命令 |
HISTSIZE | history命令记录条数 |
HISTFILESIZE | history文件记录的最多条数 |
TMOUT | 不进行操作,自动断开时间 |
export创建或设置环境变量
[root@shell /server/scripts]# export TEST=666
[root@shell /server/scripts]# cat test.sh
#!/bin/bash
echo $TEST
[root@shell /server/scripts]# sh TEST.sh
666
2、普通变量(局部变量)
命名规则(习惯)
1、不能以数字开头
2、不要占用系统环境变量
3、驼峰:linuxTest=1
4、下划线:age_person = 1
3、特殊变量
1、特殊变量的作用
- 提高书写脚本 及脚本执行效率
- 判断服务状态
2、 表示位置的特殊变量
符号 | 作用 |
---|---|
$0 | 脚本执行错误,给出错误提示 |
$n | 命令行中参数,传递给脚本,在脚本中使用 |
$# | 脚本参数的个数 |
$* | 取出所有的参数 加上双引号:相当于是1个整体1个参数 |
$@ | 取出所有的参数 加上双引号:就是每个都是独立 |
[root@shell /server/scripts]# vim good01.sh
#!/bin/bash
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "argume:$@"
echo "show parm list:$*"
[root@shell /server/scripts]# sh good01.sh 1 2
number:2
scname:good1.sh
first :1
second:2
argume:1 2
show parm list:1 2
[root@shell /server/scripts]# sh -x good01.sh 1 2
+ echo number:2
number:2
+ echo scname:good1.sh
scname:good1.sh
+ echo 'first :1'
first :1
+ echo second:2
second:2
+ echo argume:1 2
argume:1 2
+ echo 'show parm list:1 2'
show parm list:1 2
3、 表示状态变量
符号 | 作用 |
---|---|
$$ | 这个程式的PID(脚本运行的当前进程ID号) |
$! | 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号) |
$? | 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误) |
$_ | 上1个命令的最后1个参数,与 ESC+ . 组合键类似 |
[root@shell /server/scripts]# vim good02.sh
#!/bin/bash
echo "show process id:$$"
echo "show precomm stat: $?"
[root@shell /server/scripts]# sh good02.sh
#!/bin/bash
show process id:1077
show precomm stat: 0
[root@shell /server/scripts]# sh -x good02.sh
#!/bin/bash
+ echo 'show process id:1077'
show process id:1076
+ echo 'show precomm stat: 0'
show precomm stat: 0
4、 变量子串
内容 | 含义 |
---|---|
${parameter} | 返回变量的内容 |
${#parameter} | 返回变量$parameter的长度 |
${parameter:offset} | 在$parameter中, 从位置offset之后开始提取子串 |
${parameter:offset:length} | 在$parameter中, 从位置offset之后开始提取长度为length的子串 |
${parameter#word} | 从变量$parameter的开头,删除最短匹配word的子串 |
${parameter##word } |