-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from yanghaiji/linux_feature
Linux feature
- Loading branch information
Showing
8 changed files
with
1,155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
## echo命令 | ||
|
||
您可以使用echo实现更复杂的输出格式控制。 | ||
|
||
### 1.显示普通字符串: | ||
|
||
``` | ||
echo "It is a test" | ||
``` | ||
|
||
这里的双引号完全可以省略,以下命令与上面实例效果一致: | ||
|
||
``` | ||
echo It is a test | ||
``` | ||
|
||
### 2.显示转义字符 | ||
|
||
``` | ||
echo "\"It is a test\"" | ||
``` | ||
|
||
结果将是: | ||
|
||
``` | ||
"It is a test" | ||
``` | ||
|
||
同样,双引号也可以省略 | ||
|
||
### 3.显示变量 | ||
|
||
read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 | ||
|
||
``` | ||
#!/bin/sh | ||
read name | ||
echo "$name It is a test" | ||
``` | ||
|
||
以上代码保存为 test.sh,name 接收标准输入的变量,结果将是: | ||
|
||
``` | ||
[root@www ~]# sh test.sh | ||
OK #标准输入 | ||
OK It is a test #输出 | ||
``` | ||
|
||
### 4.显示换行 | ||
|
||
``` | ||
echo -e "OK! \n" # -e 开启转义 | ||
echo "It is a test" | ||
``` | ||
|
||
输出结果: | ||
|
||
``` | ||
OK! | ||
It is a test | ||
``` | ||
|
||
### 5.显示不换行 | ||
|
||
``` | ||
#!/bin/sh | ||
echo -e "OK! \c" # -e 开启转义 \c 不换行 | ||
echo "It is a test" | ||
``` | ||
|
||
输出结果: | ||
|
||
``` | ||
OK! It is a test | ||
``` | ||
|
||
### 6.显示结果定向至文件 | ||
|
||
``` | ||
echo "It is a test" > myfile | ||
``` | ||
|
||
### 7.原样输出字符串,不进行转义或取变量(用单引号) | ||
|
||
``` | ||
echo '$name\"' | ||
``` | ||
|
||
输出结果: | ||
|
||
``` | ||
$name\" | ||
``` | ||
|
||
### 8.显示命令执行结果 | ||
|
||
``` | ||
echo `date` | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Shell 函数 | ||
|
||
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。 | ||
|
||
shell中函数的定义格式如下: | ||
|
||
``` | ||
[ function ] funname [()] | ||
{ | ||
action; | ||
[return int;] | ||
} | ||
``` | ||
|
||
说明: | ||
|
||
- 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。 | ||
- 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255 | ||
|
||
下面的例子定义了一个函数并进行调用: | ||
|
||
``` | ||
#!/bin/bash | ||
#!/bin/bash | ||
hello(){ | ||
echo "hello shell func" | ||
} | ||
echo "-----start-----" | ||
hello | ||
echo "-----end-----" | ||
``` | ||
|
||
输出结果: | ||
|
||
``` | ||
-----start----- | ||
hello shell func | ||
-----end----- | ||
``` | ||
|
||
下面定义一个带有return语句的函数: | ||
|
||
``` | ||
#!/bin/bash | ||
funWithReturn(){ | ||
echo "这个函数会对输入的两个数字进行相加运算..." | ||
echo "输入第一个数字: " | ||
read aNum | ||
echo "输入第二个数字: " | ||
read anotherNum | ||
echo "两个数字分别为 $aNum 和 $anotherNum !" | ||
return $(($aNum+$anotherNum)) | ||
} | ||
funWithReturn | ||
echo "输入的两个数字之和为 $? !" | ||
``` | ||
|
||
输出类似下面: | ||
|
||
``` | ||
这个函数会对输入的两个数字进行相加运算... | ||
输入第一个数字: | ||
1 | ||
输入第二个数字: | ||
2 | ||
两个数字分别为 1 和 2 ! | ||
输入的两个数字之和为 3 ! | ||
``` | ||
|
||
函数返回值在调用该函数后通过 $? 来获得。 | ||
|
||
注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。 | ||
|
||
------ | ||
|
||
## 函数参数 | ||
|
||
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数... | ||
|
||
带参数的函数示例: | ||
|
||
``` | ||
#!/bin/bash | ||
funWithParam(){ | ||
echo "第一个参数为 $1 !" | ||
echo "第二个参数为 $2 !" | ||
echo "第十个参数为 $10 !" | ||
echo "第十个参数为 ${10} !" | ||
echo "第十一个参数为 ${11} !" | ||
echo "参数总数有 $# 个!" | ||
echo "作为一个字符串输出所有参数 $* !" | ||
} | ||
funWithParam 1 2 3 4 5 6 7 8 9 34 73 | ||
``` | ||
|
||
输出结果: | ||
|
||
``` | ||
第一个参数为 1 ! | ||
第二个参数为 2 ! | ||
第十个参数为 10 ! | ||
第十个参数为 34 ! | ||
第十一个参数为 73 ! | ||
参数总数有 11 个! | ||
作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 ! | ||
``` | ||
|
||
注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。 | ||
|
||
另外,还有几个特殊字符用来处理参数: | ||
|
||
| 参数处理 | 说明 | | ||
| :------- | :----------------------------------------------------------- | | ||
| $# | 传递到脚本或函数的参数个数 | | ||
| $* | 以一个单字符串显示所有向脚本传递的参数 | | ||
| $$ | 脚本运行的当前进程ID号 | | ||
| $! | 后台运行的最后一个进程的ID号 | | ||
| $@ | 与$*相同,但是使用时加引号,并在引号中返回每个参数。 | | ||
| $- | 显示Shell使用的当前选项,与set命令功能相同。 | | ||
| $? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
## 基本运算符 | ||
|
||
shell 和其他编程语言一样,支持多种运算符,包括: | ||
|
||
- 算数运算符 | ||
- 关系运算符 | ||
- 布尔运算符 | ||
- 字符串运算符 | ||
- 文件测试运算符 | ||
|
||
原生bash不支持简单的数学运算,但是可以通过其他命令来实现, | ||
例如 [awk](../command/FileManagement.md) 和 [expr](../command/FileManagement.md) , | ||
[expr](../command/FileManagement.md) 最常用。 | ||
|
||
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。 | ||
|
||
例如,两个数相加(**注意使用的是反引号 \**`\** 而不是单引号 \**'\****): | ||
```shell script | ||
#!/bin/bash | ||
val=`expr 2 + 2` | ||
echo "sum : ${val}" | ||
``` | ||
两点注意: | ||
表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。 | ||
完整的表达式要被 ` ` 包含,注意这个字符不是常用的单引号,在 Esc 键下边。 | ||
|
||
## 算术运算符 | ||
|
||
下表列出了常用的算术运算符,假定变量 a 为 10,变量 b 为 20: | ||
|
||
| 运算符 | 说明 | 举例 | | ||
| :----- | :-------------------------------------------- | :---------------------------- | | ||
| + | 加法 | `expr $a + $b` 结果为 30。 | | ||
| - | 减法 | `expr $a - $b` 结果为 -10。 | | ||
| * | 乘法 | `expr $a \* $b` 结果为 200。 | | ||
| / | 除法 | `expr $b / $a` 结果为 2。 | | ||
| % | 取余 | `expr $b % $a` 结果为 0。 | | ||
| = | 赋值 | a=$b 将把变量 b 的值赋给 a。 | | ||
| == | 相等。用于比较两个数字,相同则返回 true。 | [ $a == $b ] 返回 false。 | | ||
| != | 不相等。用于比较两个数字,不相同则返回 true。 | [ $a != $b ] 返回 true。 | | ||
|
||
**注意:**条件表达式要放在方括号之间,并且要有空格,例如: **[$a==$b]** 是错误的,必须写成 **[ $a == $b ]**。 | ||
|
||
```shell | ||
#!/bin/bash | ||
val=`expr 2 + 2` | ||
echo "sum : ${val}" | ||
|
||
a=2021 | ||
b=126 | ||
|
||
val=`expr $a + $b` | ||
echo "a + b : $val" | ||
|
||
val=`expr $a - $b` | ||
echo "a - b : $val" | ||
|
||
val=`expr $a \* $b` | ||
echo "a * b : $val" | ||
|
||
val=`expr $b / $a` | ||
echo "b / a : $val" | ||
|
||
val=`expr $b % $a` | ||
echo "b % a : $val" | ||
|
||
if [ $a == $b ] | ||
then | ||
echo "a 等于 b" | ||
fi | ||
if [ $a != $b ] | ||
then | ||
echo "a 不等于 b" | ||
fi | ||
``` | ||
|
||
|
||
|
Oops, something went wrong.