mirror of
https://github.com/dunwu/linux-tutorial.git
synced 2024-04-15 19:55:24 +08:00
update shell scripts
This commit is contained in:
parent
5cf5fc7b40
commit
9ff1cd5f22
@ -1,3 +0,0 @@
|
||||
# Shell 示例源码
|
||||
|
||||
> 本目录的代码是和 [`Shell 教程`](https://github.com/dunwu/os-tutorial/blob/master/docs/linux/scripts/shell.md) 相关的示例代码。
|
3
codes/shell/action/README.md
Normal file
3
codes/shell/action/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Shell 脚本实战
|
||||
|
||||
> 本目录的代码是本人在学习、开发中总结的一些面向实战的 Shell 代码。
|
3
codes/shell/demos/README.md
Normal file
3
codes/shell/demos/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Shell 示例源码
|
||||
|
||||
> 本目录的代码是和 『[一篇文章让你彻底掌握 shell 语言](https://github.com/dunwu/os-tutorial/blob/master/docs/linux/scripts/shell.md)』 相配套的示例代码。
|
26
codes/shell/demos/debug-demo.sh
Normal file
26
codes/shell/demos/debug-demo.sh
Normal file
@ -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
|
41
codes/shell/demos/echo-demo.sh
Normal file
41
codes/shell/demos/echo-demo.sh
Normal file
@ -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:(当前目录路径)
|
41
codes/shell/demos/function/function-demo.sh
Normal file
41
codes/shell/demos/function/function-demo.sh
Normal file
@ -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
|
36
codes/shell/demos/function/function-demo2.sh
Normal file
36
codes/shell/demos/function/function-demo2.sh
Normal file
@ -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
|
42
codes/shell/demos/printf-demo.sh
Normal file
42
codes/shell/demos/printf-demo.sh
Normal file
@ -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
|
@ -1,4 +0,0 @@
|
||||
# OS `codes/practice`
|
||||
|
||||
> 本目录的代码是本人在开发中总结的一些实用代码。
|
||||
>
|
Loading…
Reference in New Issue
Block a user