From 2805e3d49fdaade75588f15a1370e8acee8e2a03 Mon Sep 17 00:00:00 2001 From: LouisFonda Date: Sun, 14 Jul 2024 09:43:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20shell=20script=20=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=92=8C=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/{basics => basic}/.gitkeep | 0 tutorials/basic/01-hello-world.sh | 8 ++++++++ tutorials/basic/02-full-name.sh | 10 ++++++++++ tutorials/basic/03-multiplying.sh | 9 +++++++++ tutorials/basic/04-cal-pi.sh | 9 +++++++++ tutorials/basic/05-file-info.sh | 21 +++++++++++++++++++++ tutorials/basic/06-ans-yn.sh | 10 ++++++++++ tutorials/basic/07-show-params.sh | 20 ++++++++++++++++++++ tutorials/basic/08-ans-yn-2.sh | 16 ++++++++++++++++ tutorials/basic/09-ans-yn-3.sh | 16 ++++++++++++++++ tutorials/basic/10-hello.sh | 15 +++++++++++++++ 11 files changed, 134 insertions(+) rename tutorials/{basics => basic}/.gitkeep (100%) create mode 100755 tutorials/basic/01-hello-world.sh create mode 100755 tutorials/basic/02-full-name.sh create mode 100755 tutorials/basic/03-multiplying.sh create mode 100755 tutorials/basic/04-cal-pi.sh create mode 100644 tutorials/basic/05-file-info.sh create mode 100644 tutorials/basic/06-ans-yn.sh create mode 100644 tutorials/basic/07-show-params.sh create mode 100644 tutorials/basic/08-ans-yn-2.sh create mode 100644 tutorials/basic/09-ans-yn-3.sh create mode 100644 tutorials/basic/10-hello.sh diff --git a/tutorials/basics/.gitkeep b/tutorials/basic/.gitkeep similarity index 100% rename from tutorials/basics/.gitkeep rename to tutorials/basic/.gitkeep diff --git a/tutorials/basic/01-hello-world.sh b/tutorials/basic/01-hello-world.sh new file mode 100755 index 0000000..bb7e434 --- /dev/null +++ b/tutorials/basic/01-hello-world.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# Program: +# 这是一个shell编写的helloworld程序。 +# History: +# 2024/07/12 LouisFonda first release + +echo -e "Hello World! \a\n" # \a表示可以输出声音 +exit 0 diff --git a/tutorials/basic/02-full-name.sh b/tutorials/basic/02-full-name.sh new file mode 100755 index 0000000..6006ab2 --- /dev/null +++ b/tutorials/basic/02-full-name.sh @@ -0,0 +1,10 @@ +#!/bin/bash +#Program: +# 输入firstName和LastName,屏幕输出fullName +#History: +# 2024/07/12 LouisFonda first release + +read -p "请输入你的姓:" lastName +read -p "请输入你的名:" firstName +echo "你的完整姓名是: ${lastName}${firstName}" + diff --git a/tutorials/basic/03-multiplying.sh b/tutorials/basic/03-multiplying.sh new file mode 100755 index 0000000..c8e8815 --- /dev/null +++ b/tutorials/basic/03-multiplying.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#Program: +# 输入两个值计算它们乘积的结果 +#History: +# 2024/07/12 LouisFonda first release + +read -p "请输入要计算的第一个值:" num1 +read -p "请输入要计算的第二个值:" num2 +echo "最终结果为: $(($num1 * $num2))" diff --git a/tutorials/basic/04-cal-pi.sh b/tutorials/basic/04-cal-pi.sh new file mode 100755 index 0000000..3608d54 --- /dev/null +++ b/tutorials/basic/04-cal-pi.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#Program: +# 计算pi的值,输入要精确到小数多少位 +#History: +# 2024/07/12 LouisFonda first release + +echo -e "**计算pi\n**" +read -p "请输入你要保留几位小数?:" scale +echo "scale=${scale};4*a(1)" | bc -lq diff --git a/tutorials/basic/05-file-info.sh b/tutorials/basic/05-file-info.sh new file mode 100644 index 0000000..d0fc135 --- /dev/null +++ b/tutorials/basic/05-file-info.sh @@ -0,0 +1,21 @@ +#!/bin/bash +#Program: +# 输入文件路径判断文件的类型,并输出文件的权限信息 +#History +# 2024/07/13 LouisFonda + +# 1 让使用者输入文件名,并且判断使用者是否真的有输入字符串。 +echo -e "请输入文件名,我将检查文件类型和权限。\n\n" +read -p "文件名:" fileName +test -z ${fileName} && echo "你必须输入文件路名!" && exit 0 +# 2 判断文件是否存在?若不存在则显示讯息并结束脚本 +test ! -e ${fileName} && echo "你输入的文件不存在" && exit 0 +# 3 开始判断文件类型与属性 +test -f ${fileName} && fileType="常规文件" +test -d ${fileName} && fileType="文件夹" +test -r ${fileName} && perm="可读" +test -w ${fileName} && perm="${perm} 可写" +test -x ${fileName} && perm="${perm} 可执行" +# 4 开始输出信息 +echo "文件: ${fileName} is ${fileType}" +echo "你对文件拥有的权限是: ${perm}" \ No newline at end of file diff --git a/tutorials/basic/06-ans-yn.sh b/tutorials/basic/06-ans-yn.sh new file mode 100644 index 0000000..709f396 --- /dev/null +++ b/tutorials/basic/06-ans-yn.sh @@ -0,0 +1,10 @@ +#!/bin/bash +#Program: +# 判断用户的输入是确认还是拒绝 +# History: +# 2024/07/14 LouisFonda + +read -p "请输入(y/n)" yn +[ "${yn}" == "y" -o "${yn}" == "Y" ] && echo "你确认了!" && exit 0 +[ "${yn}" == "n" -o "${yn}" == "N" ] && echo "你拒绝了!" && exit 0 +echo "我不理解你的输入!" && exit 0 \ No newline at end of file diff --git a/tutorials/basic/07-show-params.sh b/tutorials/basic/07-show-params.sh new file mode 100644 index 0000000..0961d36 --- /dev/null +++ b/tutorials/basic/07-show-params.sh @@ -0,0 +1,20 @@ +#!/bin/bash +#Program: +# 显示用户执行命令时输入的参数 +# History: +# 2024/07/14 LouisFonda + +echo "这个程序的名字 ==> ${0}" +echo "总共有多少个参数 ==> $#" +# 判断输入的参数是否小于2 +[ "$#" -lt 2 ] && echo "参数不能小于两个" && exit 0 +echo "你的所有的参数是 ==》'$@'" +echo "你的所有的参数是 ==》'$*'" +echo "第一个参数是 ==》${1}" +echo "第二个参数是 ==》${2}" +shift +echo "偏移后的所有参数 => '$@'" # 会把参数向左移动,后面可以接参数,比如 one two three shift 2 => three + +# $@表示 "$1" "$2" "$3" +# $*表示 "$1 $2 $3" + diff --git a/tutorials/basic/08-ans-yn-2.sh b/tutorials/basic/08-ans-yn-2.sh new file mode 100644 index 0000000..f9b663f --- /dev/null +++ b/tutorials/basic/08-ans-yn-2.sh @@ -0,0 +1,16 @@ +#!/bin/bash +#Program: +# 这个程序是对07-ans-yn.sh的改进版本,使用条件判断式-if +#History: +# 2024/07/14 LouisFonda + +read -p "请输入:" yn +if [ "${yn}" == 'y' ] || [ "${yn}" == 'Y' ]; then + echo "你确认了!" + exit 0 +fi +if [ "${yn}" == 'n' ] || [ "${yn}" == 'N' ]; then + echo "你拒绝了!" + exit 0 +fi +echo "我不理解你的输出!" && exit 0 \ No newline at end of file diff --git a/tutorials/basic/09-ans-yn-3.sh b/tutorials/basic/09-ans-yn-3.sh new file mode 100644 index 0000000..d06ef00 --- /dev/null +++ b/tutorials/basic/09-ans-yn-3.sh @@ -0,0 +1,16 @@ +#!/bin/bash +#Program: +# 这个程序是对07-ans-yn.sh的改进版本,使用条件判断式-if +#History: +# 2024/07/14 LouisFonda + +read -p "请输入:" yn +if [ "${yn}" == 'y' ] || [ "${yn}" == 'Y' ]; then + echo "你确认了!" + exit 0 +elif [ "${yn}" == 'n' ] || [ "${yn}" == 'N' ]; then + echo "你拒绝了!" + exit 0 +else + echo "我不理解你的输出!" && exit 0 +fi \ No newline at end of file diff --git a/tutorials/basic/10-hello.sh b/tutorials/basic/10-hello.sh new file mode 100644 index 0000000..4447a7f --- /dev/null +++ b/tutorials/basic/10-hello.sh @@ -0,0 +1,15 @@ +#!/bin/bash +#Program: +# 1. 判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?"; +# 2. 如果没有加任何参数,就提示使用者必须要使用的参数下达法; +# 3. 而如果加入的参数不是 hello ,就提醒使用者仅能使用 hello 为参数。 +#History: +# 2024/07/14 LouisFonda + +if [ ${1} == "hello" ]; then + echo "hello, how are you?" +elif [ "${1}" == "" ]; then + echo "你必须输入参数!" +else + echo "这个参数必须是 'hello'" +fi \ No newline at end of file