Introduction
这部分是《操作系统进阶》课上的第一个project的project_a部分的内容,主要是关于shell的几个命令的应用。以下是我的解答
info.sh
#!/bin/bash
mkdir foo #创建目录
cd foo #进入目录下
touch name.txt #创建文件
vim name.txt
echo "Ouyangxue">>name.txt #向文件写入
touch stno.txt
vim stno.txt
echo "10205102488">>stno.txt
cp -i name.txt name2.txt #复制文件
cp -i stno.txt stno2.txt
myls.sh
#!/bin/bash
ls -al --color=always -t -h
解释: ls命令:全称list,显示文件的文件名与相关属性。(目前工作目录所含之文件及子目录)
find.sh
#! /bin/sh
touch output.txt
ls -alth /bin/b* | tail -n +2 | awk '{print $9" "$3" "$1}' | cut -d / -f 3- | sort > output.txt
chmod o=r output.txt
其中,tail, 用于输入文件中的尾部内容 ,tail -n +2, 从文件的开头第N项内容 开始显示。
awk可以用来文本格式化:
awk [option] 'pattern[action]' file ...
awk 参数 条件动作 文件
这里是格式化输出第9列第3列和第1列的内容。
cut, 从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出 ,cut -d / -f 3- ,把文件以 / 分隔的第3列内容剪切下来。
选项与参数:
-d:分隔符,按照指定分隔符分割列。与 -f 一起使用
-f:依据 -d 的分隔字符将一段信息分割成为数段,用 -f 取出第几段的意思(列号,提取第几列)
-c:以字符 (characters) 的单位取出固定字符区间
-b:以字节为单位进行分割
dbg.sh
#!/bin/bash
touch output.txt
./worker.sh
while(("$?" == 0))
do
echo $? >> output.txt
./worker.sh
done
$? : 显示最后命令的退出状态,0表示没有错误,其他表示有错误
这里要注意的是循环内两条指令的位置。因为$? 是上一条指令执行后的返回值,在循环内就是最后一条。