Shell basic overview

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.

1. What is interactive:

Interactive mode is executed at the terminal, Shell waiting for your input, and execute the command you have submitted immediately. This mode is called an interactive, because Shell interact with the user. This model is also very familiar with most users: login, execute commands, quit. When you exit, Shell also terminated. 2. What is Shell non-interactive:

Non-interactive mode, perform to Shell Script (non-interactive) mode. In this mode, Shell does not interact with you, but read commands stored in a file and execute them. When it reads the end of the file, Shell will cease. You can print "$ -" to distinguish between interactive and non-interactive Shell value of the variable (representing Shell option currently set).


#命令行输入
[root@gjy ~]# echo $-
himBH            
#交互式Shell (脚本)
[root@gjy ~]# cat 1.sh
\#!/bin/bash
echo $-
[root@gjy ~]# sh 1.sh
hB        

#shell 种类
[root@web01 ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
[root@web01 ~]# echo $SHELL
/bin/bash

Non-interactive Shell # meaning of each character represented by:

01) h: hashall, open this option, Shell will command the path where recorded, to avoid every time the query.

02) i: interactive, include this option to indicate the current Shell is an interactive Shell.

03) m: monitor, turn on monitor mode, you can control the process by Job control stop, continue, such as the background or the foreground.

04) B: braceexpand, brace expansion.

05) H: history, we will execute the Shell command record can be viewed by history command.

3. What is Shell Script?

Command 01) system stacked together, executed in the order.

02) specific format + + syntax specific file system command =

03) to the end of the file as .sh

4. Why should I learn Shell Programming?

Repetitive work

Automated Work

Improve work efficiency

Shell scripting language to achieve Linux system administration and automation of operation and maintenance is important and necessary tool, the underlying core infrastructure and application software for Linux systems mostly involve content Shell script. Each qualified Linux system administrator or operation and maintenance engineers, we need to prepare skilled Shell scripting languages, and the ability to read Shell script and all kinds of content that comes with the system software, the only way to enhance the efficiency of operation and maintenance personnel, to meet the increasingly complex work environment, reduce unnecessary repetitive work, so as to lay a good foundation for the career development of the individual.

5. What Shell programming knowledge and learning needs?

01) of vim editor is fluent, familiar SSH terminal and configuration ".vimrc" and the like.

02) have a certain basic Linux commands to master at least 80 Linux common commands and can skillfully use.

03) To master the Linux Three Musketeers command and regular expressions (grep, sed, awk).

04) be familiar with common Linux network services deployment, optimization, log analysis and troubleshooting.

6. How to Learn Shell Programming?

01) Reading, imitation, reading, imitation

02) Core: more practice - more thinking - to practice - rethinking - and so the cycle can persist

03) to master a variety of common Shell script syntax

04) form their own style of script development

05) start from the simple, simple judgment, a simple cycle

06) more than imitation, leaving more than references to practice, more thinking

07) learn to analyze problems, and gradually form programming thinking

08) Programming variable name specification, using the hump Syntax Notation

09) Do ism, especially for novices

7. What Shell script do?

01) Basic configuration: system initialization, system update, adjusting the kernel, the network, time zone, SSH optimization.

02) Setup: LNMP, LAMP, MySQL, Nginx, Redis and so on.

03) configuration changes: Nginx Conf, PHP Conf, MySQL Conf, Redis Conf so on.

04) service deployment: Shell with Git, Jenkins automate the deployment of PHP, Java code, and the code rollback.

05) daily backups: MySQL full incremental backup + + binlog + crond + Shell script backup.

06) information collection: Zabbix + Shell: monitoring hardware, systems, services, networks and so on.

07) log analysis: ELK: Value -> Sort -> deduplication -> Summary -> analysis.

08) Service expansion / contraction capacity: Zabbix + Shell

Expansion: monitoring server cpu, such as cpu load for 80% + trigger action (script)
script: call api open cloud host -> to initialize the environment -> added to the cluster -> provide external access to volume reduction: 20% cpu usage monitoring server -> determine how many web node -> determine whether exceeds a preset -> reduced to the corresponding preset state -> change loaded configuration

8. Shell script development norms and habits

Shell脚本开发规范及习惯非常重要,虽然这些规范不是必须要遵守的,但有了好的规范和习惯,可以大大提升开发效率,并降低后期对脚本的维护成本。当多人协作开发时,大家有一个互相遵守的规范就显得更重要了。哪怕是一个人开发,最好也采取一套固定的规范,这样脚本更易读、易于后期维护,最主要的是要让自己养成一个一出手就是专业和规范的习惯。

01) put on a uniform script directory
# mkdir -p / scripts /

02) is recommended to use vim editor to edit the script (highlighted)

03) with .sh extension 04) specification Shell scripts the first line will indicate the contents of which are performed by a program (interpreter) script.
#! / bin / bash or #! / bin / sh # if not specified Shell, the default bash to execute.

05) begins with "#!" Called magic number, in the implementation of Shell scripts, the kernel "! #" After the interpreter to determine which program to explain the contents of the script based. Note: This line must be the first line at the top of each script, if the script was not the first line of comment lines.

06) included with the author and copyright information.

07) script Note: Shell script, what follows the "#" indicates a comment. Comment section will not be executed, only the posters. Comments can be on its own line, you can also follow in order behind the command counterparts. To develop the habit of writing comments, to facilitate themselves and others. Chinese better not comment, because the system will be garbled in different character sets.

08) pairs of symbols to complete once written.

09) write once format loop is completed.

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@gjy ~]# cat oldboy.sh
#!/bin/bash
pwd
[root@gjy ~]# ll oldboy.sh
-rw-r--r-- 1 root root 16 2019-06-16 22:38 oldboy.sh
[root@gjy ~]# sh oldboy.sh
root

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

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

#另打开一个会话窗口查看进程    
[root@gjy ~]# 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@gjy ~]# source test.sh >/dev/null
#另一个会话窗口查看进程    
[root@gjy ~]# ps -ef|grep test.sh
root 13394 13355 0 22:43 pts/0 00:00:00 grep --color=auto test.sh
[root@gjy ~]# 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,

例如:直接命令"bash"就是打开一个新的非登录Shell,在Gnome或KDE中打开一个"终端"(terminal)窗口程序也是一个非登录Shell。执行exit命令,退出一个Shell(登录或非登录Shell);执行logout命令,退出登录Shell(不能退出非登录Shell)。
/etc/profile文件是登录Shell启动后运行的第一个启动脚本,它只为登录Shell运行;非登录Shell不会引发这个脚本。

#登录式shell配置文件执行顺序/etc/profile->/etc/profile.d/*.sh->~/.bash_profile->~/.bashrc->/etc/bashrc每个调用的脚本会依次撤销前一个调用脚本中的改变,在退出登录Shell时,我们还可以执行某些任务,如创建自动备份、清除临时文件。把这些任务放在.bash_logout文件中。

#非登录式shell配置文件执行顺序~/.bashrc->/etc/bashrc->/etc/profile.d/*.shPS:验证使用echo在每行添加一个输出即可,注意,要把输出放在文件的第一行

Guess you like

Origin www.cnblogs.com/gongjingyun123--/p/11724041.html