The Missing Semester -02- shell和脚本-习题记录

习题解答

参考的博主记录:
https://www.cnblogs.com/grapefruit-cat/p/17068443.html

  1. 阅读 man ls ,然后使用 ls 命令进行如下操作:

    • 所有文件(包括隐藏文件)assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626170215988.png ls -a
    • 文件打印以人类可以理解的格式输出 (例如,使用 454M 而不是 454279954)assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626170259838.png ls -l -h
    • 文件以最近访问顺序排序assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626170358195.png ls -l -t
    • 以彩色文本显示输出结果assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626170445247.png ls -l -color=auto
      典型输出如下:
     -rw-r--r--   1 user group 1.1M Jan 14 09:53 baz
     drwxr-xr-x   5 user group  160 Jan 14 09:53 .
     -rw-r--r--   1 user group  514 Jan 14 06:42 bar
     -rw-r--r--   1 user group 106M Jan 13 12:12 foo
     drwx------+ 47 user group 1.5K Jan 12 18:08 ..
    

    实际输出如下:
    assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626171004320.png

  2. 编写两个 bash 函数 marco 和 polo 执行下面的操作。 每当你执行 marco 时,当前的工作目录应当以某种形式保存,当执行 polo 时,无论现在处在什么目录下,都应当 cd 回到当时执行 marco 的目录。 为了方便 debug,你可以把代码写在单独的文件 marco.sh 中,并通过 source marco.sh 命令,(重新)加载函数。
    两种写法:
    法一:使用echo

     #!/bin/bash                                                                 
     macro (){
     echo "$(pwd)" > $HOME/pwd.txt
     echo save successed!"$(pwd)"
     }
     polo(){
     cd "$(cat "$HOME/pwd.txt")"
     }
    

    assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626172413761.png运行过程:assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626172734847.png
    法二:使用export输出脚本变量:

    #!/bin/bash                                                                 
    macro1(){
        export MARCO=$(pwd)
    }
    polo1(){
        cd "$MARCO"
    }
    

    assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626173341969.png
    运行过程:assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626173319584.png

  3. 假设您有一个命令,它很少出错。因此为了在出错时能够对其进行调试,需要花费大量的时间重现错误并捕获输出。 编写一段 bash 脚本,运行如下的脚本直到它出错,将它的标准输出和标准错误流记录到文件,并在最后输出所有内容。 加分项:报告脚本在失败前共运行了多少次:fail_times.sh

     #!/usr/bin/env bash
    
     n=$(( RANDOM % 100 ))
    
     if [[ n -eq 42 ]]; then
        echo "Something went wrong"
        >&2 echo "The error was using magic numbers"
        exit 1
     fi
    
     echo "Everything went according to plan"
    

    使用while循环:

      1 #!/usr/bin/env bash                                                       
      2 count=0
      3 echo "Starting count fail_times now is $count"
      4 while true
      5   do ./fail_times.sh &>output.txt
      6     if [[ $? -ne 0 ]]; then
      7       cat output.txt
      8       echo "failed after $count times"
      9       break
     10     fi
     11     ((count++))
     12 
     13 done
    
    

    使用for循环:

      1 #! /usr/bin/bash                                                          
      2  echo > output_for.txt
      3   for (( count=0;;count++ ))
      4 do
      5   ./fail_times.sh &>> output_for.txt
      6   if [[ $? -ne 0 ]]; then
      7     echo "failed after $count times"
      8     break
      9   fi
     10 done
    
    

    踩了几个坑:
    第一个变量输入是:$count, 不是$(count),后者会被视为一个变量,具体报错如下:assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626181515188.png 第二个是if的[[ ]]里面都要有空格,不然认不出来里面的内容
    运行过程:
    对于新建的脚本要赋予使用权限
    while运行:assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626181040868.png for运行过程:assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626182400315.png

  4. 本节课我们讲解的 find 命令中的 -exec 参数非常强大,它可以对我们查找的文件进行操作。但是,如果我们要对所有文件进行操作呢?例如创建一个 zip 压缩文件?我们已经知道,命令行可以从参数或标准输入接受输入。在用管道连接命令时,我们将标准输出和标准输入连接起来,但是有些命令,例如 tar 则需要从参数接受输入。这里我们可以使用 xargs 命令,它可以使用标准输入中的内容作为参数。 例如 ls | xargs rm 会删除当前目录中的所有文件。

    您的任务是编写一个命令,它可以递归地查找文件夹中所有的 HTML 文件,并将它们压缩成 zip 文件。注意,即使文件名中包含空格,您的命令也应该能够正确执行(提示:查看 xargs 的参数 -d,译注:MacOS 上的 xargs 没有 -d查看这个 issue

    如果您使用的是 MacOS,请注意默认的 BSD find 与 GNU coreutils 中的是不一样的。你可以为 find 添加 -print0 选项,并为 xargs 添加 -0 选项。作为 Mac 用户,您需要注意 mac 系统自带的命令行工具和 GNU 中对应的工具是有区别的;如果你想使用 GNU 版本的工具,也可以使用 brew 来安装

    解题方法:
    首先建立文件列表,里面子文件夹有html,文件也有html
    然后输入命令:

    find question4 -name "*.html" -print0 |xargs -0 tar vcf html.zip
    
    

    意思:找到question4文件夹下面所有带有.html后缀的文件

    • -print0 会让 findnull 字符 (\0) 分隔每个文件名
    • -0 告诉 xargs 按 null 分隔,能处理含空格的文件名
    • xargs -0xargs 命令的一个选项,它的意思是:使用 null 字符 (\0) 作为输入分隔符,而不是默认的空格或换行。
    • 后面的tar vcf 是打包创建压缩包的意思
      1.(进阶)编写一个命令或脚本递归的查找文件夹中最近使用的文件。更通用的做法,你可以按照最近的使用时间列出文件吗?
find . -type f -print0 | xargs -0 ls -lt | head -1

assets/The Missing Semester -02- shell和脚本-习题记录/file-20250626211919155.png

head -1 是-mmin的意思,就是展示的最小文件数目

posted @ 2025-06-26 21:30  艳爚邀灼  阅读(25)  评论(0)    收藏  举报