diff --git a/codes/shell/README.md b/codes/shell/README.md deleted file mode 100644 index 8f1b28a..0000000 --- a/codes/shell/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Shell 示例源码 - -> 本目录的代码是和 [`Shell 教程`](https://github.com/dunwu/os-tutorial/blob/master/docs/linux/scripts/shell.md) 相关的示例代码。 diff --git a/codes/shell/action/README.md b/codes/shell/action/README.md new file mode 100644 index 0000000..5607683 --- /dev/null +++ b/codes/shell/action/README.md @@ -0,0 +1,3 @@ +# Shell 脚本实战 + +> 本目录的代码是本人在学习、开发中总结的一些面向实战的 Shell 代码。 diff --git a/codes/shell/practice/system/useradd.sh b/codes/shell/action/system/useradd.sh similarity index 100% rename from codes/shell/practice/system/useradd.sh rename to codes/shell/action/system/useradd.sh diff --git a/codes/shell/demos/README.md b/codes/shell/demos/README.md new file mode 100644 index 0000000..2005a90 --- /dev/null +++ b/codes/shell/demos/README.md @@ -0,0 +1,3 @@ +# Shell 示例源码 + +> 本目录的代码是和 『[一篇文章让你彻底掌握 shell 语言](https://github.com/dunwu/os-tutorial/blob/master/docs/linux/scripts/shell.md)』 相配套的示例代码。 diff --git a/codes/shell/array-demo.sh b/codes/shell/demos/array-demo.sh similarity index 100% rename from codes/shell/array-demo.sh rename to codes/shell/demos/array-demo.sh diff --git a/codes/shell/comment-demo.sh b/codes/shell/demos/comment-demo.sh similarity index 100% rename from codes/shell/comment-demo.sh rename to codes/shell/demos/comment-demo.sh diff --git a/codes/shell/demos/debug-demo.sh b/codes/shell/demos/debug-demo.sh new file mode 100644 index 0000000..37afac6 --- /dev/null +++ b/codes/shell/demos/debug-demo.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +# 开启 debug +set -x +for (( i = 0; i < 3; i++ )); do + printf ${i} +done +# 关闭 debug +set +x +# Output: +# + (( i = 0 )) +# + (( i < 3 )) +# + printf 0 +# 0+ (( i++ )) +# + (( i < 3 )) +# + printf 1 +# 1+ (( i++ )) +# + (( i < 3 )) +# + printf 2 +# 2+ (( i++ )) +# + (( i < 3 )) +# + set +x + +for i in {1..5}; do printf ${i}; done +printf "\n" +# Output: 12345 diff --git a/codes/shell/demos/echo-demo.sh b/codes/shell/demos/echo-demo.sh new file mode 100644 index 0000000..083c0dc --- /dev/null +++ b/codes/shell/demos/echo-demo.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# 输出普通字符串 +echo "hello, world" +# Output: hello, world + +# 输出含变量的字符串 +echo "hello, \"zp\"" +# Output: hello, "zp" + +# 输出含变量的字符串 +name=zp +echo "hello, \"${name}\"" +# Output: hello, "zp" + +# 输出含换行符的字符串 +echo "YES\nNO" +# Output: YES\nNO +echo -e "YES\nNO" # -e 开启转义 +# Output: +# YES +# NO + +# 输出含不换行符的字符串 +echo "YES" +echo "NO" +# Output: +# YES +# NO + +echo -e "YES\c" # -e 开启转义 \c 不换行 +echo "NO" +# Output: +# YESNO + +# 输出内容定向至文件 +echo "test" > test.txt + +# 输出执行结果 +echo `pwd` +# Output:(当前目录路径) diff --git a/codes/shell/demos/function/function-demo.sh b/codes/shell/demos/function/function-demo.sh new file mode 100644 index 0000000..7db68a7 --- /dev/null +++ b/codes/shell/demos/function/function-demo.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +calc(){ + PS3="choose the oper: " + select oper in + - \* / # 生成操作符选择菜单 + do + echo -n "enter first num: " && read x # 读取输入参数 + echo -n "enter second num: " && read y # 读取输入参数 + exec + case ${oper} in + "+") + return $((${x} + ${y})) + ;; + "-") + return $((${x} - ${y})) + ;; + "*") + return $((${x} * ${y})) + ;; + "/") + return $((${x} / ${y})) + ;; + *) + echo "${oper} is not support!" + return 0 + ;; + esac + break + done +} +calc +echo "the result is: $?" # $? 获取 calc 函数返回值 +# $ ./function-demo.sh +# 1) + +# 2) - +# 3) * +# 4) / +# choose the oper: 3 +# enter first num: 10 +# enter second num: 10 +# the result is: 100 diff --git a/codes/shell/demos/function/function-demo2.sh b/codes/shell/demos/function/function-demo2.sh new file mode 100644 index 0000000..51dd54b --- /dev/null +++ b/codes/shell/demos/function/function-demo2.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +runner() { + return 0 +} + +name=zp +paramsFunction(){ + echo "函数第一个入参:$1" + echo "函数第二个入参:$2" + echo "传递到脚本的参数个数:$#" + echo "所有参数:" + printf "+ %s\n" "$*" + echo "脚本运行的当前进程 ID 号:$$" + echo "后台运行的最后一个进程的 ID 号:$!" + echo "所有参数:" + printf "+ %s\n" "$@" + echo "Shell 使用的当前选项:$-" + runner + echo "runner 函数的返回值:$?" +} +paramsFunction 1 "abc" "hello, \"zp\"" +# Output: +# 函数第一个入参:1 +# 函数第二个入参:abc +# 传递到脚本的参数个数:3 +# 所有参数: +# + 1 abc hello, "zp" +# 脚本运行的当前进程 ID 号:26400 +# 后台运行的最后一个进程的 ID 号: +# 所有参数: +# + 1 +# + abc +# + hello, "zp" +# Shell 使用的当前选项:hB +# runner 函数的返回值:0 diff --git a/codes/shell/helloworld.sh b/codes/shell/demos/helloworld.sh similarity index 100% rename from codes/shell/helloworld.sh rename to codes/shell/demos/helloworld.sh diff --git a/codes/shell/operator/operator-demo.sh b/codes/shell/demos/operator/operator-demo.sh similarity index 100% rename from codes/shell/operator/operator-demo.sh rename to codes/shell/demos/operator/operator-demo.sh diff --git a/codes/shell/operator/operator-demo2.sh b/codes/shell/demos/operator/operator-demo2.sh similarity index 100% rename from codes/shell/operator/operator-demo2.sh rename to codes/shell/demos/operator/operator-demo2.sh diff --git a/codes/shell/operator/operator-demo3.sh b/codes/shell/demos/operator/operator-demo3.sh similarity index 100% rename from codes/shell/operator/operator-demo3.sh rename to codes/shell/demos/operator/operator-demo3.sh diff --git a/codes/shell/operator/operator-demo4.sh b/codes/shell/demos/operator/operator-demo4.sh similarity index 100% rename from codes/shell/operator/operator-demo4.sh rename to codes/shell/demos/operator/operator-demo4.sh diff --git a/codes/shell/operator/operator-demo5.sh b/codes/shell/demos/operator/operator-demo5.sh similarity index 100% rename from codes/shell/operator/operator-demo5.sh rename to codes/shell/demos/operator/operator-demo5.sh diff --git a/codes/shell/operator/operator-demo6.sh b/codes/shell/demos/operator/operator-demo6.sh similarity index 100% rename from codes/shell/operator/operator-demo6.sh rename to codes/shell/demos/operator/operator-demo6.sh diff --git a/codes/shell/demos/printf-demo.sh b/codes/shell/demos/printf-demo.sh new file mode 100644 index 0000000..3750339 --- /dev/null +++ b/codes/shell/demos/printf-demo.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +# 单引号 +printf '%d %s\n' 1 "abc" +# Output:1 abc + +# 双引号 +printf "%d %s\n" 1 "abc" +# Output:1 abc + +# 无引号 +printf %s abcdef +# Output: abcdef(并不会换行) + +# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出 +printf "%s\n" abc def +# Output: +# abc +# def + +printf "%s %s %s\n" a b c d e f g h i j +# Output: +# a b c +# d e f +# g h i +# j + +# 如果没有参数,那么 %s 用 NULL 代替,%d 用 0 代替 +printf "%s and %d \n" +# Output: +# and 0 + +# 格式化输出 +printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg +printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 +printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 +printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876 +# Output: +# 姓名 性别 体重kg +# 郭靖 男 66.12 +# 杨过 男 48.65 +# 郭芙 女 47.99 diff --git a/codes/shell/statement/break-demo.sh b/codes/shell/demos/statement/break-demo.sh similarity index 100% rename from codes/shell/statement/break-demo.sh rename to codes/shell/demos/statement/break-demo.sh diff --git a/codes/shell/statement/case-demo.sh b/codes/shell/demos/statement/case-demo.sh similarity index 100% rename from codes/shell/statement/case-demo.sh rename to codes/shell/demos/statement/case-demo.sh diff --git a/codes/shell/statement/continue-demo.sh b/codes/shell/demos/statement/continue-demo.sh similarity index 100% rename from codes/shell/statement/continue-demo.sh rename to codes/shell/demos/statement/continue-demo.sh diff --git a/codes/shell/statement/for-demo.sh b/codes/shell/demos/statement/for-demo.sh similarity index 100% rename from codes/shell/statement/for-demo.sh rename to codes/shell/demos/statement/for-demo.sh diff --git a/codes/shell/statement/if-demo.sh b/codes/shell/demos/statement/if-demo.sh similarity index 100% rename from codes/shell/statement/if-demo.sh rename to codes/shell/demos/statement/if-demo.sh diff --git a/codes/shell/statement/select-demo.sh b/codes/shell/demos/statement/select-demo.sh similarity index 100% rename from codes/shell/statement/select-demo.sh rename to codes/shell/demos/statement/select-demo.sh diff --git a/codes/shell/statement/until-demo.sh b/codes/shell/demos/statement/until-demo.sh similarity index 100% rename from codes/shell/statement/until-demo.sh rename to codes/shell/demos/statement/until-demo.sh diff --git a/codes/shell/statement/while-demo.sh b/codes/shell/demos/statement/while-demo.sh similarity index 100% rename from codes/shell/statement/while-demo.sh rename to codes/shell/demos/statement/while-demo.sh diff --git a/codes/shell/string-demo.sh b/codes/shell/demos/string-demo.sh similarity index 100% rename from codes/shell/string-demo.sh rename to codes/shell/demos/string-demo.sh diff --git a/codes/shell/variable-demo.sh b/codes/shell/demos/variable-demo.sh similarity index 100% rename from codes/shell/variable-demo.sh rename to codes/shell/demos/variable-demo.sh diff --git a/codes/shell/practice/README.md b/codes/shell/practice/README.md deleted file mode 100644 index 28cc5d6..0000000 --- a/codes/shell/practice/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# OS `codes/practice` - -> 本目录的代码是本人在开发中总结的一些实用代码。 ->