shell基础认知

shell 实现的功能

1)手动安装操作系统
2)初始化操作系统
3)安装服务
4)服务启动systemd
5)shell进行代码上线
6)监控zabbix cacti网卡 Nagios监控硬
件交换机路由器
7)日志分析

面试题:你都写过什么脚本,系统初始化都有哪些,系统优化那些内容

必备基础

1.熟练使用vim编辑器
2.熟悉ssh终端
3.熟练使用linux基础命令
4.熟练使用四剑客

那么,如何高效学习shell呢?

shell包含:
	变量基础
	条件表达式
	if判断
	for循环
	while循环
	utile循环
		continue
		break	
		exit
	case语句
	数组
看懂别人的脚本 掌握常见语法 重复练习
要有编程思维
找合适的教材(项目多)	

shell入门

1)什么是Shell
Shell是一个命令解释器,作用解释用户输入的命令以及程序。

2)什么是Shell脚本
把命令统一在放一个文件中进行执行,称为Shell脚本。
Shell脚本包含若干个命令+IF判断+FOR循环+变量+数组等等。

面试题: 编译型语言比解释型或脚本语言速度快吗?
不一定 看解释型语言和脚本语言优化的程度

Shell脚本规范

1.脚本存放固定的目录
mkdir /server/scripts -p
2.开头需要加脚本的解释器 #!/bin/sh 指定解释器
3.附带作者及版本信息 对当前版本的说明
#!/bin/sh
#Author ajie
#Crete time 20200412
#cut log backup
4.脚本扩展名使用.sh结尾
5.注释尽量不使用中文
6.成对的符号和循环格式一次性书写完毕

第一个shell脚本从打印helloword开始

[root@m01 /server/scripts ]# vim test.sh
#!/bin/sh
#Author ajie
#Crete time 20200412
#print hello word
echo "hello,word!"

执行脚本的方法

sh test.sh

chmod +x test.sh
./test.sh
/server/scritps/test.sh

source test.sh 
. test.sh

cat test.sh|bash
bash < test.sh

变量

变量的生存周期

永久的 需要修改环境变量配置文件 变量永久生效 /etc/profile
临时的 直接使用export声明变量即可,关闭shell则变量失效

不加export 则只对当前的shell生效
加export   则对当前打开窗口所有的shell生效

环境变量配置文件生效的顺序

 1) /etc/profile		 
 2) .bash_profile 
 3) .bashrc		     
 4) /etc/bashrc

自定义环境变量

变量名=变量值
获取值 $变量名

[root@m01 ~ ]# name=ajie
[root@m01 ~ ]# echo $name
ajie

变量值的定义

数字变量内容定义
oldboy_age=18

字符串定义
oldboy='I am oldboy teacher' 默认是双引号 混合字符串必须加双引号 数字 字符串 变量

单引号和双引号的区别

单引号: 所见即所得 吃什么吐什么 定义的什么值输出或者调用的就是什么值 不会解析变量

双引号:可以解析变量

命令的定义方式

test=`pwd`
test=$(pwd)

小思考?

[root@m01 ~ ]# date
2020年 04月 12日 星期日 18:02:45 CST
[root@m01 ~ ]# time=`date`
[root@m01 ~ ]# echo $time
2020年 04月 12日 星期日 18:02:55 CST
[root@m01 ~ ]# echo $time

回车之后的结果是什么呢?

shell特殊位置重要变量

配合echo使用

$0  获取当前Shell脚本的文件名  如果脚本全路径执行则显示全路径 basename 只获取脚本名称
$n  获取当前脚本的第n个参数,n为0则是脚本名称,从$1开始代表脚本的第一个参数	$9以后需要加{} ${10}
$#  获取shell脚本所有传参的总个数
$*  获取shell脚本所有传参的参数,如果不加双引号则和$@相同,在循环语句中如果加上双引号,则表示将所有的参数视为单个字符串
$@  获取shell脚本所有传参的参数,如果不加双引号则和$*相同,在循环语句中如果加上双引号,则表示将所有的参数视为独立字符串
$?  获取执行上一条命令的执行状态结果,返回值0为成功 非0失败
$$  获取当前脚本的PID
$!  获取上一个后台工作脚本进行的PID
$_  获取脚本最后的一个参数 类似于ESC .

脚本的传参

直接传参

[root@m01 /server/scripts ]# cat test.sh 
#!/bin/bash
#Author: ajie
#Date: 2020-04-09
#FileName: test.sh
ping -c1 -W1 $1 &>/dev/null
[ $? -eq 0 ] && echo "ping is ok" || echo "ping is error"
[root@m01 /server/scripts ]# sh test.sh baidu.com
ping is ok
[root@m01 /server/scripts ]# sh test.sh baiduuuuuuuuu.com
ping is error

变量赋值

[root@m01 /server/scripts ]# cat test.sh 
#!/bin/bash
#Author: ajie
#Date: 2020-04-09
#FileName: test.sh
url=$1
ping -c1 -W1 $url &>/dev/null
[ $? -eq 0 ] && echo "ping is ok" || echo "ping is error"

read读入

[root@m01 /server/scripts ]# cat test.sh 
#!/bin/bash
#Author: ajie
#Date: 2020-04-09
#FileName: test.sh
read -p "check your url: " url
ping -c1 -W1 $url &>/dev/null
[ $? -eq 0 ] && echo "ping is ok" || echo "ping is error"
[root@m01 /server/scripts ]# sh test.sh 
check your url: lvxinjie.cn
ping is ok
面试题:结果为空
[root@shell ~]# cat 2.sh
#!/bin/sh
user=`whoami`
[root@shell ~]# sh 2.sh
[root@shell ~]# echo $user

变量的子串

子串切片

name="I am shuaiguo"
取出am 三种方式取值

echo $name|awk '{print $2}'
am

echo $name|cut -c 3-4
am

echo ${name:2:2}
am

如何统计变量内的长度

echo $name|wc -L
13

echo ${#name}
13

echo $name|awk '{print length}'
13

expr length "$name"
13

变量的删除和替换

url=www.sina.com.cn

方法1
sed -r 's#www.(.*)#1#'
方法2
echo www.sina.com.cn|grep 's.*$' -o
方法3
echo $url|sed 's#www.##g'
方法4
echo ${url:4}
方法5 切碎组合
echo $url|cut -d "." -f2
方法6 变量删除
[root@shell ~]# echo $url
www.sina.com.cn
[root@shell ~]# echo ${url#*.}
sina.com.cn
[root@shell ~]# echo ${url#*.*.}
com.cn
[root@shell ~]# echo ${url#*.*.*.}
cn

[root@shell ~]# echo ${url}
www.sina.com.cn
[root@shell ~]# echo ${url#*s}
ina.com.cn
[root@shell ~]# echo ${url}   
www.sina.com.cn
[root@shell ~]# echo ${url#*c}
om.cn
[root@shell ~]# echo ${url##*c}
n

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

[root@shell ~]# echo $url
www.sina.com.cn
[root@shell ~]# echo $url|sed 's#sina#baidu#g'
www.baidu.com.cn
[root@shell ~]# echo ${url/sina/baidu}
www.baidu.com.cn
[root@shell ~]# echo ${url}
www.sina.com.cn
[root@shell ~]# echo ${url/w/a}
aww.sina.com.cn
[root@shell ~]# echo ${url//w/a}
aaa.sina.com.cn

小结:
变量从前往后删除 # 贪婪匹配 ##
变量从后往前删除 % 贪婪匹配 %%
变量内容替换 // 贪婪匹配 ///

原文地址:https://www.cnblogs.com/youhongliang/p/12706238.html