From b205c2c28f35f4379fbe49e4768963c6fb2f8e79 Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 14 Aug 2023 20:13:50 +0800 Subject: [PATCH] =?UTF-8?q?shell=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day1/homework/h1.sh | 6 ++++++ day1/homework/h2.sh | 11 +++++++++++ day1/homework/h3.sh | 13 +++++++++++++ day1/homework/h4.sh | 13 +++++++++++++ day1/homework/h5.sh | 14 ++++++++++++++ day1/homework/h6.sh | 11 +++++++++++ day1/homework/h7.sh | 14 ++++++++++++++ day1/homework/h8.sh | 24 ++++++++++++++++++++++++ 8 files changed, 106 insertions(+) create mode 100644 day1/homework/h1.sh create mode 100644 day1/homework/h2.sh create mode 100644 day1/homework/h3.sh create mode 100644 day1/homework/h4.sh create mode 100644 day1/homework/h5.sh create mode 100644 day1/homework/h6.sh create mode 100644 day1/homework/h7.sh create mode 100644 day1/homework/h8.sh diff --git a/day1/homework/h1.sh b/day1/homework/h1.sh new file mode 100644 index 0000000..2c29a38 --- /dev/null +++ b/day1/homework/h1.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# 编写一个Shell脚本,输出当前日期和时间。 +# 【要求】按 “xxxx年xx月xx日 xx时xx分xx秒” 格式输出 + +echo `date +"%Y年%m月%d日 %H时%M分%S秒"` \ No newline at end of file diff --git a/day1/homework/h2.sh b/day1/homework/h2.sh new file mode 100644 index 0000000..29069a1 --- /dev/null +++ b/day1/homework/h2.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# 编写一个Shell脚本,计算1到100之间所有3倍数的整数和svi + +declare -i svi=0 +for (( i=1;i<=100;i++ )); do + if (( i%3==0 )); then + svi+=i + fi +done +echo "svi="$svi \ No newline at end of file diff --git a/day1/homework/h3.sh b/day1/homework/h3.sh new file mode 100644 index 0000000..041c807 --- /dev/null +++ b/day1/homework/h3.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# 编写一个Shell脚本,接受一个文件名作为参数,并统计该文件中的行数、单词数和字符数 +# 【提示】wc 统计命令 + +if [ -e $1 ] && [ -f $1 ]; then + echo "文件名:$1" + echo "行数:`wc -l $1 | awk '{print $1}'`" + echo "单词数:`wc -w $1 | awk '{print $1}'`" + echo "字符数:`wc -c $1 | awk '{print $1}'`" +else + echo "文件不存在或者不是一个文件" +fi \ No newline at end of file diff --git a/day1/homework/h4.sh b/day1/homework/h4.sh new file mode 100644 index 0000000..e92dd0b --- /dev/null +++ b/day1/homework/h4.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# 编写一个Shell脚本,接受一个目录名作为参数,并列出该目录下的所有文件和子目录,并人性化显示文件的大小 + +if [ -d $1 ]; then + echo "目录名:$1" + echo "文件列表:" + # 也可以直接写成 ls -lh $1 + output=`ls -lh $1` + echo "$output" +else + echo "目录不存在" +fi \ No newline at end of file diff --git a/day1/homework/h5.sh b/day1/homework/h5.sh new file mode 100644 index 0000000..cc622bd --- /dev/null +++ b/day1/homework/h5.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# 编写一个Shell脚本,接受一个数字作为参数,并判断该数字是奇数还是偶数。 + +if [ $# -ne 1 ]; then + echo "参数个数不正确" + exit 1 +else + if (( $1%2==0)); then + echo "$1 是偶数" + else + echo "$1 是奇数" + fi +fi \ No newline at end of file diff --git a/day1/homework/h6.sh b/day1/homework/h6.sh new file mode 100644 index 0000000..928ed22 --- /dev/null +++ b/day1/homework/h6.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# 编写一个Shell脚本,接受一个字符串作为参数,并将该字符串逆序输出。 +# 【提示】 echo "abc"|rev + +if [ $# -ne 1 ]; then + echo "请提供一个字符串作为参数" + exit 1 +fi + +echo "$1"|rev diff --git a/day1/homework/h7.sh b/day1/homework/h7.sh new file mode 100644 index 0000000..fbeaeb0 --- /dev/null +++ b/day1/homework/h7.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# 编写一个Shell脚本,接受一个文件路径作为参数,并检查该文件是否存在。如果存在,则输出 "文件名和文件大小",否则输出 "File does not exist." + +# 方法一 +if [ -e $1 ]; then + output=`ls -lh $1 | awk '{print $5}'` + echo "文件名: $1, 文件大小: $output" +else + echo "File does not exist." +fi + +# 方法二 +[ -e $1 ] && echo "文件名: $1, 文件大小: `ls -lh $1 | awk '{print $5}'`" || echo "File does not exist." \ No newline at end of file diff --git a/day1/homework/h8.sh b/day1/homework/h8.sh new file mode 100644 index 0000000..abe7bba --- /dev/null +++ b/day1/homework/h8.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# 编写一个Shell脚本,接受一个目录名作为参数,将目录中所有文件打包成xx.tar.bz2到它的父级目录,然后再删除该目录及其所有子目录和文件。 + +if [ $# -ne 1 ]; then + echo "请提供目录参数" + exit 1 +fi + +dir=$1 +# 获取父级目录 +father_dir=`dirname $dir` +# 获取目录名 +dir_name=`basename $dir` +# 打包目录中所有文件 +tar -jcvf $father_dir/$dir_name.tar.bz2 $dir +# 判断是否打包成功 +if [ $? -eq 0 ];then + # 删除目录 + rm -rf $dir + echo "已删除目录 $dir" +else + echo "打包失败" +fi