Skip to content

Commit 9ff1cd5

Browse files
committed
update shell scripts
1 parent 5cf5fc7 commit 9ff1cd5

29 files changed

Lines changed: 192 additions & 7 deletions

codes/shell/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

codes/shell/action/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Shell 脚本实战
2+
3+
> 本目录的代码是本人在学习、开发中总结的一些面向实战的 Shell 代码。

codes/shell/demos/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Shell 示例源码
2+
3+
> 本目录的代码是和 『[一篇文章让你彻底掌握 shell 语言](https://github.com/dunwu/os-tutorial/blob/master/docs/linux/scripts/shell.md)』 相配套的示例代码。

codes/shell/demos/debug-demo.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# 开启 debug
4+
set -x
5+
for (( i = 0; i < 3; i++ )); do
6+
printf ${i}
7+
done
8+
# 关闭 debug
9+
set +x
10+
# Output:
11+
# + (( i = 0 ))
12+
# + (( i < 3 ))
13+
# + printf 0
14+
# 0+ (( i++ ))
15+
# + (( i < 3 ))
16+
# + printf 1
17+
# 1+ (( i++ ))
18+
# + (( i < 3 ))
19+
# + printf 2
20+
# 2+ (( i++ ))
21+
# + (( i < 3 ))
22+
# + set +x
23+
24+
for i in {1..5}; do printf ${i}; done
25+
printf "\n"
26+
# Output: 12345

codes/shell/demos/echo-demo.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
# 输出普通字符串
4+
echo "hello, world"
5+
# Output: hello, world
6+
7+
# 输出含变量的字符串
8+
echo "hello, \"zp\""
9+
# Output: hello, "zp"
10+
11+
# 输出含变量的字符串
12+
name=zp
13+
echo "hello, \"${name}\""
14+
# Output: hello, "zp"
15+
16+
# 输出含换行符的字符串
17+
echo "YES\nNO"
18+
# Output: YES\nNO
19+
echo -e "YES\nNO" # -e 开启转义
20+
# Output:
21+
# YES
22+
# NO
23+
24+
# 输出含不换行符的字符串
25+
echo "YES"
26+
echo "NO"
27+
# Output:
28+
# YES
29+
# NO
30+
31+
echo -e "YES\c" # -e 开启转义 \c 不换行
32+
echo "NO"
33+
# Output:
34+
# YESNO
35+
36+
# 输出内容定向至文件
37+
echo "test" > test.txt
38+
39+
# 输出执行结果
40+
echo `pwd`
41+
# Output:(当前目录路径)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
calc(){
4+
PS3="choose the oper: "
5+
select oper in + - \* / # 生成操作符选择菜单
6+
do
7+
echo -n "enter first num: " && read x # 读取输入参数
8+
echo -n "enter second num: " && read y # 读取输入参数
9+
exec
10+
case ${oper} in
11+
"+")
12+
return $((${x} + ${y}))
13+
;;
14+
"-")
15+
return $((${x} - ${y}))
16+
;;
17+
"*")
18+
return $((${x} * ${y}))
19+
;;
20+
"/")
21+
return $((${x} / ${y}))
22+
;;
23+
*)
24+
echo "${oper} is not support!"
25+
return 0
26+
;;
27+
esac
28+
break
29+
done
30+
}
31+
calc
32+
echo "the result is: $?" # $? 获取 calc 函数返回值
33+
# $ ./function-demo.sh
34+
# 1) +
35+
# 2) -
36+
# 3) *
37+
# 4) /
38+
# choose the oper: 3
39+
# enter first num: 10
40+
# enter second num: 10
41+
# the result is: 100
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
runner() {
4+
return 0
5+
}
6+
7+
name=zp
8+
paramsFunction(){
9+
echo "函数第一个入参:$1"
10+
echo "函数第二个入参:$2"
11+
echo "传递到脚本的参数个数:$#"
12+
echo "所有参数:"
13+
printf "+ %s\n" "$*"
14+
echo "脚本运行的当前进程 ID 号:$$"
15+
echo "后台运行的最后一个进程的 ID 号:$!"
16+
echo "所有参数:"
17+
printf "+ %s\n" "$@"
18+
echo "Shell 使用的当前选项:$-"
19+
runner
20+
echo "runner 函数的返回值:$?"
21+
}
22+
paramsFunction 1 "abc" "hello, \"zp\""
23+
# Output:
24+
# 函数第一个入参:1
25+
# 函数第二个入参:abc
26+
# 传递到脚本的参数个数:3
27+
# 所有参数:
28+
# + 1 abc hello, "zp"
29+
# 脚本运行的当前进程 ID 号:26400
30+
# 后台运行的最后一个进程的 ID 号:
31+
# 所有参数:
32+
# + 1
33+
# + abc
34+
# + hello, "zp"
35+
# Shell 使用的当前选项:hB
36+
# runner 函数的返回值:0

0 commit comments

Comments
 (0)