title: Missing Semester (2020) Lecture 笔记
date: 2020-03-07 17:15:52
categories:

  • 笔记

Missing Semester (2020) Lecture 笔记

前言:此系列为对应课程习题的笔记。

课程链接

Lecture 1: Course overview + the shell

Exercises选析

10. Write a command that reads out your laptop battery’s power level or your desktop machine’s CPU temperature from /sys. Note: if you’re a macOS user, your OS doesn’t have sysfs, so you can skip this exercise.

我使用的环境是WSL+VSCode,与真实系统环境有所区别,不过还是能找到一些硬件信息,例如💻🔋。

Lecture 2: Shell Tools and Scripting

Exercises选析

2. Write bash functions marco and polo that do the following. Whenever you execute marco the current working directory should be saved in some manner, then when you execute polo, no matter what directory you are in, polo should cd you back to the directory where you executed marco. For ease of debugging you can write the code in a file marco.sh and (re)load the definitions to your shell by executing source marco.sh.

注意写function,否则无法通过source装载到shell中执行。

3. Say you have a command that fails rarely. In order to debug it you need to capture its output but it can be time consuming to get a failure run. Write a bash script that runs the following script until it fails and captures its standard output and error streams to files and prints everything at the end. Bonus points if you can also report how many runs it took for the script to fail.

 #!/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"
cnt=0
while true
do
    let cnt=$cnt+1
    bash target.sh &> out.txt
    if [[ $? -ne  0 ]];then
        cat out.txt >> print.txt
        echo $cnt >>print.txt
        exit 0
    fi
done

4. As we covered in lecture find’s -exec can be very powerful for performing operations over the files we are searching for. However, what if we want to do something with all the files, like creating a zip file? As you have seen so far commands will take input from both arguments and STDIN. When piping commands, we are connecting STDOUT to STDIN, but some commands like tar take inputs from arguments. To bridge this disconnect there’s the xargs command which will execute a command using STDIN as arguments. For example ls | xargs rm will delete the files in the current directory.

Your task is to write a command that recursively finds all HTML files in the folder and makes a zip with them. Note that your command should work even if the files have spaces (hint: check -d flag for xargs)

find test -name "*.html" | xargs -d '\n' zip target.zip

注意通过'\n'来分割参数,使文件名含有空格的文件能够进行压缩。

5. (Advanced) Write a command or script to recursively find the most recently modified file in a directory. More generally, can you list all files by recency?

find test -type f | xargs -d '\n'  ls -l -t

通过find完成递归查找文件,这里只查找普通文件,然后传入ls按照时间顺序排列。

Lecture 3: Editors (Vim)

我暂时并无深入学习掌握Vim的想法。

Lecture 4: Data Wrangling

Exercises选析

2. Find the number of words (in /usr/share/dict/words) that contain at least three as and don’t have a 's ending. What are the three most common last two letters of those words? sed’s y command, or the tr program, may help you with case insensitivity. How many of those two-letter combinations are there? And for a challenge: which combinations do not occur?

cat /usr/share/dict/words | grep -v "'s$" | grep "^.*[aA].*[aA].*[aA].*$" | sed -E 's/.*(..)$/\1/' | sort | uniq -c 

通过两步grep筛选出符合要求的单词,然后sed提取末尾组成即可。

如果要找出哪些组合没有出现,可以先把上述出现的组合保存到一个文件中,然后再写一个脚本循环查找每种组合是否在该文件中出现,根据$?来判断即可。

4. Find your average, median, and max system boot time over the last ten boots. Use journalctl on Linux and log show on macOS, and look for log timestamps near the beginning and end of each boot.

cat startup.txt | grep "kernel" | sed -E 's/.*=(.*)s\./\1/' | R --slave -e 'x <- scan(file="stdin", quiet=TRUE); summary(x)'

通过ssh筛选记录后下载到本机,然后提取时间分析即可。

5. Look for boot messages that are not shared between your past three reboots (see journalctl’s -b flag). Break this task down into multiple steps. First, find a way to get just the logs from the past three boots. There may be an applicable flag on the tool you use to extract the boot logs, or you can use sed '0,/STRING/d' to remove all lines previous to one that matches STRING. Next, remove any parts of the line that always varies (like the timestamp). Then, de-duplicate the input lines and keep a count of each one (uniq is your friend). And finally, eliminate any line whose count is 3 (since it was shared among all the boots).

cat boot.txt | sed -E 's/^\w+\b \w+\b [0-9:]{8,8} //'| sed -E 's/\[[0-9]+\]//' | sort | uniq -c | awk '$1 < 3  { print $0 }' | wc -l

通过journalctl-b标记分别导出最后三次启动日志,合并为一个文件后去掉变量部分(这里只简单去掉了时间戳和PID)然后统计即可。

Lecture 5: Command-line Environment

Exercises选析

Job control

Terminal multiplexer

在这一部分可以体验tmux的使用。

Aliases

Dotfiles

在这一部分我使用了Dotbot来管理我的dotfiles,已经上传到dotfiles仓库

Remote Machines

在这一部分除了正常的密钥登陆服务器外,最有趣的是练习本地端口转发。通过本机浏览器来访问远程服务器文件,值得一试。

Lecture 6: Version Control (Git)

在这一节内容里可以补充对于Git的了解,例如其数据模型,练习中也指出了一些可能面临的应用场景。

Lecture 7: Debugging and Profiling

走马观花,并未深入了解。

Lecture 8: Metaprogramming

第一次了解到GitHub Action的存在,这一节的内容偏向于软件工程的实践方面。

Lecture 9: Security and Cryptography

关于安全与密码学的科普内容,可以了解熵、hash、KDF、对称加密与非对称加密的一些基本概念与现实应用领域。

Lecture 10: Potpourri

这一节介绍了一些完全独立的话题,例如DaemonsBackupsBooting + Live USBs

Lecture 11: Q&A

答疑啦。

Docker和虚拟机之间有什么区别?

后记:Lecture(2020)的视频笔记都看完啦,时间原因部分内容仅作了解。