研究学习 Linux Shell 的系列文章.
这篇文章主要讲 Shell 环境和 Shell 变量的概念.
1. Shell 环境和 Bash 环境
用户使用字符表示的命令或鼠标点击动作向计算机输入指令,Shell 依据 ASCII 将用户指令翻译为二进制数指令传递给操作系统内核,系统内核按照二进制数指令调用硬件资源,计算得到结果并以二进制数形式返回给 Shell,Shell 再次依据 ASCII 将二进制数翻译为用户可理解的表示,如字符或图像,并打印在屏幕上.
抽象来说,Shell 就是人机交互界面. 命令行界面(command line interface)和图形化界面(graphical user interface)都可看作 Shell.
从计算机的具体实现来说,在 Linux 中,Shell 一般指的就是命令行界面程序. 同时,Shell 这个命令行界面程序还是 Shell 脚本语言解释器,可以运行 Shell 脚本.
综上所述,Shell 可以总结为以下两点:
- Shell 是一个命令行解释器,它为用户提供了一个向 Linux 内核发送请求以便运行程序的界面系统级,用户可以用 shell 来启动、挂起、停止甚至编写一些程序.
- Shell 还是一个功能相当强大的编程语言,易编写,易调式,灵活性强。shell 是解释执行的脚本语言,在 shell 中可以直接调用.
Shell 的分类:
- Bourne Shell:从1979起 Unix 就开始使用 Bourne Shell,Bourne Shell 的主文件名为 sh.
- C Shell:C Shell 主要在 BSD 版的 Unix 系统中使用,其语法和 C 语言相类似而得名.
Shell 的两种主要语法类型有 Bourne 和 C,这两种语法彼此不兼容. Bourne Shell 主要包括 sh、ksh、bash、psh、zsh;C Shell 主要包括:csh、tcsh.
bash 与 sh 兼容,现在使用的 Linux 就是使用 bash 作为用户的基本 Shell. 因此,一般情况下,Shell 指的就是 Bash Shell.
查看当前 Linux 使用的 Shell 类型.
$ echo "$SHELL"
/bin/bash
查看当前 Linux 支持的 Shell 类型.
$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
/usr/bin/tmux
/usr/bin/screen
进入 sh,可以发现 Shell 提示符消失了,这是因为 sh 缺少很多功能.
user@ubuntu:~$ sh
$
退出 sh.
$ exit
user@ubuntu:~$
.
2. Shell 变量及其类型
Shell 变量的分类:
- 用户自定义变量:自定义的变量;
- 环境变量:这种变量中主要保存的是和系统操作环境相关的数据。环境变量可以自定义,但是系统使用的的环境变量名和变量作用是固定的;
- 预定义变量:bash 中已经定义好的变量,变量名不能自定义,变量作用也是固定的;
- 位置参数变量:这种变量主要是用来向脚本当中传递参数或数据的,变量名不能自定义,变量作用是固定的. 位置参数变量属于预定义变量,是一类特别的预定义变量.
3. 用户自定义变量
定义变量
变量名=变量值
例如
user@ubuntu:~$ x=5
user@ubuntu:~$ name1=lucifer
user@ubuntu:~$ name2="radiant jeral"
- Shell 变量名可以由字母、数字和下划线组成,但是数字不能在首位.
- 定义变量时,等号左右不能由空格.
- 字符串中含有空格时,需要使用双引号或单引号.
user@ubuntu:~$ 5x=10
5x=10: command not found
user@ubuntu:~$ x = 5
x: command not found
user@ubuntu:~$ name2=radiant jeral
jeral: command not found
调用变量
[命令] $变量名
[命令] ${变量名}
例如:
user@ubuntu:~$ echo $x
5
user@ubuntu:~$ echo $name1
lucifer
user@ubuntu:~$ echo $name2
radiant jeral
- 在 Shell 变量中,默认所有变量都是字符串.
- Shell 变量的数值计算需要另外处理.
user@ubuntu:~$ y=6
user@ubuntu:~$ z=$x+$y
user@ubuntu:~$ echo $z
5+6
叠加变量
x=123
x="$x"456
x=${x}789
user@ubuntu:~$ x=123
user@ubuntu:~$ x="$x"456
user@ubuntu:~$ echo $x
123456
user@ubuntu:~$ x=${x}789
user@ubuntu:~$ echo $x
123456789
查看变量
set
set | more :分页显示,空格下翻,b 上翻,q 退出
选项 -u:如果设定此选项,调用未声明变量时会报错(默认无任何提示)
user@ubuntu:~$ echo $b
user@ubuntu:~$ set -u
user@ubuntu:~$ echo $b
-bash: b: unbound variable
删除变量
unset 变量名
user@ubuntu:~$ x=123
user@ubuntu:~$ unset x
user@ubuntu:~$
4. 环境变量
export 命令可以将用户自定义变量设置为环境变量.
环境变量详细笔记参看《Shell 变量之环境变量》.
5. 预定义变量
预定义变量 | 作用 |
---|---|
$? | 最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确. |
$$ | 当前进程的进程好(PID) |
$! | 后台运行的最后一个进程的进程好(PID) |
user@ubuntu:~$ ls
eg1.sh eg2.sh eg3.sh
user@ubuntu:~$ echo $?
0
user@ubuntu:~$ ls qwer
ls: cannot access 'qwer': No such file or directory
user@ubuntu:~$ echo $?
2
user@ubuntu:~$ qwer
qwer: command not found
user@ubuntu:~$ echo $?
127
user@ubuntu:~$
user@ubuntu:~$ echo $$
31147
user@ubuntu:~$ ps aux | grep 31147
lucifer 1080 0.0 0.0 13772 1040 pts/0 S+ 20:39 0:00 grep --color=auto 31147
lucifer 31147 0.0 0.2 22148 5352 pts/0 Ss 20:24 0:00 -bash
user@ubuntu:~$
编辑 displaypid.sh
#!/bin/bash
echo "current pid is $$"
运行 displaypid.sh
user@ubuntu:~$ ./displaypid.sh
current pid is 1519
编辑 displaypid.sh
, 添加内容. &
是后台运行符.
#!/bin/bash
echo "current pid is $$"
sudo find / -name hello.sh &
echo "background pid is $!"
再次运行 displaypid.sh
user@ubuntu:~$ ./displaypid.sh
current pid is 4108
background pid is 4109
6. 位置参数变量
位置参数变量 | 作用 |
---|---|
$n | n为数字,$0 代表命令本身,$1 - $9 代表第一到第九个参数,十以上的参数需要用大括号包含,如${10}. |
$* | 这个变量代表命令行中所有的参数,$* 把所有的参数堪称一个整体 |
$@ | 这个变量也代表命令行中所有的参数,不过 $@ 把每个参数区分对待 |
$# | 这个变量代表命令行中所有参数的个数 |
例子1:
eg1.sh
#!/bin/bash
num1=$1
num2=$2
sum=$(($num1 + $num2)) # parameter sum is equal to parameter num1 and parameter num2
echo $sum # print parameter sum
例子2:
eg2.sh
#!/bin/bash
echo "\$* is: $*"
echo "\$@ is: $@"
echo "\$# is: $#"
例子3:
eg3.sh
#!/bin/bash
for i in "$*"
do
echo "parameters are: $i"
done
for j in "$@"
do
echo "parameter is: $j"
done
6.1 接受键盘输入
read 命令是替代位置参数变量接受键盘输入的更好方式
read [选项] [变量名]
- 选项:
- -p “提示信息”:在等待 read 输入时,输出提示信息
- -t 秒数:read 命令会一直等待用户输入,使用此选项可以指定等待时间
- -n 字符数:read 命令只接受指定的字符数,就会执行
- -s:隐藏输入的数据,适用于机密信息的输入
编辑 read.sh
,添加 -p
参数示例代码
#!/bin/bash
read -p "please input your name: " name
echo $name
运行 read.sh
, 测试 -p
参数
user@ubuntu:~$ ./read.sh
please input your name: lucifer
lucifer
编辑 read.sh
,添加 -t
和 -s
参数实例代码
#!/bin/bash
read -p "please input your name: " -t 30 name
echo $name
read -p "please input your passwd: " -s passwd
echo -e "\n"
echo $passwd
运行 read.sh
,测试 -t
和 -s
参数
user@ubuntu:~$ ./read.sh
please input your name: lucifer
lucifer
please input your passwd:
123
编辑 read.sh
,添加 -n
参数示例代码
#!/bin/bash
read -p "please input your name: " -t 30 name
echo $name
read -p "please input your passwd: " -s passwd
echo -e "\n"
echo $passwd
read -p "please input your sex [M/F] " -n 1 sex
echo -e "\n"
echo $sex
运行 read.sh
,测试 -n
参数
user@ubuntu:~$ ./read.sh
please input your name: lucifer
lucifer
please input your passwd:
123
please input your sex [M/F] m
m