Q2.2 编写并执行脚本

问题:编写两个bash函数marco和polo执行下面的操作:每当你执行marco时,将当前的工作目录以某种形式保存;当你执行polo时,无论当前处于什么目录下,都切换到执行marco的目录(即在任意目录下执行polo即切换到marco保存的目录)。

Step 1 编写脚本marco.sh

保存形式一:将当前目录保存至文件

#!/bin/bash
marco(){echo "$(pwd)">$HOME/marco_history.logecho "save pwd $(pwd)"
}
polo(){cd "$(cat "$HOME/marco_history.log")"
}

保存形式二:将当前目录保存至临时环境变量

marco(){export MARCO=$(pwd)echo "save pwd $(pwd)
}
polo(){cd "$MARCO"
}

tip:Shell中我们使用$(CMD)获得命令的输出

Step 2 执行脚本

~/missing % source marco.sh # 通过source加载函数
~/missing % marco
save pwd /Users/Owner/missing
~/missing % cd
~ % polo
~/missing %

参考资料:MIT-Missing-Semester > Shell Scripting > Exercises 2

Q2.3 编写一个脚本执行另一个脚本

问题:假设你有一个命令,它很少出错。为了调试它,你需要花费大量时间重现错误并捕获它的输出。编写一个脚本,运行如下脚本直到它出错,将标准输出和标准错误流记录到文件,并在最后输出所有内容。加分点:报告脚本出错前运行了多少次。

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

tips:
$(())
RANDOM % 100

if [[]]; then
fi

Step1 编写脚本

使用while循环

count=1while true
do./buggy.sh 2>out.logif [[ $? -ne 0 ]]; thenecho "failed after $count times"cat out.logbreakfi((count++))done

使用for循环

for ((count=1;;count++))do./buggy.sh 2> out.logif [[ $? -ne 0 ]]; thenecho "failed after $count times"cat out.logbreakecho "$count try"fidone

使用until循环

#!/usr/bin/env bashcount=0until [[ "$?" -ne 0 ]];docount=$((count+1))./random.sh 2> out.txtdoneecho "found error after $count runs"cat out.txt

Step2 执行脚本

~/missing % vim
~/missing % ./debug_until.sh
zsh: permission denied: ./debug_until.sh
~/missing % chmod 777 debug_until.sh
~/missing % ./debug_until.sh
found error after 1 runs
./debug_until.sh: line 6: ./random.sh: Permission denied
~/missing % chmod 777 random.sh
~/missing % ./debug_until.sh
Everything went according to plan
Everything went according to plan
Something went wrong
found error after 2 runs
The error was using magic numbers
~/missing %

参考资料:MIT-Missing-Semester > Shell Scripting > Exercises 3

参考材料链接:MIT-Missing-Semester > Shell Scripting

Q2.4 查找文件并压缩

Q: 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).

Step 1 创建所需文件

mkdir html_root
cd html_root
touch {1..10}.html
mkdir html
cd html
touch xxxx.html

Step 2 执行find命令

#for MacOS
find html_root -name "*.html" -print0 | xargs -0 tar vcf
#for Linux
find . -type f -name "*.html" | xargs -d '\n' tar -cvzf

tips
find’s -exec can be very powerful for performing operations over the files we are searching for. However, if we want to do something with all the files, like creating a zip file.
As we have seen so far commands will take inut 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 argument. For exemple ls | xargs rm will delete the files in the current directory.

xargs extended arguments- construst argument list(s) and execute utility
options/flags
for MacOS -0 Change xarges to expext NUL (\0) characters as separators, instead of spaces and newlines.This is expected to be used in concert with the -print function in find.
for Linux -d delim分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符

find
PRIMARIES
-print0 This primary alwaysevaluates to true. It prints the pathname of the current file to standard output, followed by an ASCII NUL character (character code 0).

tar - manipulate tape archives 用于备份文件
-c Create a new archive containing the sprecified items. The long option form is –create
-t, --list List archive contents to stdout.
-x, --extract Extract to disk from the archive.
-v, --verbose Produce vebose output.
-z, --gunzip, --gzip Compress the resulting archive with gzip.
-f <file>, --file <file> Read the archive from or write the archive to the specified file.

# Creat a gzipped archive:
tar czvf target.tar.gz file1 file2 file3
# Extract a gzipped archive in the current directory:
tar xzvf source.tar.gz
# List the contents of a tar file:
tar tzvf source.tar

Q2.5

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 . -type f -mmin -60 -print0 | xarges -0 ls -lt | head -10

tips
find -mmin [-|+]n True if the difference between this file last modification time and the time find was started, round up to the next full minute, is more than n (+n), less than n (-n), or exactly n minutes ago.
head- display first lines of a file
-n, --lines = count Print count lines of each of the specified files.

[Exercises] MIT Missing Semester相关推荐

  1. MIT 计算机操作环境导论Missing Semester Lesson 9 安全和密码学

    去年的这节课我们从计算机 用户 的角度探讨了增强隐私保护和安全的方法. 今年我们将关注比如散列函数.密钥生成函数.对称/非对称密码体系这些安全和密码学的概念是如何应用于前几节课所学到的工具(Git和S ...

  2. MIT 计算机操作环境导论Missing Semester Lesson 6 Git 版本控制(Git)

    版本控制系统 (VCSs) 是一类用于追踪源代码(或其他文件.文件夹)改动的工具.顾名思义,这些工具可以帮助我们管理代码的修改历史:不仅如此,它还可以让协作编码变得更方便.VCS通过一系列的快照将某个 ...

  3. The missing semester of your CS education--调试及性能分析

    课程结构 01.课程概览与 shell 02.Shell 工具和脚本 03.编辑器 (Vim) 04.数据整理 05.命令行环境 06.版本控制(Git) 07.调试及性能分析 08.元编程 09.安 ...

  4. The missing semester of your CS education--命令行环境

    课程结构 01.课程概览与 shell 02.Shell 工具和脚本 03.编辑器 (Vim) 04.数据整理 05.命令行环境 06.版本控制(Git) 07.调试及性能分析 08.元编程 09.安 ...

  5. 计算机教育中缺失的一课 · the missing semester of your cs education

    小编作为一个程序猿圈子的过来猿,一直觉得现在很多大学里的计算机课程往往只专注于传授学生关于从操作系统到机器学习这些学院派的课程或主题,而在一些工具的运用及精通方面,往往会留给学生自行摸索. 其实学编程 ...

  6. The Missing Semester

    计算机教育中缺失的一课 开设此课程的动机 不再总是从文档中拷贝粘贴 命令.不要再"逐个执行这 15 个命令",不要再"你忘了执行这个命令"."你忘了传 ...

  7. 计算机科学基础知识第一节讲义 The Missing Semester of Your CS

    这节课的链接课程链接 运行环境 老师的演示电脑采用Linux系统的终端Terminal,可以在Windows电脑上安装WSL实现. echo命令在powershell中也能运行,但是运行echo he ...

  8. 学习The Missing Semester of Your CS Education (第一课)

    第一堂课: 很多我已经很熟悉的命令 新命令: 1.date 查看日期 2.cut --delimiter=' ' -f2 把输入按照空格截断,打印出第二个token 3./sys/ 目录下有许多新奇好 ...

  9. MIT缺失的一课——Lecture1:Shell

    学习的动机 As computer scientists, we know that computers are great at aiding in repetitive tasks. Howeve ...

最新文章

  1. GitHub 的微服务架构设计与实践
  2. 零基础学python爬虫-我是如何零基础开始能写Python爬虫的
  3. 图像的Gamma(伽玛)校正的原理及OpenCV代码实现
  4. mysql 无论输入什么都是现实 not found_NotAPanda
  5. Apache RocketMQ 正式开源分布式事务消息
  6. Ajax/JavaScript脚本大全,JS脚本大全
  7. mysql 添加表索引_如何向MySQL表中添加索引?
  8. stringWithUTF8String return null (返回null)的解决办法
  9. Hadoop单机配置
  10. JTAG,PLL ,ICE
  11. MySQL的性能优化理论
  12. Python:snownlp中文文本情感分析
  13. 大盘是超跌反弹还是彻底反转?
  14. Springboot毕设项目老年健康数据管理及分析平台t46d0(java+VUE+Mybatis+Maven+Mysql)
  15. SparkSQL之“Dataset和Dataframe
  16. 十级龙王间的决斗(C++)
  17. 旧版MAC Air WIN7安装
  18. mysql 设置字符集
  19. 不搞笑不给力——年会小品《山寨新闻联播》
  20. 星级推荐,列举一下2018年购入的书籍

热门文章

  1. java shell spool_批量快速的导入导出Oracle的数据(spool缓冲池、java实现)
  2. 为何有些软件需要安装,而有些则是免安装的?
  3. [武侠小说]《血鹦鹉》摘录(古龙作品)
  4. DCDC电路电感的选择(转)
  5. 【一张图搞定关机程序】让你的代码有趣起来!送兄弟送闺蜜,快乐原来如此简单!(赋全过程和结果,超详细解说)
  6. oracle---常用函数4---日期函数
  7. 针对媒体和娱乐行业的NVIDIA MAXIMUS
  8. 现在有哪些网页防篡改的技术?
  9. 移动平台的产品设计世界
  10. mybatis-plus联合主键的使用