Skip to content

Commit 32388fc

Browse files
committed
2017-06-27
1 parent 2f466a1 commit 32388fc

File tree

1 file changed

+97
-8
lines changed

1 file changed

+97
-8
lines changed

Shell.md

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
## 基础
1111

12-
- shell 本质是一个程序,我们这里讲的是 shell 脚本的编写/编程。
12+
- shell 本质是一个命令解析器,我们这里讲的是 shell 脚本的编写/编程。
13+
- shell 的软件有:sh、bash、zsh、csh、tcsh、ksh 等,Linux 发行版一般是 bash,我个人常用 bash 和 zsh
1314
- 查看 bash 版本号:`bash --version`,CentOS 7.3 是 4.2.46
14-
- 文件后缀`sh`
15-
- 开头语言标识。文件开头是:`#!/bin/sh``#!/bin/bash`(优先使用这个)(其实也可以不加,因为 Linux 默认就是 bash,只是为了合理,一般都要加),主要是告诉哪个解析器来执行脚本。如果该语句写在非第一行,那就是当做注释使用。
15+
- shell 脚本文件后缀`sh`
16+
- shell 脚本开头语言标识。文件开头是:`#!/bin/sh``#!/bin/bash`(优先使用这个)(其实也可以不加,因为 Linux 默认就是 bash,只是为了合理,一般都要加),主要是告诉哪个解析器来执行脚本。如果该语句写在非第一行,那就是当做注释使用。
1617
- 其中 /bin/sh 是 bash 的软链接,所以以上两种写法都可以。(证明:`ls -l /bin/sh`)
1718
- 但是业界上,sh 和 bash 其实是有历史区别的。
1819
- sh 即 Bourne shell,POSIX(Portable Operating System Interface)标准的 shell 解释器,它的二进制文件路径通常是 /bin/sh,由 Bell Labs 开发。
@@ -21,28 +22,32 @@
2122
- 换行符(line separator):IntelliJ IDEA 下编写脚本的时候一定要注意后下角的 LF 还是 CRLF,LF 才是 Linux 的要求。
2223
- 执行脚本命令:`bash /opt/test.sh`(推荐),还有:`. /opt/test.sh``source /opt/test.sh`
2324
- 用 source 或是 点开头 的优点是:后面执行的脚本可以读取 source 或 点 执行脚本中的变量内容,但是这个读取也只是基于一个 shell 状态下,如果是另外开的 shell 是没有用的。
24-
- 注释:用 # 号开头
25+
- 注释:用 # 号开头,只有单行注释,没有多行注释的用法。
2526
- 如果脚本是用于 Crontab 定时任务,则定义在 /etc/profile 中的环境变量是无法使用的,需要重新定义。
26-
27+
- 给脚本赋上执行权限:` chmod +x gitnavi.sh`
2728

2829
## 变量
2930

31+
### 变量概念
32+
33+
- **敲黑板,重点**:等于号,左右两边不要有空格,这个跟一般语言的语法很有冲突感。
3034
- 查看系统的环境变量列表:`env``set`
3135
- 用户环境变量设置位置:`vim ~/.bash_profile`
3236
- 全局环境变量:`vim /etc/profile`,或者在目录:/etc/profile.d/ 下自己写一个 sh 文件,然后设置一下变量
33-
- 环境变量要设置为全大写
37+
- 变量命名只能使用字母,下划线和数字,并且不能以数字开头
38+
- 环境变量要建议设置为全大写
3439
- 环境变量需要 export 导出,完整设置,比如下面设置 JDK:
3540

3641
```
3742
JAVA_HOME=/usr/program/jdk1.8.0_121
3843
export JAVA_HOME
3944
```
4045

41-
- 变量的等于号,左右两边不要有空格
4246
- 普通字符串变量:`gitnavi_url="gitnavi.com"`,虽然也可以用单引号或是不使用任何符号,但是不推荐。
4347
- 使用双引号可以在变量中使用其他变量,双引号内容中有双引号,再加个转移斜杠即可,如下:
4448
- `str="Hello, \"${gitnavi_url}\"! \n"`
45-
- 数值型变量、路径型变量:`gitnavi_age=3`
49+
- 数值型变量:`gitnavi_age=3`
50+
- 路径型变量:`gitnavi_patch=/com/gitnavi/youmeek`
4651
- 删除变量:`unset gitnavi_url`
4752
- 使用变量:
4853
- `echo $gitnavi_url`,不推荐这种
@@ -77,6 +82,90 @@ gitnavi_url="gitnavi.com"
7782
echo `expr index "${gitnavi_url}" navi`
7883
```
7984

85+
## 控制流
86+
87+
### 条件语句
88+
89+
- if
90+
91+
``` shell
92+
if [ condition ]
93+
then
94+
command
95+
fi
96+
```
97+
98+
- if - else
99+
100+
``` shell
101+
if [ condition ]
102+
then
103+
command
104+
else
105+
command
106+
fi
107+
```
108+
109+
- if - else if - else if - else
110+
111+
``` shell
112+
if [ condition1 ]
113+
then
114+
command1
115+
elif [ condition2 ]
116+
command2
117+
elif [ condition3 ]
118+
command3
119+
else
120+
commandN
121+
fi
122+
```
123+
124+
### case 语句(switch - case)
125+
126+
- 条件可以是一个变量或是常数
127+
128+
``` shell
129+
case "${gitnavi_url}" in
130+
"${condition1}" )
131+
command1
132+
;;
133+
134+
"helloworld" )
135+
command2
136+
;;
137+
138+
* )
139+
command3 # 这里相当于 case 语句的 default
140+
;;
141+
142+
esac
143+
```
144+
145+
146+
### 循环语句
147+
148+
- for 语句
149+
- 列表是一组值(数字、**字符串**等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。
150+
151+
``` shell
152+
for 变量 in 列表
153+
do
154+
command1
155+
command2
156+
...
157+
commandN
158+
done
159+
```
160+
161+
- while 语句
162+
163+
``` shell
164+
while [ condition ]
165+
do
166+
command
167+
done
168+
```
80169

81170

82171
## 资料

0 commit comments

Comments
 (0)