【MIT Missing semester-1】Linux Shell

主要来自MIT的missing semester,课程主要想讲述各种linux shell的有意思的用法和工具,让大家需要的时候能够想起来:嗯,是有这么个工具来着…
非常推荐!b站可看link
建议安装oh-my-zsh,大幅提高效率;
windows10可以安装linux子系统 (WSL),不需要安装双系统也可以用linux shell

Lecture 1 the shell

shell 是啥

textual interface,可以运行程序,给定输入返回输出等。
bash就是非常常见的一种shell
需要用终端terminal来打开shell(简单来说就是那个小黑窗命令行)
在这里插入图片描述
通常会包含哪个用户,在哪个机器上,以及当前的路径是哪里
普通用户的符号是$ root是#
此时就可以运行各种命令
shell中空格不能随便用,就是以一个空格来进行split 认为第一个word是运行的程序,后面跟着的是参数。
shell其实类似一个programming environment,有自己的变量、条件、循环函数等,$PATH是环境变量的路径
比如

  • date 显示时间
  • cat xxx 打印某个文件的内容 可以用\对空格转义
    • 比如 touch test file会新建两个文件分别是test和file,而touch 'test file'touch test\ file可以新建一个叫“test file”的单个文件
    • cat test file会报错找不到文件,得用cat test\ file
  • echo xxxx 打印出xxxx,可以用空格进行转移
    • echo helloecho "hello"结果一样
    • echo $PATH
  • man xx查看某个命令的文档

shell会去$PATH中查找date\echo等程序,找到了就会运行。可以用which来查看程序在哪
在这里插入图片描述
另外,ctrl+l可以清空当前的shell内容,重新回到最上面

Navigating in the shell

  • /表示根目录,不过windows每个盘有自己的根目录,windows linux子系统下目录一般在/mnt下

  • 以/开头的路径是绝对路径,其他是相对路径,也就是相对当前路径的

  • pwd显示当前路径

  • . 表示当前路径 ..是上一级目录

  • cd xxxx切换到xxx目录 可以是相对路径可以是绝对路径

  • ls 查看当前目录下有啥

    • ls --help 查看ls命令的各种参数用法
      -ls -l 给出更详细的信息
      在这里插入图片描述
      d表示是目录 rwx三组表示对owner owner group 其他用户的权限。想要修改得有w添加删除一类的,想要enter某个文件夹得有x权限,想要list得有r权限 文件也是一样。 通常bin下面的程序都是所有人都有x权限的
      其他可以看man ls

  • mv move or rename

  • cp复制

  • mkdir新建文件夹

Connecting programs

程序有输入流和输出流
通常就是键盘输入然后输出到终端屏幕,但是也可以修改
主要是< > |三种方法

  • cmd > file 将输出重定向到 file。
  • cmd < file 将输入重定向到 file。
  • cmd >> file 讲输出以追加的方式重定向到file

在这里插入图片描述可以理解为 > tofile输出到哪里 < infile从哪里读进来然后执行程序命令 >>后续追加,而不是重写
|pipe可以让一个程序的输出变成下一个的输入,比如:
ls -l / | tail -n1
更多的会有专门一节讲解数据的处理

A versatile and powerful tool

超级用户
The root user is above (almost) all access restrictions, and can create, read, update, and delete any file in the system
一般是普通用户登录以免出现问题
当遇到权限问题,比如读写系统文件时(linux上的/sys),可能需要超级用户的权限
sudo cmd 不过sudo的权限是不能通过Pipe传递的

练习

  • windows上需要安装WSL以使用shell echo $SHELL来查看
  • 创建新的文件夹,在下面用touch创建新的文件semester
  • 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. 这里第一行想要用echo输进去,需要用单引号,其他方式都无法正确的输入进去
  • 执行这个文件: ./semester 这里的第一行指定了怎么运行,不然就得用sh来运行了
  • Look up the chmod program
  • Use | and > to write the “last modified” date output by semester into a file called last-modified.txt in your home directory.
  • ./semester | grep last-modified |cut -c15->last-modified.txt

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值