系统相关
首页 > 系统相关> > 【Missing Semester】课程概览与Shell

【Missing Semester】课程概览与Shell

作者:互联网

本文为计算机教育中缺失的一课 The Missing Semester of Your CS Education 笔记

Shell

Shell 是什么:

Shell 的种类:

终端

打开终端的几种方法(Mac):

终端样式设置:

第一个命令:

bogon:Missing Semester wangxinyuan$ echo "Hello World"
Hello World
bogon:Missing Semester wangxinyuan$ date
2022年 2月17日 星期四 10时55分59秒 CST

# 查看当前使用的Shell
bogon:Missing Semester wangxinyuan$ echo $SHELL
/bin/bash
# 查看安装的Shell
bogon:Missing Semester wangxinyuan$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

命令是如何执行的:

​ 当你在 Shell 中执行命令时,实际上是在执行一段 Shell 可以解释执行的简短代码。如果你要求 Shell 执行某个指令,但是该指令并不是 Shell 所了解的编程关键字,那么它会去咨询环境变量 $PATH,它会列出当 Shell 接到某条指令时,进行程序搜索的路径。

bogon:Missing Semester wangxinyuan$ echo $PATH
/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/3.0.0/bin:/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/3.0.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
# 定位echo
bogon:Missing Semester wangxinyuan$ which echo
/bin/echo
bogon:Missing Semester wangxinyuan$ which ruby
/usr/bin/ruby
# 等价于echo “Hello World”
bogon:Missing Semester wangxinyuan$ /bin/echo "Hello World"
Hello World

文件

路径:

bogon:Missing Semester wangxinyuan$ pwd
/Users/wangxinyuan/Desktop/Missing Semester
# 绝对路径
bogon:Missing Semester wangxinyuan$ cd /Users/wangxinyuan/Desktop

# .
bogon:Desktop wangxinyuan$ cd ./Missing\ Semester

# ..
bogon:Missing Semester wangxinyuan$ cd ..

# 相对路径
bogon:Desktop wangxinyuan$ cd "Missing Semester"

# previously
bogon:Missing Semester wangxinyuan$ cd -
/Users/wangxinyuan/Desktop

# home
bogon:Desktop wangxinyuan$ cd
bogon:~ wangxinyuan$ ls
Applications	Library		Pictures	a.out		bt.plist
Desktop		Movies		Public		ap.plist	history.plist
Documents	Music		PycharmProjects	apps.plist
Downloads	OneDrive	Users		brew_install

一些文件操作命令:

# 在当前文件夹创建"名未命文件夹"
bogon:tmp wangxinyuan$ mkdir 名未命文件夹

# 在"名未命文件夹"中新建"笔记文件"
bogon:tmp wangxinyuan$ touch ./名未命文件夹/笔记.txt

# 删除"名未命文件夹"中"笔记文件"
bogon:tmp wangxinyuan$ rm ./名未命文件夹/笔记.txt

# 删除"名未命文件夹"
bogon:tmp wangxinyuan$ rm -r 名未命文件夹

# 将"missing文件夹"中"semester.txt文件"移动到"名未命文件夹"
bogon:tmp wangxinyuan$ mv missing/semester.txt 名未命文件夹

# 将"名未命文件夹"文件夹中"semester.txt文件"移动到"missing文件夹"并重命名为"hhh.txt"
bogon:tmp wangxinyuan$ mv 名未命文件夹/semester.txt missing/hhh.txt

# 将"missing文件夹"中"hhh.txt文件"复制到"名未命文件夹"
bogon:tmp wangxinyuan$ cp missing/hhh.txt 名未命文件夹

查看帮助信息:

流的重定向:

>>>

bogon:tmp wangxinyuan$ echo "Hello World" > out.txt
bogon:tmp wangxinyuan$ cat out.txt
Hello World
# out.txt中内容如下
# Hello World
bogon:tmp wangxinyuan$ echo "Hello WXY" >> out.txt
# out.txt中内容如下
# Hello World
# Hello WXY
bogon:tmp wangxinyuan$ echo "hhh" > out.txt
# out.txt中内容如下
# hhh

| 操作符:

bogon:tmp wangxinyuan$ ls -l
total 8
drwxr-xr-x  3 wangxinyuan  staff   96  2 17 14:25 missing
-rw-r--r--@ 1 wangxinyuan  staff  251  2 17 14:44 out.txt
drwxr-xr-x  4 wangxinyuan  staff  128  2 17 14:27 名未命文件夹
bogon:tmp wangxinyuan$ ls -l | tail -n1
drwxr-xr-x  4 wangxinyuan  staff  128  2 17 14:27 名未命文件夹

根用户

课后练习

5 ~ 8 题:

# 创建exercise
bogon:tmp wangxinyuan$ touch exercise

# 写入exercise
bogon:tmp wangxinyuan$ echo '#!/bin/sh' >> exercise
bogon:tmp wangxinyuan$ echo "curl --head --silent https://baidu.com" >> exercise

# 打印exercise内容
bogon:tmp wangxinyuan$ cat exercise
#!/bin/sh
curl --head --silent https://baidu.com

# 使用./exercise执行exercise,失败
bogon:tmp wangxinyuan$ ./exercise
-bash: ./exercise: Permission denied

# 使用sh执行exercise
bogon:tmp wangxinyuan$ sh exercise
HTTP/1.1 302 Moved Temporarily
Server: bfe/1.0.8.18
Date: Thu, 17 Feb 2022 07:20:58 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://www.baidu.com/

# 失败原因:没有执行权限
bogon:tmp wangxinyuan$ ls -l exercise
-rw-r--r--  1 wangxinyuan  staff  49  2 17 15:20 exercise

# 使用chmod修改权限
bogon:tmp wangxinyuan$ chmod a+rx exercise
bogon:tmp wangxinyuan$ ls -l exercise
-rwxr-xr-x  1 wangxinyuan  staff  49  2 17 15:20 exercise

# 使用./exercise执行exercise,成功
bogon:tmp wangxinyuan$ ./exercise
HTTP/1.1 302 Moved Temporarily
Server: bfe/1.0.8.18
Date: Thu, 17 Feb 2022 07:21:56 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://www.baidu.com/

参考

标签:tmp,Shell,Missing,wangxinyuan,Semester,文件夹,bogon,txt,exercise
来源: https://www.cnblogs.com/wxy4869/p/15904995.html