From 2efc15795a11b48a636bc2f8383e13d7a31d41e0 Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 14 Aug 2023 15:53:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=87=AA=E5=B7=B1=E7=BC=96?= =?UTF-8?q?=E5=86=99vim=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day1/README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++ day1/d1.sh | 23 +++++++++++++++++++ day1/d10.sh | 5 +++++ day1/d11.sh | 22 ++++++++++++++++++ day1/d12.sh | 7 ++++++ day1/d13.sh | 45 +++++++++++++++++++++++++++++++++++++ day1/d14.sh | 36 ++++++++++++++++++++++++++++++ day1/d15.sh | 17 ++++++++++++++ day1/d16.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++ day1/d17.sh | 4 ++++ day1/d18.sh | 3 +++ day1/d19.sh | 3 +++ day1/d2.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ day1/d20.sh | 15 +++++++++++++ day1/d3.sh | 5 +++++ day1/d4.sh | 5 +++++ day1/d5.sh | 5 +++++ day1/d6.sh | 6 +++++ day1/d7.sh | 17 ++++++++++++++ day1/d8.sh | 8 +++++++ day1/d9.sh | 4 ++++ day1/svim.sh | 9 ++++++-- 22 files changed, 406 insertions(+), 2 deletions(-) create mode 100644 day1/README.md create mode 100755 day1/d1.sh create mode 100755 day1/d10.sh create mode 100644 day1/d11.sh create mode 100644 day1/d12.sh create mode 100644 day1/d13.sh create mode 100644 day1/d14.sh create mode 100644 day1/d15.sh create mode 100644 day1/d16.sh create mode 100644 day1/d17.sh create mode 100644 day1/d18.sh create mode 100644 day1/d19.sh create mode 100644 day1/d2.sh create mode 100644 day1/d20.sh create mode 100755 day1/d3.sh create mode 100644 day1/d4.sh create mode 100644 day1/d5.sh create mode 100644 day1/d6.sh create mode 100644 day1/d7.sh create mode 100644 day1/d8.sh create mode 100644 day1/d9.sh diff --git a/day1/README.md b/day1/README.md new file mode 100644 index 0000000..e21004b --- /dev/null +++ b/day1/README.md @@ -0,0 +1,54 @@ +在 Bash 脚本中,圆括号 `()` 和花括号 `{}` 有不同的用途和行为。 + +1. 圆括号 `()` + + - 用途: + - 创建子 shell:在圆括号中的命令序列将在一个子 shell 中执行,子 shell 是一个独立的进程,它会继承父 shell 的环境变量和文件描述符,但是变量的修改在子 shell 中不会影响到父 shell。 + - 数组赋值:在圆括号中可以定义数组,并将值赋给数组。 + - 示例: + ```bash + (command1; command2; command3) + ``` + - 注意事项: + - 在圆括号中的命令序列与外部环境是隔离的,所以在子 shell 中定义的变量对于父 shell 是不可见的。 + +2. 花括号 `{}` + - 用途: + - 命令扩展和分组:花括号可以用于扩展和分组命令,可以将多个命令组合在一起,作为一个整体进行处理。 + - 文件名扩展:花括号可以用于生成文件名的列表,例如使用通配符进行文件名扩展。 + - 示例: + ```bash + { command1; command2; } + ``` + - 注意事项: + - 花括号中的命令序列会在当前 shell 中执行,不会创建子 shell。 + - 花括号中的命令序列可以使用分号 `;` 或换行符分隔多个命令。 + +总结: + +- 圆括号 `()` 用于创建子 shell,并在子 shell 中执行命令序列。 +- 花括号 `{}` 用于命令扩展和分组,将多个命令组合在一起进行处理。 + +#### 判断文件 + +test -e "$1" # 与下面等价 +[ -e "$1" ] # 与上面等价 + +更多判断 +-d 是目录 +-f 是文件 +-e 是存在 +-r 是可读 +-w 是可写 +-x 是可执行 +-s 是非空 +-h 是软链接 +-L 是软链接 +-p 是管道 +-S 是 socket +-b 是块设备 +-c 是字符设备 +-t 是终端 +-g 是设置了组 ID +-u 是设置了用户 ID +-k 是设置了粘滞位 diff --git a/day1/d1.sh b/day1/d1.sh new file mode 100755 index 0000000..4076801 --- /dev/null +++ b/day1/d1.sh @@ -0,0 +1,23 @@ +#!/bin/bash +mkdir /home/flykhan/qfedu-linux-advanced-level/day1/cmd_test +echo "1-----mkdir-----end" +cd /home/flykhan/qfedu-linux-advanced-level/day1/cmd_test +echo "2-----cd-----end" +cp /lib/*.so* ./ +echo "3-----cp-----end" +tar jcvf so.tar.bz2 *.so* +echo "4-----tar jcvf-----end" +tar zcvf so.tar.gz *.so* +echo "5-----tar zcvf-----end" +mkdir c_test +echo "6-----mkdir c_test-----end" +tar zxvf so.tar.gz -C ./c_test +echo "7-----tar zxvf-----end" +rm so.tar.gz +echo "8-----rm-----end" +chmod 700 so.tar.bz2 +echo "9-----chmod 700-----end" +ls /dev/ > dev.txt +echo "10-----ls /dev-----end" +grep console dev.txt -n +echo "11-----grep-----end" \ No newline at end of file diff --git a/day1/d10.sh b/day1/d10.sh new file mode 100755 index 0000000..1e1f2c1 --- /dev/null +++ b/day1/d10.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo "your name is $USER" +echo "your home directory is $HOME" +echo "your shells path is $ShellsPath" \ No newline at end of file diff --git a/day1/d11.sh b/day1/d11.sh new file mode 100644 index 0000000..9bbec35 --- /dev/null +++ b/day1/d11.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# $# 用于获取参数的个数 +echo "$#" +# $* 用于获取所有参数 +echo "$*" +# $1 $2 $3 用于获取第 1 2 3 个参数 +echo "$1,$2,$3" + +# $? 用于获取上一条命令的返回值,0 为正确,非 0 为错误 +echo $? +rm a.txt +echo $? + +# $$ 用于获取当前脚本的进程号 +echo $$ +# $0 用于获取当前脚本的名称 +echo $0 + + + +# 测试使用 ./d11.sh 1 2 3 \ No newline at end of file diff --git a/day1/d12.sh b/day1/d12.sh new file mode 100644 index 0000000..c7d466a --- /dev/null +++ b/day1/d12.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# `` 使用示例 +echo "current datetime is `date +"%y年-%m月-%d日 %H:%M:%S"`" + +# 转义空格 +echo "current datetime is `date +%y年-%m月-%d日\ \ %H:%M:%S`" \ No newline at end of file diff --git a/day1/d13.sh b/day1/d13.sh new file mode 100644 index 0000000..5d90ece --- /dev/null +++ b/day1/d13.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +name=flykhan +echo "0 $name" +echo "main $$" + +# 使用圆括号创建子shell +# () 不影响 +(name=Lucy; echo "1 $name"; echo "() $$") +echo "0 $name" + +# 使用花括号进行命令扩展和分组,记得语句后的 ';' +{ name=Jack; echo "2 $name"; echo "{} $$"; } +echo "0 $name" + + + +# 在 Bash 脚本中,圆括号 `()` 和花括号 `{}` 有不同的用途和行为。 + +# 1. 圆括号 `()` +# - 用途: +# - 创建子shell:在圆括号中的命令序列将在一个子shell 中执行,子shell 是一个独立的进程,它会继承父shell 的环境变量和文件描述符,但是变量的修改在子shell 中不会影响到父shell。 +# - 数组赋值:在圆括号中可以定义数组,并将值赋给数组。 +# - 示例: +# ```bash +# (command1; command2; command3) +# ``` +# - 注意事项: +# - 在圆括号中的命令序列与外部环境是隔离的,所以在子shell 中定义的变量对于父shell 是不可见的。 + +# 2. 花括号 `{}` +# - 用途: +# - 命令扩展和分组:花括号可以用于扩展和分组命令,可以将多个命令组合在一起,作为一个整体进行处理。 +# - 文件名扩展:花括号可以用于生成文件名的列表,例如使用通配符进行文件名扩展。 +# - 示例: +# ```bash +# { command1; command2; } +# ``` +# - 注意事项: +# - 花括号中的命令序列会在当前shell 中执行,不会创建子shell。 +# - 花括号中的命令序列可以使用分号 `;` 或换行符分隔多个命令。 + +# 总结: +# - 圆括号 `()` 用于创建子shell,并在子shell 中执行命令序列。 +# - 花括号 `{}` 用于命令扩展和分组,将多个命令组合在一起进行处理。 \ No newline at end of file diff --git a/day1/d14.sh b/day1/d14.sh new file mode 100644 index 0000000..c2b8f4a --- /dev/null +++ b/day1/d14.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +echo "Using test -e:" +test -e "$1" +echo $? + +echo "Using [ -d ]:" +[ -d "$1" ] +echo $? + +echo "Using [ -x ]:" +[ -x "$1" ] +echo $? + +# 判断文件是否存在 +# test -e "$1" # 与下面等价 +# [ -e "$1" ] # 与上面等价 + +# 更多判断 +# -d 是目录 +# -f 是文件 +# -e 是存在 +# -r 是可读 +# -w 是可写 +# -x 是可执行 +# -s 是非空 +# -h 是软链接 +# -L 是软链接 +# -p 是管道 +# -S 是socket +# -b 是块设备 +# -c 是字符设备 +# -t 是终端 +# -g 是设置了组ID +# -u 是设置了用户ID +# -k 是设置了粘滞位 diff --git a/day1/d15.sh b/day1/d15.sh new file mode 100644 index 0000000..a7177c4 --- /dev/null +++ b/day1/d15.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# -r 防止特殊字符被转义,例如 \n +read -r pwd1 +read -r pwd2 + +echo "$pwd1 is zero string?" +[ -z "$pwd1" ] +echo $? + +[ "$pwd1" = "$pwd2" ] +if [ $? -eq 0 ] +then + echo "pwd1 is equal to pwd2" +else + echo "pwd1 is not equal to pwd2" +fi \ No newline at end of file diff --git a/day1/d16.sh b/day1/d16.sh new file mode 100644 index 0000000..c4de919 --- /dev/null +++ b/day1/d16.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# -eq 是等于 +test "$1" -eq "$2" +if [ $? -eq 0 ] +then + echo "$1 == $2" +else + echo "$1 != $2" +fi + +# -gt 是大于 +test "$1" -gt "$2" +if [ $? -eq 0 ] +then + echo "$1 > $2" +else + echo "$1 !> $2" +fi + +# -lt 是小于 +test "$1" -lt "$2" +if [ $? -eq 0 ] +then + echo "$1 < $2" +else + echo "$1 !< $2" +fi + +# -ge 是大于等于 +test "$1" -ge "$2" +if [ $? -eq 0 ] +then + echo "$1 >= $2" +else + echo "$1 !>= $2" +fi + +# -le 是小于等于 +test "$1" -le "$2" +if [ $? -eq 0 ] +then + echo "$1 <= $2" +else + echo "$1 !<= $2" +fi + +# -ne 是不等于 +test "$1" -ne "$2" +if [ $? -eq 0 ] +then + echo "$1 != $2" +else + echo "$1 == $2" +fi \ No newline at end of file diff --git a/day1/d17.sh b/day1/d17.sh new file mode 100644 index 0000000..3ff0193 --- /dev/null +++ b/day1/d17.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# 当 a.txt 存在时,执行 echo "good" >> a.txt,否则执行 echo "bad" >> a.txt +test -e a.txt && echo "good" >> a.txt || echo "bad" >> a.txt \ No newline at end of file diff --git a/day1/d18.sh b/day1/d18.sh new file mode 100644 index 0000000..2cbd4dc --- /dev/null +++ b/day1/d18.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +test -e $1 && test -f $1 && grep flykhan $1 -n || echo "file not exist or not a file" \ No newline at end of file diff --git a/day1/d19.sh b/day1/d19.sh new file mode 100644 index 0000000..e35e58c --- /dev/null +++ b/day1/d19.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +[ -e $1 ] && ls -l $1 || echo "file not exist" \ No newline at end of file diff --git a/day1/d2.sh b/day1/d2.sh new file mode 100644 index 0000000..f5804a6 --- /dev/null +++ b/day1/d2.sh @@ -0,0 +1,60 @@ +#!/bin/bash +flag=1 + +# -e 检测存在性 +if [ -e ~/qfedu-linux-advanced-level/day1/test2 ]; then + echo "the test dir is exist" +else + echo "the test dir is not exist" + + # 等同于 while[ $flag ] + # do 用于开始 while 循环 + while [ $flag -eq 1 ] + do + echo "press y to create the dir,press n to exit" + read yn + case $yn in + y) + echo "mkdir ~/qfedu-linux-advanced-level/day1/test2" + mkdir ~/qfedu-linux-advanced-level/day1/test2 + # flag = 0 用于结束 while 循环 + flag = 0 + # ;; 用于结束 case 语句,相当于 break + ;; + n) + echo "end operation" + # exit -1 用于退出脚本,-1 为错误码 + exit -1 + ;; + *) + echo "your input is error exit" + echo "please input again" + flag=1 + ;; + # esac 用于结束 case 语句 + esac + # done 用于结束 while 循环 + done +# fi 用于结束 if 语句 +fi + +cd ~/qfedu-linux-advanced-level/day1/test2 + +for((i=1 ; i<4 ; i++)) +do + echo "please input a string, we will create it as a dir" + read dir_name + if [ -e $dir_name ]; then + echo "$i please input other name" + else + echo "the $dir_name is created" + mkdir $dir_name + break + fi +done + +echo "your times is over" +if [ $i -eq 3 ]; then + echo "your times is over" + exit 0 +fi \ No newline at end of file diff --git a/day1/d20.sh b/day1/d20.sh new file mode 100644 index 0000000..b68ad7a --- /dev/null +++ b/day1/d20.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# 多重条件判定: +# 1. -a 是与 +# 2. -o 是或 +# 3. ! 是非 + +#!/bin/bash + +# 检查文件是否存在并且可读可写可执行 +if [ -e "$1" ] && [ -r "$1" -a -w "$1" -a -x "$1" ]; then + echo "file $1 exists and is readable, writable, and executable" +else + echo "file $1 does not exist or is not readable, writable, or executable" +fi diff --git a/day1/d3.sh b/day1/d3.sh new file mode 100755 index 0000000..db05371 --- /dev/null +++ b/day1/d3.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# 注释的部分 +echo "--t1.sh--" +ls ~/qfedu-linux-advanced-level/day1/cmd_test -l diff --git a/day1/d4.sh b/day1/d4.sh new file mode 100644 index 0000000..c176c4d --- /dev/null +++ b/day1/d4.sh @@ -0,0 +1,5 @@ +# no bash interpreter + +echo "--mkdir abc--" +mkdir abc +echo "--ok--" \ No newline at end of file diff --git a/day1/d5.sh b/day1/d5.sh new file mode 100644 index 0000000..1c61410 --- /dev/null +++ b/day1/d5.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +age=20 +name=flykhan +echo "my name is $name, and my age is $age" \ No newline at end of file diff --git a/day1/d6.sh b/day1/d6.sh new file mode 100644 index 0000000..9ddf1ff --- /dev/null +++ b/day1/d6.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# read 是读取, -p 是打印 +read -p "please input your name: " name +read -p "please input your age: " age +echo "my name is $name, and my age is $age" \ No newline at end of file diff --git a/day1/d7.sh b/day1/d7.sh new file mode 100644 index 0000000..77c3391 --- /dev/null +++ b/day1/d7.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +readonly PI=3.1415926 +echo PI=$PI + +x=1 +x=2 +echo $x +# 清除 x 变量之后,变量还可以再次读取,但是变量的内容为空 +unset x +echo $x + +r=2 +# 进行算术运算时,变量前面不需要加 $ 符号,且不支持浮点数运算 +# S=$((PI*r)) +S=$(echo "$PI * $r" | bc -l) +echo S=$S \ No newline at end of file diff --git a/day1/d8.sh b/day1/d8.sh new file mode 100644 index 0000000..a1ef80d --- /dev/null +++ b/day1/d8.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +msg="hi flykhan" +echo $msg +echo "$msg very good" +echo '$msg very good' +bye='bye bye' +echo $bye \ No newline at end of file diff --git a/day1/d9.sh b/day1/d9.sh new file mode 100644 index 0000000..e2207b9 --- /dev/null +++ b/day1/d9.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +echo "your name is $USER" +echo "your home directory is $HOME" \ No newline at end of file diff --git a/day1/svim.sh b/day1/svim.sh index 49765d7..38f3166 100755 --- a/day1/svim.sh +++ b/day1/svim.sh @@ -12,7 +12,12 @@ then echo "" >> $1 chmod +x $1 fi - vi +'set nonu' +'set fileencoding=utf-16' +2 $1 + vi +'set nu' +'set fileencoding=utf-8' +2 $1 else echo "Usage: svim filename" -fi \ No newline at end of file +fi + +# 使用 Vim 编辑器打开该文件,并进行以下设置: +# +'set nu':设置显示行号。 +# +'set fileencoding=utf-8':设置文件编码为 UTF-8。 +# +2:将光标定位到文件的第二行(在这里,$1 是传递的文件名参数)。 \ No newline at end of file