-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash.sh
78 lines (49 loc) · 1.62 KB
/
bash.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# 参数模式
# @Note: 传统的接收参数的方法
echo "get parameters from terminal";
echo "The first parameter is $1";
echo "The second parameter is $2";
echo "The third parameter is $3";
#!/bin/bash
# @Note: 1. loop args
for args in "$@"; do
echo $args
done
#!/bin/bash
for args in "$@"; do
key=${args%=*} #获取 key
value=${args#*=} #获取 value
case "$key" in
--prefix)
PREFIX=$value
echo "PRFFIX=$PREFIX"
;;
--target)
TARGET=$value
echo "TARGET=$TARGET"
;;
esac
done
# sh test.sh --prefix=/home --target=/opt
# [1]Debug 模式
set -x # start debug print
# do something
set +x # start debug print
# [2]Strict 模式
# 开启:单行执行或者pipeline错误即停止
set -e -o pipfail
#来源:https://www.bilibili.com/video/BV1384y1u7yt
# https://www.cnblogs.com/xingmuxin/p/8431970.html
#1、set -e
#"Exit immediately if a simple command exits with a non-zero status."
#在“set -e”之后出现的代码,一旦出现返回值非零,整个脚本就会立即退出。
#2、set -o pipefail
#"If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands in the pipeline exit successfully. This option is disabled by default."
#在这个设置执行后,其后面的代码,包括管道命令的返回值,为最后一个非零的命令的返回值,或者当管道内的所有命令都执行成功后返回零。
# 文件判断
if [[ -e 文件路径 ]]; then
echo "文件存在"
else
echo "文件不存在"
fi