forked from Ewenwan/ShiYanLou
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,87 @@ | |
# 获取文件的绝对路径 | ||
test.sh | ||
```sh | ||
root_path=$(readlink -f "$(dirname "$0")") | ||
root_path=$(readlink -f "$(dirname "$0")") # "$0"为脚本名称 | ||
echo $root_path | ||
``` | ||
|
||
不管在那里运行 test.sh | ||
|
||
该脚本打印的都是 test.sh 所在的绝对路径 | ||
|
||
|
||
多行注释 | ||
|
||
```sh | ||
:<<! | ||
这里是多行注释! | ||
作者:poplar | ||
时间:2019年5月5日20:53:21 | ||
联系:[email protected] | ||
! | ||
: ' | ||
这里是多行注释! | ||
作者:poplar | ||
时间:2019年5月5日20:53:21 | ||
联系:[email protected] | ||
' | ||
``` | ||
|
||
与脚本文件有关系的变量 | ||
```sh | ||
$0 获取脚本的名称 | ||
|
||
$# 获取脚本的参数数量 | ||
$n 获取指定位置的参数 | ||
$? 获取上一个命令执行的返回值 | ||
``` | ||
|
||
变量的默认值 | ||
```sh | ||
变量为null 或 空字符串时使用默认值:${变量名:-默认值} | ||
|
||
|
||
a=123 | ||
echo "test: ${a:-666}" | ||
echo "test: ${b:-666}" | ||
|
||
# 输出 | ||
test: 123 | ||
test: 666 | ||
变量不为空时使用默认值:${变量名:+默认值} | ||
|
||
a=123 | ||
echo "test: ${a:+666}" | ||
echo "test: ${b:+666}" | ||
# 输出 | ||
test: 666 | ||
test: | ||
``` | ||
Shell数组¶ | ||
```sh | ||
arr=(A "B" C D) | ||
|
||
arr[0]=A | ||
arr[1]=B | ||
arr[2]=C | ||
arr[3]=D | ||
|
||
${arr[1]} | ||
echo "数组的所有元素:${arr[*]} 长度${#arr[*]}" | ||
echo "${arr[@]}" | ||
|
||
加元素 | ||
arr+=(666) | ||
arr+=(123 ddd) | ||
|
||
删除元素 | ||
unset a[2] | ||
|
||
清空数组 | ||
unset arr | ||
|
||
``` | ||
|
||
# 数字比较 | ||
```sh | ||
my_scale=0.056 | ||
|
@@ -46,6 +119,85 @@ else | |
echo my_scale equal to zero | ||
fi | ||
``` | ||
算数运算符 | ||
```sh | ||
原生bash不支持数学运算,使用expr完成表达式的求值 | ||
val=`expr 3 + 2` | ||
echo "计算结果: $val" | ||
|
||
if (( a > b )) 如果a>b # 直接使用>或<等符号,需要在数值操作符(())中使用 | ||
|
||
关系 支持数字或内容是数字的字符串 | ||
-eq [ $a -eq $b ] 返回 false。 相等 | ||
-ne [ $a -ne $b ] 返回 true。 不相等 | ||
-gt [ $a -gt $b ] 返回 false。 大于 | ||
-lt [ $a -lt $b ] 返回 true。 小于 | ||
-ge [ $a -ge $b ] 返回 false。 大于等于 | ||
-le [ $a -le $b ] 返回 true。 小于等于 | ||
|
||
逻辑运算符 | ||
-a | ||
(and )等同于&& (注意空格) | ||
|
||
if [ $a -gt $b -a $a -lt $c ] | ||
|
||
-o | ||
(or )等同于|| (注意空格) | ||
|
||
if [ $a -gt $b -o $a -lt $c ] | ||
|
||
&& | ||
例:命令1 && 命令2 | ||
|
||
执行传递:命令1成功才执行命令2 | ||
|
||
两者必须都成功,整个表达式才为成功 | ||
|
||
|| | ||
例:命令1 || 命令2 | ||
|
||
执行传递:命令1失败才执行命令2 | ||
|
||
两者只要有一个成功,整个表达式即为成功 | ||
|
||
如果使用逻辑判断父判断条件成立,每个条件必须使用操作符包裹: | ||
|
||
如果a>b且a<c | ||
if (( $a > $b && $a < $c )) | ||
|
||
if (( $a > $b )) && (( $a < $c )) | ||
|
||
如果a>b或a<=c | ||
if (( a > b || a <= c )) | ||
|
||
if (( a > b )) || (( a <= c )) | ||
|
||
如果a<100且b>100 | ||
if [[ $a -lt 100 && $b -gt 100 ]]空格敏感 | ||
|
||
如果a<100或b>=100 | ||
if [[ $a -lt 100 || $b -ge 100 ]]空格敏感 | ||
|
||
``` | ||
总结:以大于等于、And为例 | ||
[ ] 关系运算符-ge,逻辑运算符-a,空格敏感 | ||
(( )) 关系运算符>=,逻辑运算符&&,空格不敏感,取变量值可省略$ (推荐) | ||
[[ ]] 关系运算符-ge,逻辑运算符&&,空格敏感 | ||
文件运算符 | ||
-f 判断输入内容是否是文件 | ||
-d 判断输入内容是否是目录 | ||
-x 判断输入内容是否有执行权限 | ||
-e 判断输入的文件或文件夹是否存在 | ||
# awk 文本分析 | ||
[awk使用shell变量及shell使用awk中的变量](https://blog.csdn.net/rj042/article/details/72860177) | ||
|