Shell-day01

Shell课程大纲


1. Shell基本概述

2. Shell的变量定义

3. Shell数值运算

4. Shell的流程控制  if语句

5. Shell的循环语句 for  while循环

6. Shell的数组函数

7. Shell内置命令 

8. Shell正则表达式  Awk Grep Sed

什么是Shell


Shell就是一个命令解释器。

Shell分为交互式Shell和非交互式Shell。

交互式Shell就是命令行上面一条一条命令的执行

非交互式Shell就是以脚本的方式运行。

通过变量$-来查看是否是交互式或者非交互式Shell

交互式和非交互式的区别

[root@shell01 ~]# echo  $-                #命令行上面的执行
himBH
[root@shell01 ~]# echo 'echo  $-' > test.sh 
[root@shell01 ~]# cat test.sh
echo  $-
[root@shell01 ~]# sh  test.sh            #脚本中的执行
hB

h        # hashall    当Shell运行的时候,会记录保存执行过的命令  hash缓存表

i        # interactive  这是交互式shell的意思

m        # monitor        监控后台的进程的状态,监控模式

B        # 大括号扩展   支持  {}  整体,序列

H        # history        历史命令的记录

什么是Shell脚本


1. 系统命令的堆积  

2. 特定的格式。特定的语法  组成的一个文件 

3. 以.sh为结尾的。

为什么要学习Shell脚本


自动化的运维

减少不必要的工作

提高工作的效率

学习Shell脚本,需要什么技能


1. 要对Vim文本编辑器熟悉。  .vimrc要会运用

2. 要有Linux命令的基础应用。  最少要熟悉80个以上的常用linux命令

3. 对正则表达式要熟悉,对Awk  Sed  Grep 三剑客要熟练使用。

4. 熟悉常见的linux服务,服务配置,网络,优化,日志的等的操作。

如何学好Shell脚本呢


多练  多思考  

要自己的思路,开发思维

变量名要规范 

不要拿来主义,特别是针对新手

Shell脚本能干什么


1. 基础配置 

2. 安装软件

3. 配置变更  

4. 业务部署 Git  jenkins  Shell  进行自动化代码上线  

5. 日常的备份   通过定时任务+脚本

6. 信息收集     Zabbix + Shell   自动化的监控系统的状态

7. 日志分析        ELK

8. 服务扩容/缩容  Zabbix  +  Shell

Shell脚本规范


1. 脚本统一放在一个目录下  

[root@shell01 ~]# mkdir  -p  /scripts

2. 建议使用Vim文本编辑器,有高亮显示 

3. 以.sh为结尾

4. 脚本的开头要写#!/bin/bash    指定这个脚本使用什么命令解释器解释脚本中的命令,如果不写,默认是/bin/bash  

#!  表示的是幻数   这个幻数必须是在脚本中第一行,在其他行就是个注释信息  

5. #号开头的都是注释信息,  写脚本最好加上注释信息  使用英文,不要使用中文,切记不要使用拼音  

6. 脚本带上作者相关信息

7. 成对的符号,要一次书写完成  '' ""      

8. 格式的语法也要一次书写完成   ifthen   fi   for  i  in  do  done

Shell的执行方式


脚本从上到下 从左到到右的方式进行执行,脚本执行时遇到子脚本,会执行子脚本,子脚本执行完成后,退出子脚本,继续执行脚本中的内容。

脚本运行时,会向系统内核请求一个进程。脚本就会在该进程下进行运行,终止操作。


执行通过哪些方式进行执行:

1. bash  script-name  或者  sh  script-name  

#不需要执行权限

[root@shell01 /scripts]# echo "pwd"  >test.sh
[root@shell01 /scripts]# cat test.sh 
pwd
[root@shell01 /scripts]# bash  test.sh
/scripts
[root@shell01 /scripts]# sh  test.sh
/scripts
[root@shell01 /scripts]# ll
total 4
-rw-r--r-- 1 root root 4 2020-02-19 11:10 test.sh


2. path/script-name(绝对路径)  或者  ./script-name   

#必须要有执行权限

[root@shell01 /scripts]# ll
total 4
-rw-r--r-- 1 root root 4 2020-02-19 11:10 test.sh
[root@shell01 /scripts]# /scripts/test.sh
-bash: /scripts/test.sh: Permission denied
[root@shell01 /scripts]# ./test.sh
-bash: ./test.sh: Permission denied
[root@shell01 /scripts]# chmod u+x test.sh 
[root@shell01 /scripts]# ll
total 4
-rwxr--r-- 1 root root 4 2020-02-19 11:10 test.sh
[root@shell01 /scripts]# /scripts/test.sh 
/scripts
[root@shell01 /scripts]# ./test.sh 
/scripts

3. source  script-name  或者  . script-name  

#无需执行权限,将脚本中的命令调入到当前环境下执行

4. cat script-name  | bash

#不需要执行权限,将脚本中的命令调入到当前环境下执行

5. bash  < script-name   

#不需要执行权限,将脚本中的命令调入到当前环境下执行

登录式Shell和非登录式


登录式   通过用户名和密码的方式进行登录的

非登录式 不是通过用户和密码的方式进行登录的

exit  logout   

exit  可以退出登录式和非登录式Shell

logout 只能退出登录式Shell,不能退出非登录式Shell

脚本其实就是一个非登录式Shell

Shell变量定义

变量的概述


什么是变量?

数据的一种传递方式。  用一个固定的字符串去表示一个不固定的字符串,数值,内容,数据等。

方便后续调用

变量名的规范

建议采用驼峰式方式

Hostname_Ip  

变量名由字母,数字,下划线组成,不能使用空格,短横岗,特殊符号尽量不用

见名知其义

ip

Ip

ip1

Host_Ip

host_ip

Host_ip

Shell变量定义的方式

1. 用户自定义变量  

2. 系统环境变量: 系统定义好的,主要是定义系统的信息的

3. 位置参数变量  变量名是固定的,意思也是固定的,通过脚本进行传递数据 

4. 预定义的变量  脚本中已经定义好的,变量名是固定的,作用也是固定的 $-  $?  $$  $@  $*

变量的实践



1. 用户自定义变量 

[root@shell01 /scripts]# Name=qiudao        #定义变量
[root@shell01 /scripts]# echo $Name            #引用变量
qiudao

[root@shell01 /scripts]# Name=qiudao shell  #变量值中不能出现空格
-bash: shell: command not found
[root@shell01 /scripts]# Name='qiudao shell'    #需要引号引起来
[root@shell01 /scripts]# echo $Name
qiudao shell
[root@shell01 /scripts]# Name=qiudao
[root@shell01 /scripts]# echo $Name
qiudao
[root@shell01 /scripts]# echo $Nameshell    #调用变量时,变量名不能跟其他字符串连在一起

[root@shell01 /scripts]# echo ${Name}shell    #将变量名使用大括号进行括起来
qiudaoshell

[root@shell01 /scripts]# echo $Name_shell

[root@shell01 /scripts]# echo $Name-shell
qiudao-shell
[root@shell01 /scripts]# echo $Name/shell
qiudao/shell


[root@shell01 /scripts]# echo $Name
qiudao
[root@shell01 /scripts]# echo '$Name'
$Name
[root@shell01 /scripts]# echo "$Name"
qiudao

单引号:强引用,里面是什么,就输出什么,吃什么吐什么

双引号:弱引用,吃什么吐什么,会解析变量  $   $()

不加引号:在变量定义值时,出现空格,就不会看做是一个整体的值。吃什么吐什么,解析变量

反引号 ``  优先执行反引号里面的命令 跟  $() 含义一样


[root@shell01 /scripts]# echo $Name money is $10000000000000
qiudao money is 0000000000000
[root@shell01 /scripts]# echo '$Name money is $10000000000000'
$Name money is $10000000000000
[root@shell01 /scripts]# echo "$Name money is $10000000000000"
qiudao money is 0000000000000
[root@shell01 /scripts]# echo "$Name money is '$10000000000000'"
qiudao money is '0000000000000'
[root@shell01 /scripts]# echo "$Name money is \$10000000000000"
qiudao money is $10000000000000
[root@shell01 /scripts]# echo '"$Name" money is $10000000000000'
"$Name" money is $10000000000000


注意:遇到特殊符号时,使用撬棍进行转义,取消其特殊含义。

2. 系统环境变量

[root@shell01 /scripts]# echo $Name
qiudao
[root@shell01 /scripts]# vim test.sh 
[root@shell01 /scripts]# cat test.sh
echo $Name
[root@shell01 /scripts]# sh test.sh

#用户自定义变量,只在当前环境下生效

[root@shell01 /scripts]# export Name=qiudao        #自定义系统环境变量,只是临时生效,当前环境及其子Shell环境下生效,永久生效:将定义的变量写入到环境变量配置文件中,  /etc/profile  /etc/profile.d/*.sh  /etc/bashrc  ~/.bashrc  ~/.bash_profile

[root@shell01 /scripts]# echo $Name
qiudao
[root@shell01 /scripts]# sh  test.sh 
qiudao
[root@shell01 /scripts]# bash
[root@shell01 scripts]# echo $Name
qiudao
[root@shell01 scripts]# exit

[root@shell01 /scripts]# set |grep Name        #显示系统中所有定义的变量
Name=qiudao
        gitcvs.dbTableNamePrefix
[root@shell01 /scripts]# env             #只显示系统环境变量,系统环境变量全部是大写的字母
XDG_SESSION_ID=47
HOSTNAME=shell01
TERM=xterm

[root@shell01 /scripts]# echo $Name
qiudao
[root@shell01 /scripts]# unset Name        #取消变量
[root@shell01 /scripts]# echo $Name

#系统环境变量练习
[root@shell01 /scripts]# cat var.sh
#!/bin/bash
echo "当前系统登录的用户为:$USER"
echo "当前所在的目录位置为:$PWD"
echo "当前系统的主机名为:$HOSTNAME"
echo "当前系统用户的家目录为:$HOME"
echo "当前系统登录的用户的UID为:$UID"
echo "当前系统登录的终端IP为:$SSH_CONNECTION"
[root@shell01 /scripts]# sh var.sh
当前系统登录的用户为:root
当前所在的目录位置为:/scripts
当前系统的主机名为:shell01
当前系统用户的家目录为:/root
当前系统登录的用户的UID为:0
当前系统登录的终端IP为:10.0.0.1 63017 10.0.0.7 22

3. 预定义变量及位置变量

[root@shell01 /scripts]# cat path.sh 
#!/bin/bash
echo "当前shell的脚本名为:$0"
echo "当前shell的第一个位置变量为:$1"
echo "当前shell的第二个位置变量为:$2"
echo "当前shell的第三个位置变量为:$3"
echo "当前shell的所有位置变量为:$*"
echo "当前shell的所有位置变量为:$@"
echo "当前shell的位置变量的个数为:$#"
echo "当前shell脚本的PID号为:$$"
echo "上一条命令的执行结果为:$?"

[root@shell01 /scripts]# sh /scripts/path.sh  nginx  php  mysql
当前shell的脚本名为:/scripts/path.sh
当前shell的第一个位置变量为:nginx
当前shell的第二个位置变量为:php
当前shell的第三个位置变量为:mysql
当前shell的所有位置变量为:nginx php mysql
当前shell的所有位置变量为:nginx php mysql
当前shell的位置变量的个数为:3
当前shell脚本的PID号为:8994
上一条命令的执行结果为:0

$*  $@的区别    了解即可

$*   把参数作为一个整体字符串返回

$@   把参数一个一个进行返回

[root@shell01 /scripts]# cat test.sh
#!/bin/bash
test() {

    echo "未加引号时对比"
    echo  $* 
    echo $@
    echo "加入引号之后对比"
    for i in "$*"
    do
        echo $i
    done

    echo "###################"

    for j in "$@"
    do
        echo $j
    done
}

test nginx  php  mysql  ansible
[root@shell01 /scripts]# sh test.sh
未加引号时对比
nginx php mysql ansible
nginx php mysql ansible
加入引号之后对比
nginx php mysql ansible
###################
nginx
php
mysql
ansible


位置变量: $1  $2 .... $9  ${10}

将命令的执行结果赋值给变量


[root@shell01 /scripts]# Hostname=$(hostname)
[root@shell01 /scripts]# echo $Hostname
shell01
[root@shell01 /scripts]# age=18
[root@shell01 /scripts]# echo $age
18
[root@shell01 /scripts]# echo $(($age+1))
19
[root@shell01 /scripts]# # $(( ))  命令行上面的计算
[root@shell01 /scripts]# 
[root@shell01 /scripts]# 
[root@shell01 /scripts]# Date=$(date +%F)
[root@shell01 /scripts]# echo $Date
2020-02-19
[root@shell01 /scripts]# mkdir  oldboy
[root@shell01 /scripts]# touch oldboy/test{01..10}.txt
[root@shell01 /scripts]# ls oldboy/
test01.txt  test02.txt  test03.txt  test04.txt  test05.txt  test06.txt  test07.txt  test08.txt  test09.txt  test10.txt
[root@shell01 /scripts]# tar czf  test.tar.gz  $(find oldboy/ -type f -name "*.txt")
[root@shell01 /scripts]# ll
total 16
drwxr-xr-x 2 root root 186 2020-02-19 15:56 oldboy
-rw-r--r-- 1 root root 440 2020-02-19 15:12 path.sh
-rw-r--r-- 1 root root 285 2020-02-19 15:25 test.sh
-rw-r--r-- 1 root root 196 2020-02-19 15:57 test.tar.gz
-rw-r--r-- 1 root root 308 2020-02-19 14:52 var.sh
[root@shell01 /scripts]# Data=$(tar czf  test.tar.gz  $(find oldboy/ -type f -name "*.txt"))
[root@shell01 /scripts]# echo $Data

[root@shell01 /scripts]# tar cvzf  test.tar.gz  $(find oldboy/ -type f -name "*.txt")
oldboy/test01.txt
oldboy/test02.txt
oldboy/test03.txt
oldboy/test04.txt
oldboy/test05.txt
oldboy/test06.txt
oldboy/test07.txt
oldboy/test08.txt
oldboy/test09.txt
oldboy/test10.txt
[root@shell01 /scripts]# Data=$(tar cvzf  test.tar.gz  $(find oldboy/ -type f -name "*.txt"))
[root@shell01 /scripts]# echo $Data
oldboy/test01.txt oldboy/test02.txt oldboy/test03.txt oldboy/test04.txt oldboy/test05.txt oldboy/test06.txt oldboy/test07.txt oldboy/test08.txt oldboy/test09.txt oldboy/test10.txt
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:44

results matching ""

    No results matching ""