初识Shell

什么是shell?

不同于可视化界面,shell这种基于文本的工具可以让你对计算机的操作更加自由,你可以通过它来访问操作系统内核的服务。绝大部分操作系统都会提供某种shell(比如Windows的PowerShell,Linux的终端等)。

总之,对我们(初学者)来说,它是操控Linux的主要工具。

shell命令基础

date

  • 作用:显示当前日期及时间
lighthouse@name:~$ date
Sun Aug 28 22:59:55 CST 2022

echo

  • 作用:在屏幕上显示一段文字
lighthouse@name:~$ echo hello world #建议带上引号,空格多了会自动缩减为一个,如果不加引号,要输出多个连续空格则需要使用转义字符"\ "
#单引号:原封不动地输出
#双引号:会解析特殊符号及变量(比如转义字符)
hello world

PATH

  • 环境变量:shell设定好的一堆东西,输入命令后,shell会在这些目录里寻找是否有与此命令相同的程序或文件,然后运行它
lighthouse@name:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games #此处省略一堆PATH
which
  • 作用:输出这个命令的绝对路径
lighthouse@name:~$ which echo
/usr/bin/echo

pwd

  • 全称:print working directory
  • 作用:输出当前所在目录路径
lighthouse@name:~$ pwd
/home/lighthouse

cd

  • 全称:change directory
  • 作用:改变当前的工作目录
lighthouse@name:~$ cd /usr/bin
lighthouse@name:/usr/bin$
. ..
  • .表示当前目录,..表示上级目录
  • 使用..还是绝对路径:哪个短用哪个(写脚本为兼容性的话用绝对路径)
lighthouse@name:/usr/bin$ cd ..
lighthouse@name:/usr$ cd ../home/lighthouse
lighthouse@name:~$
~-
  • ~表示当前登录的用户目录,-表示所处的上一个目录
lighthouse@name:/$ cd ~
lighthouse@name:~$ cd -
/

ls

  • 全称:list
  • 作用:列出当前目录下的所有文件
lighthouse@name:/$ ls
bin   data  etc   init  lib32  libx32           media  opt   root  sbin  srv  tmp  var
boot  dev   home  lib   lib64  linux.chmod.bak  mnt    proc  run   snap  sys  usr

lighthouse@name:/$ ls -a    #-a的作用是把隐藏文件也列出来(通常以.开头)
.   bin   data  etc   init  lib32  libx32           media  opt   root  sbin  srv  tmp  var
..  boot  dev   home  lib   lib64  linux.chmod.bak  mnt    proc  run   snap  sys  usr

lighthouse@name:/$ ls -l    #-l作用是显示详细信息
total 4624
lrwxrwxrwx  1 root root             7 Apr 23  2020 bin -> usr/bin
drwxr-xr-x  1 root root          4096 Apr 23  2020 boot
drwxr-xr-x  1 root root          4096 Aug 18 19:48 data
(此处省略一堆文件详细信息)

--help

  • 作用:输出某个命令的帮助
lighthouse@name:/$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               e.g., '--block-size=M'; see SIZE format below
(此处省略)

其中,...表示不填或一个或更多,[]表示可选

权限

ls -l后,可以看到每行开头的权限情况。

lrwxrwxrwx  1 root root             7 Apr 23  2020 bin -> usr/bin
drwxr-xr-x  1 root root          4096 Apr 23  2020 boot
drwxr-xr-x  1 root root          4096 Aug 18 19:48 data

其中,第一个字母表示是文件l还是文件夹d

后面有三组三个字符组成的字符串,第一组是文件所有者的权限,第二组是有这些文件的用户组权限,第三组是非所有者的其他人的权限。

-表示没有对应权限;rread,读权限;wwrite,写权限;xexecute(大概),执行权限(访问权限包含在x权限中,决定了你能否进入这个目录)。

mv

  • 作用:移动或重命名文件
#重命名
lighthouse@name:~$ ls
read.txt  sys_backup.bak
lighthouse@name:~$ mv read.txt read0.txt
lighthouse@name:~$ ls
read0.txt  sys_backup.bak

#移动
lighthouse@name:~$ sudo mv read0.txt ../read.txt
lighthouse@name:~$ ls ..
lighthouse  read.txt

cp

  • 全称:copy
  • 作用:拷贝 参考mv

rm

  • 全称:remove
  • 作用:删除文件,默认非递归
lighthouse@name:~$ ls
sys_backup.bak  test  txt.txt
lighthouse@name:~$ rm test
rm: cannot remove 'test': Is a directory
lighthouse@name:~$ rm txt.txt
lighthouse@name:~$ ls
sys_backup.bak  test
lighthouse@name:~$ rm -r test   #加上-r,递归地删除
lighthouse@name:~$ ls
sys_backup.bak
rmdir
  • 作用:删除目录(必须是空目录)

mkdir

  • 全称:make directory
  • 作用:创建文件夹
lighthouse@name:~$ mkdir hello world
lighthouse@name:~$ ls
hello  sys_backup.bak  world
#如果想创建带空格的文件夹,则需要转义字符或者是用引号

man

  • 全称:manual pages
  • 作用:类似于 --help

流 stream

shell会给每个程序创建两个主要的流(简化),一个输入流(input stream)和一个输出流(output stream)。默认输入流是键盘,默认输出流是终端。

当然,shell也提供了重定向这些流的方法

<>

  • <:重定向输入流;>:重定向输出流。
lighthouse@name:~$ echo 'hello world' > hello.txt
lighthouse@name:~$ cat hello.txt    #cat:连接文件并打印到标准输出设备上
hello world

lighthouse@name:~$ cat < hello.txt > hello2.txt #实现cp的作用
lighthouse@name:~$ cat hello2.txt
hello world

lighthouse@name:~$ cat < hello.txt >> hello2.txt #两个>表示追加,而不是覆盖
lighthouse@name:~$ cat hello2.txt
hello world
hello world
| pipe
  • 作用:取左侧程序的输出,成为右侧程序的输入
# tail打印它输入的最后n行,这里打印最后一行,故为-n1
# 注意,它们之间并没有设计互相兼容的模块,它们只是各司其职,由pipe把它们连到一起
lighthouse@name:~$ ls -l / | tail -n1
drwxr-xr-x  1 root root          4096 Apr 23  2020 var

lighthouse@name:~$ ls -l / | tail -n1 > ls.txt #可以重定向输出流到文件
lighthouse@name:~$ cat ls.txt
drwxr-xr-x  1 root root          4096 Apr 23  2020 var

lighthouse@name:~$ curl --head --silent baidu.com #给你展示访问baidu.com时,所有的headers
HTTP/1.1 200 OK
Date: Mon, 29 Aug 2022 01:37:40 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Tue, 30 Aug 2022 01:37:40 GMT
Connection: Keep-Alive
Content-Type: text/html

#grep用于关键字搜索,grep -ri '关键字' 路径
lighthouse@name:~$ curl --head --silent baidu.com | grep -i Content-Type
Content-Type: text/html

sudo

  • 全称:do as super user
  • 作用:以root身份运行命令(放在命令前面即可)
$#

在终端中可以看到输入处前有一个符号$(如上文)

lighthouse@name:~$ sudo su
[sudo] password for lighthouse:
root@name:/home/lighthouse# 

tee

  • 作用:从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件
#相当于输入到文件后你还能瞅一眼文件里有啥
lighthouse@name:~$ echo hello | tee h.txt
hello

xdg-open

  • 作用:用合适的程序打开某个文件

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.

  1. 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.
  2. Create a new directory called missing under /tmp.
  3. Look up the touch program. The man program is your friend.
  4. Use touch to create a new file called semester in missing.
  5. 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.
  6. 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).
  7. 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?
  8. Look up the chmod program (e.g. use man chmod).
  9. 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.
  10. Use | and > to write the “last modified” date output by semester into a file called last-modified.txt in your home directory.
  11. 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.

SUFE大二在读