Shell阶段01 shell脚本概述, 脚本规范, shell执行方式, shell变量

战斗小人 / 2024-04-22 / 原文

Shell

什么是Shell

Shell就是一个命令解释器。

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

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

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

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

交互式和非交互式的区别
[root@shell01 ~]# echo $-                #命令行上面的执行
himBH
[root@shell01 ~]# echo 'echo $-' > test.sh
[root@shell01 ~]# sh test.sh             #脚本中的执行
hB

h        #hashall        当Shell运行的时候,会记录保存执行过的命令        hash缓存表
i        #interactive    这是交互式shell的意思
m        #monitor        监控后台的进程的状态,监控模式(shell中执行命令,命令行上后台进程是看不到的)
B        #大括号扩展        支持 {}  整体,序列
H        #history        历史命令的记录(shell中执行命令是没有记录的)

什么是Shell脚本

1.系统命令的堆积
2.特定的格式。特定的语法 组成的一个文件
3.以.sh为结尾的。

为什么要学习Shell脚本

自动化的运维
减少不必要的工作
提高工作的效率

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

1.要对vim文本编辑器熟悉。        .vimrc (vim的个人环境配置文件)  要会运用
在根目录下,编辑.vimrc
[root@shell01 ~]# vim .vimrc
set tabstop=4
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
    if expand("%:e") == 'sh'
    call setline(1,"#!/bin/bash")
    endif
endfunc

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

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

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

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.格式的语法也要一次书写完成   if ; then fi     for i in do done

Shell的执行方式

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

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

执行通过哪些方式进行执行:
1. bash script-name 或者 sh script-name

#不需要执行权限
[root@shell01 ~]# echo "pwd" > test.sh
[root@shell01 ~]# bash test.sh 
/root
[root@shell01 ~]# sh test.sh 
/root

2. 通过绝对路径或者相对路径执行脚本 
path/script-name(绝对路径) 或者 ./script-name

#必须哟有执行权限
[root@shell01 ~]# /root/test.sh
-bash: /root/test.sh: Permission denied
[root@shell01 ~]# chmod u+x test.sh
[root@shell01 ~]# ll test.sh 
-rwxr--r-- 1 root root 4 Apr 22 23:00 test.sh
[root@shell01 ~]# /root/test.sh 
/root
[root@shell01 ~]# ./test.sh 
/root

3. source script-name 或者 . script-name
#实际是把脚本里命令调到当前环境(命令行)运行

#无需执行权限
[root@shell01 ~]# chmod -x test.sh     # 先去除执行权限
[root@shell01 ~]# ll test.sh 
-rw-r--r-- 1 root root 4 Apr 22 23:00 test.sh
[root@shell01 ~]# source test.sh 
/root
[root@shell01 ~]# . test.sh 
/root
#通过sh执行,能看到后台进程
[root@shell01 ~]# cat test.sh 
ping baidu.com
[root@shell01 ~]# sh test.sh
[root@shell01 ~]# ps aux|grep sh    # 可以查到sh进程   S+表示可中断状态
...
root       7524  0.0  0.1 113276  1192 pts/0    S+   23:22   0:00 sh test.sh
#通过source执行,查不到后台进程,是在当前环境命令行执行的
[root@shell01 ~]# ps aux|grep source
root       7559  0.0  0.0 112808   972 pts/1    R+   23:25   0:00 grep --color=auto source
[root@shell01 ~]# ps aux|grep test
root       7561  0.0  0.0 112808   976 pts/1    R+   23:25   0:00 grep --color=auto test
[root@shell01 ~]# ps aux|grep ping    # 可以查到ping命令
root       7567  0.0  0.2 150072  2000 pts/0    S+   23:27   0:00 ping baidu.com

4. cat script-name | bash
#不需要执行权限,将脚本中的命令调入到当前环境下执行

5. bash < script-name
#不需要执行权限,将脚本中的命令调入到当前环境下执行
[root@shell01 ~]# bash < test.sh

登录式Shell和非登录式

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

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

exit  logout

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

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

脚本其实就是一个非登录时Shell,退出脚本要用exit
#执行bash命令进去,没有输入密码也是非登录式登录,要通过exit退出

Shell变量定义

变量的概述

什么是变量?
数据的一种传递方式。 用一个固定的字符串去表示一个不固定的字符串,数值,内容,数据等。
方便后续调用

变量名的规范
建议采用驼峰式方式
Hostname_Ip
变量名由字母,数字,下划线组成,不能使用空格,短横杠,特殊符号尽量不用
见名知意

Shell变量定义的方式
1.用户自定义变量
2.系统环境变量: 系统定义好的,主要是定义系统的信息的
3.位置参数变量  变量名是固定的,意思也是固定的,通过脚本进行传递数据
4.预定义的变量  脚本中已经定义好的,变量名是固定的,作用也是固定的 $-  $?   $$   $@  $*