第一节课:课程概览与shell

课程概览与shell

第一节课

(一)课程概览与shell

1、windows和linux的shell的不同

#windows:PowerShell
#linux:Bourne Again SHell(Bash)

2、常见的命令、操作以及概念理解

(1)date
date #打印日期
(2)echo
echo xx #打印你想要的文本 
echo Hello
echo World
echo "Hello World" #注意""与''的不同
echo $PATH #打印路径
which echo #打印echo程序的路径?
(3)windows、linux、macOS的路径的不同

windows路径通常是反斜杠“\”表示,linux和macOS路径通常是“/”开头(绝对路径)

(4)pwd
pwd #打印当前工作目录
(5)cd
cd #更改当前工作目录的命令,"."表示当前目录,".."表示父目录,更方便切换目录的方式
cd .. #假设本来在/home,现在在/
cd ./home #进入当前目录的home目录,再输入一遍会显示no such file因为当前目录没有home目录
cd /usr/local/bin #这是绝对路径
../../../bin/echo world #通过构建路径来遍历文件系统
(6)ls
ls #列出当前文件目录中的所有文件
ls ..#或者ls某一path,列出相应路径下的文件
cd ~ #回到主目录(~总是可以扩展到主目录,主目录是唯一确定的)
cd - #回到上一个文件目录
--help #打印出命令的帮助信息,Usage:ls[option]...[file]...  其中...表示零个、一个或多个选项,[]表示可选项。不带值的称为标志 -a --all,带值的称为选项 -C --color 为啥为啥???
ls -l #打印文件信息(以下三组不是很理解)
#第一组:文件的所有者设置的权限
#第二组:拥有该文件的组设置的权限
#第三组:其他人的权限列表
(7)文件操作
mv 1 6 #mv 旧路径 目标路径 :重命名文件、移动文件(重命名文件1为文件6)
cp -r 2 8 #cp 旧路径 新路径 :复制文件 (by default不能复制目录,复制目录需要使用-r
rm -r 7 #rm 路径 删除某一文件(by default不能删除目录,删除目录需要使用-r)
(8)目录操作
rmdir 2 #rmdir 路径 删除空目录 
mkdir 2#mkdir 名字 创建目录 
(9)文件流
man ls #man 查看手册  按q退出
#CTRL+L 清除终端回到顶部
#两种流:输入流、输出流 输入和输出的重定向不是程序所知道的
 < file #表示将这个程序的输入重定向为这个文件的内容
 \> file #表示将前面程序的输出重定向到这个文件中 echo hello > hello.txt (新创建hello.txt)
cat #输出文件内容 cat hello.txt
cat < hello.txt #从hello.txt读取输入,然后打印输出
cat < hello.txt > hello2.txt# 从hello.txt读取输入,然后将输出重定向到hello2.txt(相当于cy)
cat <hello.txt >>hello2.txt #>>将hello.txt的内容追加到hello2.txt中,表示追加而不是覆盖 
  |    #将左边程序的输出作为右边程序的输入(管道链接)
 tail -nl (--line=l) #输出程序的最后l行,l表示数字
 e.g.ls -l / tail -n1(--line=1)
 e.g.ls -l / | tail -n5 > hello.txt
(10)root用户(super!)
#root用户,类似于windows的管理员用户,用户ID是0,可以在系统中做任何事情
do as su su  #运行sudo程序,指超级用户(Super User)
#什么情况下需要使用:一个叫做sysfs的文件系统,输入/sys,里面不是计算机上的文件,而是各种内核参数。(这话不知道在说啥)


#e.g.例子一(前两个怎么/的位置都不同,好像是一样的 不一样 为啥为啥???因为一个是绝对路径一个是相对路径,傻)
cd /sys #绝对路径这样写
cd class/ #相对路径这样写(或者直接写cd class)
cd backlight/
cd intel_backlight/
cat brightness (1060)
echo 500 > brightness #(Permission denied,因为输入和输出的重定向不是程序所知道的)
sudo echo 500 > brightness #(Permission denied)
#方法一:奇怪这里怎么显示的是注释?
#^Ccho 1 > /sys/net/ipv4_forward
#方法二:
sudo su #以超级用户身份获取一个shell
exit
#方法三:
echo 1060 | sudo tee brightness #将echo的输出发送到sudo tee中,tee命令会将输入内容写入一个文件,同时也将它输出到标准输出 (比如想保存并输出文件的内容)


# e.g.例子二 让scrolllock(/sys/class/leds)亮灯
cd input\:\:scrolllock/
cat brightness
echo 1 | sudo tee brightness 

(二)家庭作业 Exercises

All classes in this course are accompanied by a series of exercises. Some give you a specific task to do, while others are open-ended, like “try using X and Y programs”. We highly encourage you to try them out.

We have not written solutions for the exercises. If you are stuck on anything in particular, feel free to send us an email describing what you’ve tried so far, and we will try to help you out.

For this course, you need to be using a Unix shell like Bash or ZSH. If you are on Linux or macOS, you don’t have to do anything special. If you are on Windows, you need to make sure you are not running cmd.exe or PowerShell; you can use Windows Subsystem for Linux or a Linux virtual machine to use Unix-style command-line tools. To make sure you’re running an appropriate shell, you can try the command echo $SHELL. If it says something like /bin/bash or /usr/bin/zsh, that means you’re running the right program.

1、Create a new directory called missing under /tmp.

#在tmp中创建missing目录
mkdir tmp
cd tmp
mkdir missing

2、Look up the touch program. The man program is your friend.

#查看touch:man touch,它是你的好朋友
#我不承认 字跟蚊子一样小
man touch

3、Use touch to create a new file called semester in missing.

#使用touch创建semester,需要注意的是touch只能用于创建文件,不能创建目录
cd missing
touch semester

4、Write the following into that file, one line at a time:

#!/bin/sh
curl --head --silent https://missing.csail.mit.edu

The first line might be tricky to get working. It’s helpful to know that # starts a comment in Bash, and ! has a special meaning even within double-quoted (") strings. Bash treats single-quoted strings (') differently: they will do the trick in this case. See the Bash quoting manual page for more information.

#向semester文件中写入以下信息,要使用单引号。
#需要注意的是,要知道在bash中以#开头以及在双引号中的!有着不同的含义。Bash对待单引号有所不同,这在这种情况下会发回作用
echo '#!/bin/sh'>semester
echo "curl --head --silent https://missing.csail.mit.edu">>semester

5、Try to execute the file, i.e. type the path to the script (./semester) into your shell and press enter. Understand why it doesn’t work by consulting the output of ls (hint: look at the permission bits of the file).

./semester
#输入./semester为什么无法执行,输入ls -l了解,因为没有执行权限(excutive?)

6、Run the command by explicitly starting the sh interpreter, and giving it the file semester as the first argument, i.e. sh semester. Why does this work, while ./semester didn’t?

#输入sh semester能够执行,为什么sh semester能够执行,第五题./semester不能够(中间自己下载了curl)
sh semester
# I don't know why,perheps sh give the file x mode.X属性大爆发

7、Look up the chmod program (e.g. use man chmod).

#查看chmod命令:man chmod
man chmod

8、Use chmod to make it possible to run the command ./semester rather than having to type sh semester. How does your shell know that the file is supposed to be interpreted using sh? See this page on the shebang line for more information.

#通过使用chmod命令使得./semester能够执行:改变文件权限
chmod -+x semester
ls -l semester #ls看看是否真的加了执行权限

9、Use | and > to write the “last modified” date output by semester into a file called last-modified.txt in your home directory.

#将semester执行后last modified后的信息写进last-modified.txt:需要使用grep和cut命令
./semester #先执行semester文件
grep "last modified" || cut -n14 > /home/last-modified.txt #这里我有点不太确定

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.

#显示电量(find命令)
#我天这里我也忘了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值