feat: shell script 基本功能和条件判断式
This commit is contained in:
parent
ffcea49184
commit
2805e3d49f
8
tutorials/basic/01-hello-world.sh
Executable file
8
tutorials/basic/01-hello-world.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
# Program:
|
||||
# 这是一个shell编写的helloworld程序。
|
||||
# History:
|
||||
# 2024/07/12 LouisFonda<yigencong@yahoo.com> first release
|
||||
|
||||
echo -e "Hello World! \a\n" # \a表示可以输出声音
|
||||
exit 0
|
10
tutorials/basic/02-full-name.sh
Executable file
10
tutorials/basic/02-full-name.sh
Executable file
@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 输入firstName和LastName,屏幕输出fullName
|
||||
#History:
|
||||
# 2024/07/12 LouisFonda<yigencong> first release
|
||||
|
||||
read -p "请输入你的姓:" lastName
|
||||
read -p "请输入你的名:" firstName
|
||||
echo "你的完整姓名是: ${lastName}${firstName}"
|
||||
|
9
tutorials/basic/03-multiplying.sh
Executable file
9
tutorials/basic/03-multiplying.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 输入两个值计算它们乘积的结果
|
||||
#History:
|
||||
# 2024/07/12 LouisFonda<yigencong@yahoo.com> first release
|
||||
|
||||
read -p "请输入要计算的第一个值:" num1
|
||||
read -p "请输入要计算的第二个值:" num2
|
||||
echo "最终结果为: $(($num1 * $num2))"
|
9
tutorials/basic/04-cal-pi.sh
Executable file
9
tutorials/basic/04-cal-pi.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 计算pi的值,输入要精确到小数多少位
|
||||
#History:
|
||||
# 2024/07/12 LouisFonda<yigencong@yahoo.com> first release
|
||||
|
||||
echo -e "**计算pi\n**"
|
||||
read -p "请输入你要保留几位小数?:" scale
|
||||
echo "scale=${scale};4*a(1)" | bc -lq
|
21
tutorials/basic/05-file-info.sh
Normal file
21
tutorials/basic/05-file-info.sh
Normal file
@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 输入文件路径判断文件的类型,并输出文件的权限信息
|
||||
#History
|
||||
# 2024/07/13 LouisFonda<yigenocng@yahoo.com>
|
||||
|
||||
# 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}"
|
10
tutorials/basic/06-ans-yn.sh
Normal file
10
tutorials/basic/06-ans-yn.sh
Normal file
@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 判断用户的输入是确认还是拒绝
|
||||
# History:
|
||||
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
|
||||
|
||||
read -p "请输入(y/n)" yn
|
||||
[ "${yn}" == "y" -o "${yn}" == "Y" ] && echo "你确认了!" && exit 0
|
||||
[ "${yn}" == "n" -o "${yn}" == "N" ] && echo "你拒绝了!" && exit 0
|
||||
echo "我不理解你的输入!" && exit 0
|
20
tutorials/basic/07-show-params.sh
Normal file
20
tutorials/basic/07-show-params.sh
Normal file
@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 显示用户执行命令时输入的参数
|
||||
# History:
|
||||
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
|
||||
|
||||
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"
|
||||
|
16
tutorials/basic/08-ans-yn-2.sh
Normal file
16
tutorials/basic/08-ans-yn-2.sh
Normal file
@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 这个程序是对07-ans-yn.sh的改进版本,使用条件判断式-if
|
||||
#History:
|
||||
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
|
||||
|
||||
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
|
16
tutorials/basic/09-ans-yn-3.sh
Normal file
16
tutorials/basic/09-ans-yn-3.sh
Normal file
@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 这个程序是对07-ans-yn.sh的改进版本,使用条件判断式-if
|
||||
#History:
|
||||
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
|
||||
|
||||
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
|
15
tutorials/basic/10-hello.sh
Normal file
15
tutorials/basic/10-hello.sh
Normal file
@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
#Program:
|
||||
# 1. 判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?";
|
||||
# 2. 如果没有加任何参数,就提示使用者必须要使用的参数下达法;
|
||||
# 3. 而如果加入的参数不是 hello ,就提醒使用者仅能使用 hello 为参数。
|
||||
#History:
|
||||
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
|
||||
|
||||
if [ ${1} == "hello" ]; then
|
||||
echo "hello, how are you?"
|
||||
elif [ "${1}" == "" ]; then
|
||||
echo "你必须输入参数!"
|
||||
else
|
||||
echo "这个参数必须是 'hello'"
|
||||
fi
|
Loading…
x
Reference in New Issue
Block a user