SHELL

1. Shell简介

    Shell的作用

1. 字符串定义方式 name="I am oldboy" 加双引号 解析内部的变量
2. 数字的定义方式 age=1834    必须是连续的数字
3. 命令的定义方式 time=`date` 反引号定义命令

2. 学习Shell用到的基础知识

1. ssh连接工具 xshell CRT
2. 熟练vim编辑器
3. 常用基础命令
4. 熟练掌握三剑客 sed awk grep

3. 如何学习Shell

1. 变量 条件表达式 if判断 for循环while循环 case语句 函数 数组 expect
2. 先理解语句
3. 能够读懂别人的脚本或者系统的脚本
4. 试着更改脚本或者完善脚本
5. 试着编写脚本
6. 需要大家有一本教材,或者是比较完善的一份文档
7. 不能拿来主义,吃透了变成自己的脚本

可以解决企业中常用的脚本问题

4. 初识Shell

1. 什么是Shell
shell是命令解释器,解释我们输入的命令,返回结果
交互式的shell shell等待用户输入,输入命令后shell会执行我们的命令,最终返回结果到屏幕,退出或关闭xshell则shell退出
非交互式shell 把能在命令行执行的命令写入到文本里执行,当执行到文本的末尾,shell结束
笔试题:执行下列脚本返回的结果为_____?

[root@shell ~]# cat test.sh
user=`whoami`

2. 什么是shell脚本
把可执行命令放在一个文本里,称为shell脚本(条件表达式 判断语句 数组等等)

c c++ JavaScript php python go ruby
编译型语言 ./configure make make install

5. 环境变量

环境变量(全局环境变量) 普通变量(局部变量)
x=1 y=x+1 x 变量名 = 赋值 =右边的 变量的值
等号的右边一堆内容,使用一个名字来代替称为变量
临时变量 export声明即可
永久变量 /etc/profile
name=oldboy export name 等于 export name=oldboy
环境变量执行的顺序
/etc/profile .bash_profile .bashrc /etc/bashrc
定义环境变量
环境变量的名字
以字母 数字 下划线的组合,建议大家使用字母或下划线开头 等号两边不能有空格 见名知其意

name=oldboy
NAME=oldboy
name_Age 小驼峰语法
Name_Age 大驼峰语法
_name 下划线开头

变量内容的定义
1. 字符串定义方式 name="I am oldboy" 加双引号 解析内部的变量
2. 数字的定义方式 age=1834 必须是连续的数字
3. 命令的定义方式 time=`date` 反引号定义命令
time=$(date) $() 定义命令
time='date' 所见即所得

6. 创建shell脚本规范

脚本执行方式

[root@shell day1]# sh test.sh
test
[root@shell day1]# /server/scripts/test.sh
-bash: /server/scripts/test.sh: No such file or directory
[root@shell day1]# /server/scripts/day1/test.sh
-bash: /server/scripts/day1/test.sh: Permission denied
[root@shell day1]# . /server/scripts/day1/test.sh
test
[root@shell day1]# source /server/scripts/day1/test.sh
test
[root@shell day1]# chmod +x /server/scripts/day1/test.sh
[root@shell day1]# /server/scripts/day1/test.sh
test

7. 环境变量 特殊位置变量

1. $0 脚本的名称,如果全路径执行,则名称带全路径,basename获取脚本名称 Usage: test.sh ***** {start|stop|status|restart|reload|force-reload}
2. $n 代表的脚本的第n个参数 $0被脚本名占用 从$1开始 $10后需要加{} *****
3. $# 代表了脚本传参的总个数 [ $# -ne 2 ] && echo "请输入两个参数" && exit 1 *****
4. $* 代表传参的参数 如果不加双引号和$@相同 加了双引号 则所有的参数视为单个字符串 nameoldboyzsls 在脚本中无区别 for循环
5. $@ 代表传参的参数 如果不加双引号和$*相同 加了双引号 则所有的参数视为独立的参数 name oldboy zs ls 在脚本五区
6. $? 上一条命令返回的结果,0为成功 非0失败 *****
7. $$ 获取脚本的PID echo $$ > /tmp/cutlog.pid ***
8. $! 上一个在后台运行脚本的PID
9. $_ 获取最后一个东东 ESC .

 

[root@shell day1]# for i in "$*";do echo $i;done
I am lizhenya teacher
[root@shell day1]# for i in "$@";do echo $i;done
I am
lizhenya
teacher

8. 变量的传参方式

#!/bin/sh
#第一种传参方式
echo "name=$1 age=$2"
#第二种传参方式 赋值传参
name=$1
age=$2
echo $name $age
#第三种传参方式 read -p
read -p "请输入你的姓名: " name
read -p "请输入你的年龄: " age

echo $name $age
read
[root@shell day1]# read name
oldboy
[root@shell day1]# echo $name
oldboy
[root@shell day1]# read name age
oldboy 18
[root@shell day1]# echo $name
oldboy
[root@shell day1]# echo $age
18

9. 变量的子串切片

[root@shell day1]# echo $name
I am lizhenya teacher
[root@shell day1]# echo ${name:0:1}
I
[root@shell day1]# echo ${name:2:4}
am l
[root@shell day1]# echo ${name:2:2}
am
[root@shell day1]# echo ${name:2:3}
am
[root@shell day1]# echo oldboy|wc -L
6
[root@shell day1]# echo ${name}
oldboy
[root@shell day1]# echo ${#name}
6
expr length "${name}"
echo ${name}|awk '{print length}'

面试题: 统计小于3的字符串 I am lizhenya teacher I am 18

#!/bin/sh
for i in I am lizhenya teacher I am 18
do
[ ${#i} -lt 3 ] && echo $i
done
echo "I am lizhenya teacher I am 18"|xargs -n1|awk '{if(length < 3)print}'
echo "I am lizhenya teacher I am 18"|awk '{for(i=1;i<=NF;i++)if(length($i)<3)print $i}'

变量子串的删除和替换

[root@shell day1]# url=www.sina.com.cn
[root@shell day1]# echo $url
www.sina.com.cn
[root@shell day1]# echo $url|awk -F. '{print $2,$3,$4}'
sina com cn
[root@shell day1]# echo $url|awk -F. '{print $2"."$3"."$4}'
sina.com.cn
[root@shell day1]# echo ${url#*.}
sina.com.cn
[root@shell day1]# echo ${url#*.*.}
com.cn
[root@shell day1]# echo ${url#*.*.*.}
cn
[root@shell day1]# echo ${url##*.}
cn
[root@shell day1]# echo ${url}
www.sina.com.cn
[root@shell day1]# echo ${url%.*}
www.sina.com
[root@shell day1]# echo ${url%%.*}
www

[root@shell day1]# echo ${url}
www.sina.com.cn
[root@shell day1]# echo ${url/w/a}
aww.sina.com.cn
[root@shell day1]# echo ${url//w/a}
aaa.sina.com.cn
[root@shell day1]# echo ${url//w/A}
AAA.sina.com.cn

作业: 三剑客 安装CRT 配置默认网关 三种方式 VIM 今天讲的练习
小结:

1.shell的用途
2.初识Shell 父shell 子shell
3.环境变量 全局环境变量 局部环境变量
4.定义环境变量 名称 变量内容定义
5.shell脚本的规范
6.特殊位置环境变量
7.三种传参方式
8.变量子串

 

posted @ 2022-03-23 14:19  风之帆  阅读(144)  评论(0)    收藏  举报