The first chapter outlines the basic Shell

1. Shell Course Syllabus

01) Shell basic overview

02) Shell variable definitions

03) Shell numerical operations

04) Shell Flow Control

05) Shell loop

06) Shell Array Functions

07) Shell built-in command

08) Shell Regular Expressions

2. What is Shell?

Shell是一个命令解释器,它的作用是解释和执行用户输入的命令及程序等,用户输入一条命令,Shell就解释执行一条。

Shell存在于操作系统的最外层,负责直接与用户对话,把用户输入的命令解释给操作系统,并处理各种各样的操作系统的输出结果,输出到屏幕返回给用户,当我们输入系统用户名和密码,登录到Linux后的所有操作都是由Shell解释并执行的。

Shell command is there is an interactive and non-interactive in two ways.

   什么是交互式:交互式模式就是在终端上执行,Shell等待你的输入,并且立即执行你提交的命令。这种模式被称作交互式,是因为Shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、退出。当你退出后,Shell也终止了。
   那什么又是Shell非交互式:非交互式模式,以Shell Script(非交互)方式执行。在这种模式下,Shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾,Shell也就终止了。

You can print "$ -" to distinguish between interactive and non-interactive Shell value of the variable (representing Shell option currently set).

[root@cc ~]# echo $-
himBH          #交互式Shell   

[root@cc ~]# cat 1.sh
#!/bin/bash
echo $-

[root@cc ~]# sh 1.sh
 hB            #非交互式Shell

The meaning of the character represented by:

01)h:hashall,打开这个选项后,Shell会将命令所在的路径记录下来,避免每次都要查询。
02)i:interactive,包含这个选项说明当前的Shell是一个交互式的Shell。    
03)m:monitor,打开监控模式,就可以通过Job control来控制进程的停止、继续,后台或者前台执行等。
04)B:braceexpand,大括号扩展。
05)H:history,Shell会把我们执行的命令记录下来,可以通过history命令查看。

3. What is Shell Script?

01)系统的命令堆积在一起,按照顺序执行。    
02)特定的格式 + 特定的语法 + 系统的命令 = 文件    
03)以.sh为结尾的文件

4. Why should I learn Shell Programming?

    Shell脚本语言是实现Linux系统管理及自动化运维重要且必备的工具,Linux系统的底层及基础应用软件的核心大都涉及Shell脚本的内容。每一个合格的Linux系统管理员或运维工程师,都需要熟练Shell脚本语言的编写,并能够阅读系统及各类软件附带的Shell脚本内容,只有这样才能提升运维人员的工作效率,适应日益复杂的工作环境,减少不必要的重复性的工作,从而为个人的职场发展奠定较好的基础。

5. What Shell programming knowledge and learning needs?

01)对vim编辑器能熟练使用,熟悉SSH终端及".vimrc"等的配置。
02)要有一定的Linux命令基础,至少掌握80个以上Linux常用命令并能熟练使用。
03)要熟练掌握Linux正则表达式以及三剑客命令(grep、sed、awk)。
04)熟悉常见的Linux网络服务部署、优化、日志分析及排错。

6. How to Learn Shell Programming?

01)阅读、模仿、阅读、模仿
02)核心:多练--多思考--再练--再思考--坚持如此循环即可
03)掌握Shell脚本的各种常见语法
04)形成自己的脚本开发风格
05)从简单做起,简单判断,简单循环
06)多模仿、多离开参考资料练习、多思考
07)学会分析问题,逐渐形成编程思维
08)编程变量名字规范,采用驼峰语法表示
09)不要拿来主义,特别针对新手

7. What Shell script do?

01)基础配置:系统初始化操作、系统更新、内核调整、网络、时区、SSH优化等。
02)安装程序:LNMP、LAMP、MySQL、Nginx、Redis等。
03)配置变更:Nginx Conf、PHP Conf、MySQL Conf、Redis Conf等。
04)业务部署:Shell配合Git、Jenkins实现自动化部署PHP、Java代码,以及代码回滚。
05)日常备份:MySQL全备 + 增量 + binlog + crond + Shell脚本备份等。
06)信息采集:Zabbix + Shell: 对硬件、系统、服务、网络的监控等。
07)日志分析:ELK:取值->排序->去重->统计->分析等。
08)服务扩容/缩容:Zabbix + Shell
    扩容: 监控服务器cpu, 如cpu负载持续80% + 触发动作(脚本)
    脚本: 调用api开通云主机->初始化环境->加入集群->对外提供访问    
    缩容: 监控服务器cpu使用率20%->判断有多少web节点->判断是否超过预设->缩减到对应的预设状态->变更负载的配置

8. Shell script development norms and habits

Shell script development norms and habits is very important, although not required to comply with these specifications, but with good norms and habits, can greatly enhance the development efficiency and reduce maintenance cost of the script. When people collaborate to develop, we have to comply with a norm of each other becomes more important. Even if a person is developed, it is best to take a fixed set of specifications so that the script easier to read, easy to post-maintenance, the most important is to make yourself a shot is to develop a professional norms and habits.

01)脚本放在放在统一的目录
# mkdir -p /scripts/
02)推荐使用vim编辑器编辑脚本(高亮显示)
03)以.sh为扩展名
04)规范的Shell脚本第一行会指出由哪个程序(解释器)来执行脚本中的内容。
#!/bin/bash 或 #!/bin/sh #如不指定Shell,默认以bash执行。
05)开头的"#!"称为幻数,在执行Shell脚本的时候,内核会根据"#!"后的解释器来确定使用哪个程序解释脚本中的内容。注意:这一行必须在每个脚本顶端的第一行,如果不是第一行则为脚本注释行。
06)附带作者及版权信息。
07)脚本注释:在Shell脚本中,跟在"#"后面的内容表示注释。注释部分不会被执行,仅给人看。注释可以自成一行,也可以跟在命令后面,与命令同行。要养成写注释的习惯,方便自己与他人。最好不用中文注释,因为在不同字符集的系统会出现乱码。
08)成对的符号一次书写完成。
09)循环的格式一次书写完成。

Implementation 9. Shell scripts

Shell scripts are top-down, command execution and statements of each line from left to right, that is, over the implementation of a command and then execute a, if you encounter sub-script (script that is nested) Shell script , will be the first implementation of the contents of sub-script, return to the parent after the completion of the script to continue execution of subsequent commands and statements within the parent script.

Normally, when Shell script execution, will start a new process to the system kernel request to execute commands and scripts Shell scripts child in the process.

Shell scripts can usually be executed in the following ways:

1)bash script-name或sh                 #无需执行权限
[root@cc ~]# cat oldboy.sh
#!/bin/bash
pwd
[root@cc ~]# ll oldboy.sh
-rw-r--r-- 1 root root 16 2019-06-16 22:38 oldboy.sh
[root@cc ~]# sh oldboy.sh
/root

2)path/script-name或./script-name    #必须要有执行权限   
[root@cc ~]# /root/oldboy.sh
-bash: /root/oldboy.sh: Permission denied
[root@cc ~]# ./oldboy.sh
-bash: ./oldboy.sh: Permission denied
[root@cc ~]# chmod +x oldboy.sh
[root@cc ~]# /root/oldboy.sh
/root
[root@cc ~]# ./oldboy.sh
/root

3)source script-name或.    #将脚本里的代码调入到当前环境运行,无需执行权限  
[root@cc ~]# cat test.sh
#!bin/bash
ping baidu.com
[root@cc ~]# sh test.sh >/dev/null

#另打开一个会话窗口查看进程   
[root@cc ~]# ps -ef|grep test.sh
root 13351 13263 0 22:42 pts/2 00:00:00 sh test.sh
root 13387 13355 0 22:43 pts/0 00:00:00 grep --color=auto test.sh

#使用source来执行脚本   
[root@cc ~]# source test.sh >/dev/null

#另一个会话窗口查看进程   
[root@cc ~]# ps -ef|grep test.sh
root 13394 13355 0 22:43 pts/0 00:00:00 grep --color=auto test.sh
[root@web01 ~]# ps -ef|grep ping
root 15616 13965 0 09:47 pts/0 00:00:00 ping baidu.com
root 15620 15591 0 09:47 pts/1 00:00:00 grep --color=auto ping

4)cat script-name |bash        #将脚本里的代码调入到当前环境运行,无需执行权限

5)bash < script-name           #将脚本里的代码调入到当前环境运行,无需执行权限

10. Log in Log in Shell and non-Shell

Login shell: that requires a user name, password, log in to enter the Shell.

Non-login shell: Of course you do not need to enter a user name and password to open the Shell, for example: direct command "bash" is to open a new non-login Shell, open a "terminal" (terminal) in Gnome or KDE window procedure is a non-login Shell.

Run the exit command to exit a Shell (Login or Login Shell); logout command execution, Log Shell (can not quit nonlogin Shell).

/ Etc / profile file is the first boot script to run after you log Shell starts, it only runs to log Shell; Shell will not lead to non-login script.

#登录式shell配置文件执行顺序
/etc/profile->/etc/profile.d/*.sh->~/.bash_profile->~/.bashrc->/etc/bashrc

Each call of a script to change the script before calling the revocation order, when Log Shell, we can also perform certain tasks, such as creating automatic backup, clear temporary files. .Bash_logout these tasks on file.

#非登录式shell配置文件执行顺序
~/.bashrc->/etc/bashrc->/etc/profile.d/*.sh

PS: Verify each echo using a line output to add, note should output the first line in the file.

Guess you like

Origin www.cnblogs.com/chenmiao531759321/p/11724019.html