文章目录

  • 文档配套视频讲解链接地址
  • 第03章 Shell基础
  • 3.1 Shell的理解
  • 3.2 命令提示符格式
  • 3.3 命令的格式
  • 3.4 命令的回溯
  • 3.5 移动光标
  • 3.6 鼠标和光标
  • 3.7 测试命令
  • 3.8 Shell补齐命令与文件名
  • 3.8 Shell查询命令历史
  • 3.9 Shell命令别名
  • 3.10 Shell中的特殊字符
  • 1. 通配符
  • 2. 管道
  • 3. 输入/输出重定向
  • 3.11 Shell命令置换

第03章 Shell基础

3.1 Shell的理解

随着各式Linux系统的图形化程度的不断提高,用户在桌面环境下,通过点击、拖拽等操作就可以完成大部分的工作。

然而,许多Ubuntu Linux功能使用Shell命令来实现,要比使用图形界面交互,完成的更快、更直接

英文单词Shell可直译为“贝壳”。“贝壳”是动物作为外在保护的一种工具。

可以这样认为,Linux中的Shell就是Linux内核的一个外层保护工具,并负责完成用户与内核之间的交互

android12 运行SHELL脚本 android linux shell_android12 运行SHELL脚本

命令是用户向系统内核发出控制请求,与之交互的文本流。

Shell是一个命令行解释器,将用户命令解析为操作系统所能理解的指令,实现用户与操作系统的交互。

同时,Shell为操作系统提供了内核之上的功能,直接用来管理和运行系统。

当需要重复执行若干命令,可以将这些命令集合起来,加入一定的控制语句,编辑成为Shell脚本文件,交给Shell批量执行。

3.2 命令提示符格式

linux@ubuntu:~$ 
root@ubuntu:/home/linux# 

linux  , root  :是用户名 
root 用户是管理员权限的用户, 等价于 window 的 admin 用户 
@      : 是一个分隔符号 
ubuntu : 计算机名 
~      : 家目录  , 就是/home/linux , 家目录是根目录的一个子目录 
/      : 根目录  , 目录的起始位置 

$      : 表示用户是普通用户 
#      : 表示用户是超级用户

3.3 命令的格式

#命令格式  Shell命令格式 
通常一条命令包含三个要素:命令名称、选项、参数。命令名称是必须的,选项和参数都可能是可选项。
命令格式如下所示:
Command [-Options] Argument1 Argument2  …
指令      选项      参数1      参数2      …

#Command:命令名称,Shell命令或程序,严格区分大小写,例如设置日期指令为date等;
#Options:命令选项,用于改变命令执行动作的类型,由“-”引导,可以同时带有多个选项;
#Argument:命令参数,指出命令作用的对象或目标,有的命令允许带多个参数。

#一条命令的三要素之间用空格隔开;   注意 注意 
#若将多个命令在一行书写,用分号(;)将各命令隔开;
#如果一条命令不能在一行写完,在行尾使用反斜杠(\)标明该条命令未结束。
#命令不带选项或参数,通常意为使用默认选项或参数。

3.4 命令的回溯

使用键盘的上下键 来进行显示命令历史 
↑ : 用来向前回溯命令
↓ : 用来向后回溯命令

window下的powershell 也支持这种操作

3.5 移动光标

1. 可借助上箭头按键,来获得上次输入的命令。
2. 使用左右箭头按键。定位到命令行的任意位置 , 编辑命令

3.6 鼠标和光标

1. 可以在终端仿真器里使用鼠标。 X 窗口系统支持快速拷贝和粘贴技巧。按下鼠标左键,沿着文本拖动鼠标(或者双击一个单词)高亮了一些文本,那么这些高亮的文本就被拷贝到了一个由 X管理的缓冲区里面。然后按下鼠标中键,这些文本就被粘贴到光标所在的位置

2. 不要在一个终端窗口里使用 Ctrl-c 和 Ctrl-v 快捷键来执行拷贝和粘贴操作。它们不起作用。
    复制的快捷键:  ctrl + shift + c
    粘贴的快捷键:  ctrl + shift + v
    
3. ctrl + c :作用是结束一个正在运行的程序 

4. ctrl + l  : 清除屏幕显示的快捷键

3.7 测试命令

date :显示系统的时间
cal  :显示系统的日历 
df   :查看磁盘使用情况
free :显示系统的空闲内存
linux@ubuntu:~/Desktop$ date
2022年 08月 17日 星期三 14:57:47 CST
linux@ubuntu:~/Desktop$ cal
      八月 2022         
日 一 二 三 四 五 六  
    1  2  3  4  5  6  
 7  8  9 10 11 12 13  
14 15 16 17 18 19 20  
21 22 23 24 25 26 27  
28 29 30 31           
                      
linux@ubuntu:~/Desktop$ df
文件系统          1K-块    已用     可用 已用% 挂载点
udev             974532       0   974532    0% /dev
tmpfs            199940    1612   198328    1% /run
/dev/sda1      51288544 8959400 39691424   19% /
tmpfs            999684       0   999684    0% /dev/shm
tmpfs              5120       4     5116    1% /run/lock
tmpfs            999684       0   999684    0% /sys/fs/cgroup
/dev/loop1         2560    2560        0  100% /snap/gnome-calculator/884
/dev/loop0        56832   56832        0  100% /snap/core18/2128
/dev/loop2       410496  410496        0  100% /snap/gnome-3-38-2004/112
/dev/loop3          640     640        0  100% /snap/gnome-logs/112
/dev/loop4          640     640        0  100% /snap/gnome-logs/106
/dev/loop5          768     768        0  100% /snap/gnome-characters/726
/dev/loop6        56960   56960        0  100% /snap/core18/2538
/dev/loop7         2688    2688        0  100% /snap/gnome-calculator/920
/dev/loop8       224256  224256        0  100% /snap/gnome-3-34-1804/72
/dev/loop9       247168  247168        0  100% /snap/gnome-3-38-2004/70
/dev/loop10        2560    2560        0  100% /snap/gnome-system-monitor/163
/dev/loop11       63488   63488        0  100% /snap/core20/1611
/dev/loop12        2688    2688        0  100% /snap/gnome-system-monitor/178
/dev/loop13         128     128        0  100% /snap/bare/5
/dev/loop14       63488   63488        0  100% /snap/core20/1593
/dev/loop15       93952   93952        0  100% /snap/gtk-common-themes/1535
/dev/loop16       48128   48128        0  100% /snap/snapd/16292
/dev/loop17       66688   66688        0  100% /snap/gtk-common-themes/1515
/dev/loop18         768     768        0  100% /snap/gnome-characters/741
/dev/loop19      224256  224256        0  100% /snap/gnome-3-34-1804/77
tmpfs            199936      28   199908    1% /run/user/1000
linux@ubuntu:~/Desktop$ free
              总计         已用        空闲      共享    缓冲/缓存    可用
内存:     1999368     1020104      153640       17880      825624      804584
交换:     2097148        1036     2096112
linux@ubuntu:~/Desktop$

3.8 Shell补齐命令与文件名

在shell解释器的快捷键中,Tab键绝对是使用频率最高的,它能够实现对命令、参数或文件的内容补全。例如,如果想执行reboot重启命令,但一时想不起来该命令的完整拼写,则可以这样输入:

  1. tab 实现命令的补全
linux@ubuntu:~/Desktop$ re<Tab键><Tab键>
read                     readonly                 recode-sr-latin          regdbdump               
remove-shell             reset                    return
readarray                readprofile              recountdiff              
reject                   rename.ul                resize2fs                rev
readelf                  realpath                 red                      
remove-default-ispell    rendercheck              resizecons               
readlink                 reboot                   rediff                  
remove-default-wordlist  renice                   resizepart      
linux@ubuntu:~/Desktop$ reb<Tab键> 
linux@ubuntu:~/Desktop$ reboot
  1. tab 实现文件名的补全
linux@ubuntu:~$ touch hello.c
linux@ubuntu:~$ ls
Desktop  Documents  Downloads  hello.c  Music  Pictures  Public  snap  Templates  Videos
linux@ubuntu:~$ vi he<Tab键>   #文件名补全
linux@ubuntu:~$ vi hello.c    # 按下两次 shift +z 退出

3.8 Shell查询命令历史

history命令用于显示执行过的命令历史,语法格式为 “history [-c]”。

history命令应该是运维人员最喜欢的命令。执行history命令能显示出当前用户在本地计算机中执行过的最近1000条命令记录。如果觉得1000不够用,可以自定义/etc/profile文件中的HISTSIZE变量值。

在使用history命令时,可以使用-c参数清空所有的命令历史记录。还可以使用“!编码数字”的方式来重复执行某一次的命令。

linux@ubuntu:~$ history 
    1  ls
    2  sudo apt-get install open-vm-tools-desktop
    3  exit
    4  sudo apt install open-vm-tools-desktop
    5  sudo apt autoremove 
    6  ls
    7  sudo apt install open-vm-tools-desktop
    8  ls
    9  shell
   10  ls
   11  sudo su 
   12  pwd
   13  ls
   14  cd Desktop/
   15  ls

linux@ubuntu:~$ !1    # 执行命令历史的第一个命令 
ls
Desktop  Documents  Downloads  hello.c  Music  Pictures  Public  snap  Templates  Videos
linux@ubuntu:~$ history -c   # 清除命令的记录 
linux@ubuntu:~$ history 
    1  history

3.9 Shell命令别名

  • 命令别名通常是其他命令的缩写,用来减少键盘输入。该命令的一般格式如下所示。
alias   alias-name='original-command'    # 添加命令的别名 
unalias  alias-name                      # 删除命令的别名
  • 例如 :
linux@ubuntu:~$ alias myls='ls -l'   # 单引号, 不是反引号 ,别弄错, 中间不能有空格  
linux@ubuntu:~$ myls 
总用量 36
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Desktop
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Documents
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Downloads
-rw-rw-r-- 1 linux linux    0 8月  17 15:18 hello.c
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Music
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Pictures
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Public
drwx------ 4 linux linux 4096 8月  17 10:19 snap
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Templates
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Videos
linux@ubuntu:~$ ls -l
总用量 36
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Desktop
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Documents
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Downloads
-rw-rw-r-- 1 linux linux    0 8月  17 15:18 hello.c
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Music
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Pictures
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Public
drwx------ 4 linux linux 4096 8月  17 10:19 snap
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Templates
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Videos
linux@ubuntu:~$ unalias myls    # 删除命令的别名 
linux@ubuntu:~$ myls

Command 'myls' not found, did you mean:

  command 'tyls' from deb terminology
  command 'mmls' from deb sleuthkit

Try: sudo apt install <deb name>

linux@ubuntu:~$

3.10 Shell中的特殊字符

1. 通配符

当需要用命令处理一组文件,例如file1.txt、file2.txt、file3.txt……,用户不必一一输入文件名,可以使用Shell通配符

1. 星号(*): 匹配任意长度的字符串  用file_*.txt,匹配file_wang.txt、file_zhang.txt、file_liu.txt


2. 问号(?): 匹配一个长度的字符 用flie_?.txt,匹配file_1.txt、file1_2.txt、file_3.txt

3. 方括号([…]) :匹配其中指定的一个字符 用file_[otr].txt,匹配file_o.txt、file_r.txt和file_t.txt

4. 方括号([ - ]):匹配指定的一个字符范围 用file_[a-z].txt,匹配file_a.txt、file_b.txt,直到file_z.txt

5. 方括号([^…]) : 除了其中指定的字符,均可匹配 用file_[^otr].txt,除了file_o.txt、file_r.txt和file_t.txt的其他文件
   在c语言清除垃圾字符时, 使用过这个表达 scanf("%*[^\n]")
  • 操作结果
# 通配符* 的理解 
linux@ubuntu:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos
linux@ubuntu:~$ mkdir work
linux@ubuntu:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos  work
linux@ubuntu:~$ cd work/
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$ touch file_wang.c file_liu.c file_zhang.c
linux@ubuntu:~/work$ ls
file_liu.c  file_wang.c  file_zhang.c
linux@ubuntu:~/work$ ls file_*
file_liu.c  file_wang.c  file_zhang.c
linux@ubuntu:~/work$ rm file_*
linux@ubuntu:~/work$ ls
# 通配符 ? 的理解 
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$ touch file_1.c file_2.c file_3.c
linux@ubuntu:~/work$ ls
file_1.c  file_2.c  file_3.c
linux@ubuntu:~/work$ ls file?
ls: 无法访问'file?': 没有那个文件或目录
linux@ubuntu:~/work$ ls file*
file_1.c  file_2.c  file_3.c
linux@ubuntu:~/work$ ls file_?
ls: 无法访问'file_?': 没有那个文件或目录
linux@ubuntu:~/work$ ls file_?.c
file_1.c  file_2.c  file_3.c
linux@ubuntu:~/work$ rm file_?.c
linux@ubuntu:~/work$ ls 

# [] 通配符的理解 
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$ touch file_o.c file_t.c file_r.c file_1.c file_2.c file_3.c
linux@ubuntu:~/work$ ls
file_1.c  file_2.c  file_3.c  file_o.c  file_r.c  file_t.c
linux@ubuntu:~/work$ ls file_[otr].c
file_o.c  file_r.c  file_t.c
linux@ubuntu:~/work$ ls file_[otr12].c
file_1.c  file_2.c  file_o.c  file_r.c  file_t.c
linux@ubuntu:~/work$ ls file_[ot].c
file_o.c  file_t.c
linux@ubuntu:~/work$ rm  file_[otr123].c
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$ 

# [-] 通配符的理解
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$ touch file_a.c file_b.c file_x.c file_z.c 
linux@ubuntu:~/work$ ls
file_a.c  file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ ls file_[a-n].c
file_a.c  file_b.c
linux@ubuntu:~/work$ ls file_[a-x].c
file_a.c  file_b.c  file_x.c
linux@ubuntu:~/work$ ls file_[a-z].c
file_a.c  file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ rm  file_[a-z].c
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$ 

# [^]  通配符的理解
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$ touch file_1.c file_2.c file_a.c file_b.c file_x.c file_z.c
linux@ubuntu:~/work$ ls
file_1.c  file_2.c  file_a.c  file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ ls file[^\n].c
ls: 无法访问'file[^n].c': 没有那个文件或目录
linux@ubuntu:~/work$ ls file_[^\n].c
file_1.c  file_2.c  file_a.c  file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ ls file_[^1].c
file_2.c  file_a.c  file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ ls file_[^12].c
file_a.c  file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ ls file_[^12a].c
file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ ls file_[^\n].c
file_1.c  file_2.c  file_a.c  file_b.c  file_x.c  file_z.c
linux@ubuntu:~/work$ rm file_[^\n].c
linux@ubuntu:~/work$ ls
linux@ubuntu:~/work$
2. 管道

管道可以把一系列命令连接起来,意味着第一个命令的输出将作为第二个命令的输入,通过管道传递给第二个命令,第二个命令的输出又将作为第三个命令的输入,以此类推。就像通过使用“|”符连成了一个管道。

  • 例如:
linux@ubuntu:~/work$ cd ..   # 返回上一级目录 
linux@ubuntu:~$ ls -l        # 查看目录的详细信息
总用量 40
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Desktop
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Documents
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Downloads
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Music
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Pictures
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Public
drwx------ 4 linux linux 4096 8月  17 10:19 snap
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Templates
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Videos
drwxrwxr-x 2 linux linux 4096 8月  18 09:53 work
linux@ubuntu:~$ ls -l | grep work    # grep work 搜索关键字 work , 输出搜索的结果 
drwxrwxr-x 2 linux linux 4096 8月  18 09:53 work
linux@ubuntu:~$ ls -l | grep Music
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Music
linux@ubuntu:~$ ls -l | grep Desk
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Desktop
linux@ubuntu:~$ ls -l | grep D
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Desktop
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Documents
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Downloads
linux@ubuntu:~$
3. 输入/输出重定向
  • 输入/输出重定向是改变Shell命令或程序默认的标准输入/输出目标,重新定向到新的目标。
  • Linux中默认的标准输入定义为键盘,标准输出定义为终端窗口
  • 输出重定向 >
# 输出重定向 
>  file   #  把输出的内容 定向到文件内 
ls > log 
# 解释 如果有同名文件log , 同名文件会被删除 ,如果文件不存在则创建这个文件
  • 例如:
linux@ubuntu:~$ ls 
Desktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos  work

#如果有同名文件log , 同名文件会被清空后再写入新的内容 ,如果文件不存在则创建这个文件 
linux@ubuntu:~$ ls > log   # 把输出到终端的内, 写到文件内  
linux@ubuntu:~$ cat log    # 显示文件中的内容 
Desktop
Documents
Downloads
log
Music
Pictures
Public
snap
Templates
Videos
work
linux@ubuntu:~$ ls
Desktop  Documents  Downloads  log  Music  Pictures  Public  snap  Templates  Videos  work
  • 输出重定向 >>
>> file  :将file文件重定向为输出源,追加模式
  • 例如:
linux@ubuntu:~$ cat log 
Desktop
Documents
Downloads
log
Music
Pictures
Public
snap
Templates
Videos
work
linux@ubuntu:~$ ls -l >>log   # 如果log 不存在则创建, 如果存在则追加写入, 文件内容不清空
linux@ubuntu:~$ cat log 
Desktop
Documents
Downloads
log
Music
Pictures
Public
snap
Templates
Videos
work
总用量 44
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Desktop
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Documents
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Downloads
-rw-rw-r-- 1 linux linux   81 8月  18 10:22 log
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Music
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Pictures
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Public
drwx------ 4 linux linux 4096 8月  17 10:19 snap
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Templates
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Videos
drwxrwxr-x 2 linux linux 4096 8月  18 09:53 work
linux@ubuntu:~$
  • 输入重定向
< file  : 将file文件重定向为输入源
  • 例如:
linux@ubuntu:~$ ls
Desktop  Documents  Downloads  log  Music  Pictures  Public  snap  Templates  Videos  work
linux@ubuntu:~$ grep work <log
work
drwxrwxr-x 2 linux linux 4096 8月  18 09:53 work
linux@ubuntu:~$ cat log 
Desktop
Documents
Downloads
log
Music
Pictures
Public
snap
Templates
Videos
work
总用量 44
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Desktop
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Documents
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Downloads
-rw-rw-r-- 1 linux linux   81 8月  18 10:22 log
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Music
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Pictures
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Public
drwx------ 4 linux linux 4096 8月  17 10:19 snap
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Templates
drwxr-xr-x 2 linux linux 4096 8月  16 11:03 Videos
drwxrwxr-x 2 linux linux 4096 8月  18 09:53 work
linux@ubuntu:~$

3.11 Shell命令置换

  • 命令替换是将一个命令的输出作为另一个命令的参数。命令格式如下所示。
command1  `command2`   # `  是反引号

其中,命令command2的输出将作为命令command1的参数。需要注意,命令置换的单引号为ESC键下方的“`”键

  • 例如:
linux@ubuntu:~$ pwd    # 显示当先的目录 
/home/linux
linux@ubuntu:~$ ls `pwd`     # 等价于 ls /home/linux
Desktop  Documents  Downloads  log  Music  Pictures  Public  snap  Templates  Videos  work