Note1_Shell

K1: Shell的定义

Shell是一个命令行解释器,它接收应用程序/用户命令,然后调用操作系统内核。

简单来说,它是外部应用程序和Linux内核沟通的桥梁。

Shell还是一个功能相当强大的编程语言,易于编写和调试,灵活性强。


K2: Shell解释器

在CentOS(RedHat系)中,/bin/sh 默认指向/bin/bash

在Ubuntu(Debian系)中,/bin/sh 默认指向/bin/dash1

这两个系统中,$SHELL 指向的都是 bash2

ls -l /bin/sh 可查看 /sh 指向,以下是两个不同系统的输出结果。

Aliyun服务器:

lrwxrwxrwx. 1 root root 4 Oct 31  2024 /bin/sh -> bash

Ubuntu虚拟机:

lrwxrwxrwx 1 root root 4 9月  11  2022 /bin/sh -> dash*

Q1: 常见的Shell解释器有哪些?

dash (Debian Almquist SHell): 符合 POSIX 标准3,同时加入了少量 Berkeley 扩展;

bash (Bourne Again SHell): 符合 POSIX 标准,提供可选的严格合规模式,同时包含了大量超出该标准的扩展(俗称 GNU Bash 扩展)。

MANUAL DESCRIPTION

Overview

The shell is a command that reads lines from either a file or the terminal, interprets them, and generally executes other commands. It is the program that is running when a user logs into the system (although a user can select a different shell with the chsh(1) command). The shell implements a language that has flow control constructs, a macro facility that provides a variety of features in addition to data storage, along with built in history and line editing capabilities. It incorporates many features to aid interactive use and has the advantage that the interpretative language is common to both interactive and non-interactive use (shell scripts). That is, commands can be typed directly to the running shell or can be put into a file and the file can be executed directly by the shell.

  • (man dash)
    dash is the standard command interpreter for the system. The current version of dash is in the process of being changed to conform with the POSIX 1003.2 and 1003.2a specifications for the shell. This version has many features which make it appear similar in some respects to the Korn shell, but it is not a Korn shell clone (see ksh(1)). Only features designated by POSIX, plus a few Berkeley extensions, are being incorporated into this shell.

  • (man bash)
    Bash is a command language interpreter that executes commands read from the standard input, from a string, or from a file. It is a reimplementation and extension of the Bourne shell, the historical Unix command language interpreter. Bash also incorporates useful features from the Korn and C shells (ksh and csh).

    POSIX is the name for a family of computing standards based on Unix. Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash POSIX mode (hereafter referred to as posix mode) changes the shell's behavior where its default operation differs from the standard to strictly conform to the standard.


Q2: 为何Ubuntu的/bin/sh$SHELL 指向的解析器不同?

它们服务的对象和场景不同。

  • /bin/sh系统默认的 POSIX Shell
  • $SHELL你的登录 Shell(Login Shell)

前者服务于系统脚本的兼容性和启动效率,后者反映的是用户登录后的交互环境

R1: bash vs dash

POSIX Shell 使用 dash 可提高启动速度。dashbash 体积更小、依赖更少,执行脚本时占用的内存和 CPU 开销也更低。

Login Shell 使用 bash 可提高易用性。bash 有众多 dash 没有的便利功能,诸如:

  • 历史命令(history
  • 自动补全(tab
  • 数组(arr=(a b c)
  • 关联数组(declare -A)等

R2: POSIX 标准

POSIX (Portable Operating System Interface) 用于维持不同操作系统间的兼容性。适用于类Unix系统(Linux、macOS、各种BSD等),确保为该系统编写的程序、脚本和用户命令,能在任何遵循该标准的其他系统上无障碍地运行。

在早期的POSIX标准体系中,1003.1和1003.2是两份独立的文档,分别负责系统接口和Shell/工具这两大块。后来,两个标准被合并成了一个统一的文档,即 POSIX.1。


K3: Shell脚本的基本格式

脚本以 #!/bin/bash 开头。(Shebang指定解析器)

说明
如果脚本依赖 bash 特性,
建议使用

#!/usr/bin/env bash

或者

#!/bin/bash

如果脚本只使用 POSIX Shell,
那么

#!/bin/sh

反而是更好的选择。

K4: Shell脚本的执行方式

  1. bash / sh + 脚本的相对路径或绝对路径(无需可执行权限)
  2. 赋予脚本可执行权限后,输入相对路径或绝对路径
演示
# Step1: 赋予脚本echo.sh可执行权限
csc@csc-VirtualBox:~/learnShell$ chmod +x echo.sh 
# Step2.1: 输入相对路径执行
csc@csc-VirtualBox:~/learnShell$ ./echo.sh
Alice is the best!
# Step2.2: 输入绝对路径执行
csc@csc-VirtualBox:~/learnShell$ /home/csc/learnShell/echo.sh 
Alice is the best!

E1: echo.sh

需求:创建一个Shell脚本,输出Alice

E2: batch.sh

需求:在/home/csc/learnShell下创建wife.txt,并在wife.txt文件中增加”Alice“。


K5: Shell中的变量

常用的系统变量

  • $HOME: 家目录
  • $PWD: 当前目录
  • $SHELL: Login Shell
  • $USER: 当前用户

如何查看变量

# echo可查看系统变量的值
echo $HOME
# set可显示当前Shell中所有变量
set

如何定义变量

  • a=10: 定义变量 ps: 定义变量时=两侧不能有空格
  • unset: 撤销变量
  • readonly: 声明静态变量 ps: 静态变量无法被撤销
  • export: 把变量导出到子进程环境

ps: 在bash中,变量默认类型都是字符串类型,无法直接进行数值运算。
pps: 变量的值如果有空格,需要使用双引号或单引号括起来。


E3: 熟悉Shell变量

  1. 查看系统变量的值
  2. 显示当前Shell中所有变量

E4: 定义变量

  1. 定义变量A
  2. 给变量A重新赋值
  3. 撤销变量A

E5: 熟悉变量性质

  1. 声明静态的变量B=2,尝试unset B
  2. 定义变量C, C=1+2
  3. 定义变量D, D的值为I love Alice

E6: 全局/局部变量有什么区别

  1. 尝试在echo.sh中打印变量D
  2. 把变量D提升为全局环境变量,并重新在echo.sh中打印

特殊变量($n, $#, $*, $@, $?)


K6: 运算符

两种基本语法

  1. “$((运算式))”或“$[运算式]”
  2. expr + , - , \*, /, % 加,减,乘,除,取余
    注意:expr运算符间要有空格
    现代 Bash 已经几乎不用expr

E7: 算术运算

  1. 计算3+2
  2. 计算3-2
  3. 计算(2+3)*4, 要求使用expr和$[]两种方式

K7: 条件测试

[ condition ](注意condition前后要有空格)

注意:条件非空即为true,[ string ]返回true,[] 返回false。

常用判断条件:

  1. 整数间比较

    = 字符串比较

    -lt 小于(less than) -le 小于等于(less equal)

    -eq 等于(equal) -gt 大于(greater than)

    -ge 大于等于(greater equal) -ne 不等于(Not equal)

  2. 判断文件权限

    -r 有读的权限(read) -w 有写的权限(write)

    -x 有执行的权限(execute)

  3. 判断文件类型

    -f 文件存在并且是一个常规的文件(file)

    -e 文件存在(existence) -d 文件存在并是一个目录(directory)

K8: If

K9: Case

K10: For

K11: While

K12: Read

K13: 函数

Shell工具(cut, sed, awk, sort)


资料

视频:【尚硅谷】Shell脚本从入门到实战

讲义:尚硅谷2018

百科:WikipediaArchWiki


  1. 关于常见的shell解释器的讨论,可参考Q1R1.

  2. 关于/bin/sh$SHELL的讨论,可参考Q2.

  3. 关于 POSIX 标准的定义,可参考R2.

评论