文章目录
一、shell 的定义及种类
- shell 是用户和linux 操作系统之间的接口,它基本上是一个命令解释器,接收用户命令(如 ls 等),然后调用相应的应用程序。
- 系统提供的 shell 种类如下图所示,不同的 shell 具备不同的功能,ubuntu 中默认使用 bash shell
- 更改系统指定的 shell
usermod –s(hell) shellname username
chsh –s(hell) shellname username
二、shell 中的变量
1. shell 中变量的种类
- 环境变量(全局变量):
- 常用的环境变量如
PATH(可执行文件路径)、PYTHONPATH、LD_LIBRARY_PATH(程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径)
,使用env
命令查看系统中所有的环境变量,亦可用echo $PATH
来查看某一环境变量 - 注意:
LIBRARY_PATH
是程序编译期间查找动态链接库时指定查找共享库的路径 - 环境变量是全局的,它适用于所有当前 shell 以及其派生出来的任意子进程(包括编辑器、脚本等)
- 环境变量对 bash 的影响:能不能执行某些命令和 PATH 有关,如果在 PATH 中找不到相关命令,屏幕会出现
command not found。
- 常用的环境变量如
- 本地变量(局部变量):
- 包括一些用户自定义的变量等
- 局部变量只适用于当前 shell,即用户的当前shell环境,当用户注销,或者启用子 shell、子进程时不起作用
2. shell 环境变量的初始化
- 系统级
/etc/profile
:为系统的每一个用户设置环境变量信息,和 bash.bashrc 的区别是 profile 中的设置对所有类型的 shell 都是有效的/etc/bash.bashrc
:此文件是每个用户运行 Bash Shell 的时候都要执行的配置文件,其中设置了一部分的环境变量和命令提示符。因此此文件的配置信息影响着每一个用户的 shell 环境/etc/environment
:此文件定义了系统的环境变量,默认情况下只有 PATH 变量
- 用户级
~/profile
:专用于当前用户的shell 信息设置,默认情况下,其设置一些环境变量,执行用户的~/.bashrc
文件~/.bashrc
:专用于当前用户的 Bash shell 信息,用户可以在此文件中设置自己的 shell 变量alias
:查看当前用户的别名设置vim ~/.bashrc alias ll='ls -alF'
:在当前用户系统中设置命令别名,若想对系统的每个用户都是用此设置,则需要使用管理员权限修改sudo vim /etc/bashrc alias ll='ls -alF'
3. shell 中的变量设置及读取
- 变量的设置
- 变量与变量内容通过
=
号来连接,且=
两边不能包含空格,变量内容包含空格则需要使用单引号或者双引号,变量内容由其他命令提供时使用$()
或者代码符 `` - 取消变量的定义:unset variable
- 将普通变量变为环境变量:export variable=variable_value,在命令行执行此命令的话,只能在当前的 Session 中有效;要想永久生效,需要修改系统或者用户的 profile 文件
- 数组定义:array_name=(v1 v2 … vn),元素间用空格隔开
- 数组引用:${array_name[index]}
- 变量与变量内容通过
- 变量的读取
echo $variable
,$ 取得变量的值,echo 将变量的值打印到屏幕上。echo ${variable}
# 变量读取的另一种格式。read -p "str:" varible_name
: 从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 varible_name(注意:字符串 str 和变量名 varible_name 间有一个空格)
三、shell 中的基本运算符
1. 算术运算符(+ - * / %)
- 基本语法:
result=$((运算内容))
,变量内容由其它运算命令提供;其它非运算命令可使用result=$(命令行)
- eg:两数相乘
result=$(($first_num*$sec_num))
orresult=$(dirname $0)
2. 条件判断表达式
bash 的条件判断表达式中有三个几乎等效的符号和命令:
test,[] 和 [[]]
。[[]]
是兼容性强(支持> < && ||
等),而且性能比较快,在做条件运算时候,应优先考虑它!
-
i. 比较运算符
比较运算符只支持数字,不支持字符串,除非字符串的值是数字。
Note:[]
首尾要各空一格-eq
:检测两个数是否相等,eg:[ $a -eq $b ] or [ $a == $b ] or [[ $a == $b ]]
-ne
:检测两个数是否不相等,eg:[ $a -eq $b ] or [ $a != $b ] or [[ $a != $b ]]
-gt
:检测左边的数是否大于右边的;[ $a -gt $b ] or [[ $a > $b ]]
-lt
:检测左边的数是否小于右边的;[ $a -lt $b ] or [[ $a < $b ]]
-ge
:检测左边的数是否大于等于右边的;[ $a -ge $b ]
-le
:检测左边的数是否小于等于右边的;[ $a -le $b ]
-
ii. 逻辑运算符
[[]]
运算符是[]
运算符的扩充。能够支持< >
符号运算不需要转义符,计算速度最快!多条件在一起时,必须使用
[[ cond1 && cond2 ]]
或者[ cond1 ] && [ cond 2 ]
或者[ cond1 -a cond2 ]
这几种形式之一!&&
:逻辑与,若 cmd1 正确执行($?=0
),则开始执行 cmd2;若 cmd1 执行出现错误 ($?≠0
),则 cmd2 不执行||
:逻辑或,若 cmd1 正确执行($?=0
),则 cmd2 不执行;若 cmd1 执行出现错误($?≠0
),则开始执行 cmd2- eg:
[[ 2 < 3 && 4 < 5 ]] && echo 'ok'
,使用 echo 显示前面的执行结果,若正确执行,则显示 ok ,否则啥都不显示
-
iii. 布尔运算符
-a
:按位与- 第一种方式:
[ $a -lt 20 -a $b -gt 100 ]
- 第二种方式:
[ $a -lt 20 ] && [ $b -gt 100 ]
- 第三种方式:
[[ $a -lt 20 && $b -gt 100 ]]
- 第一种方式:
-o
:按位或!
:非运算
-
iiii. 字符串运算符
= 或 ==
:检测两个字符串是否相等,eg:[ $a = $b ]
!=
:检测两个字符串是否不相等,eg:[ $a != $b ]
-
iiiii. 文件测试运算符
-d file
检测文件是否是目录,如果是,则返回 true。eg:[ -d file ] or test -d file
-f file
检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。eg:[ -f file ] or test -f file
-e file
检测文件(包括目录)是否存在,如果是,则返回 true。eg:[ -e file ] or test -e file
if [ ! -e bins/dra_bin ]; then # 若不存在则执行下面的操作
echo "make dirs: bins/dra_bin!"
mkdir -p bins/dra_bin
else
echo "bins/dra_bin exsits!"
fi
if [ ! -d "$hi_path" ]; then 若不存在则执行下面的操作
hi_path=../../hilib
fi
echo "hi_path is: $hi_path"
# shell 脚本的执行:bash start.sh