Bash 的基本语法
分号
分号;
是命令的结束符,使得一行可以放置多个命令,上一个命令执行结束后,再执行第二个命令。
$ clear; ls
命令的分组和合并
Shell 允许将多个命令分组和合并执行。
Command1 ; Command2
上面命令是,执行完Command1
,不管是否成功,再继续执行Command2
。
Command1 && Command2
上面命令是,如果Command1
命令运行成功,则继续运行Command2
命令。
Command1 || Command2
上面命令是,如果Command1
命令运行失败,则继续运行Command2
命令。
下面是一些例子。
$ cat filelist.txt ; ls -l filelist.txt
$ cat filelist.txt && ls -l filelist.txt
$ mkdir foo || mkdir bar
if 结构
if
结构用于条件判断,符合条件时,才会执行指定的命令。它的语法如下。
if commands; then
commands
[elif commands; then
commands...]
[else
commands]
fi
这个命令分成三个部分:if
、elif
和else
。其中,后两个部分是可选的。if
后面是判断的条件,如果不成立,并且存在elif
部分,就会进行elif
判断;如果还不成立,并且存在else
部分,则会执行else
代码块。
if test $USER = "foo"; then
echo "Hello foo."
else
echo "You are not foo."
fi
上面的例子中,if test
判断环境变量$USER
是否等于foo
,如果等于就输出Hello foo.
,否则输出其他内容。
if
结构可以写在一行内。
$ if true; then echo "It's true."; fi
It's true.
$ if false; then echo "It's true."; fi
上面的例子中,true
和false
是两个特殊命令,前者代表操作成功,后者代表操作失败。
if
结构写成下面两种形式,也是可以的。
# 形式一
if [ -f .bash_profile ]
then
echo ".bash_profile 文件存在"
else
echo ".bash_profile 文件不存在"
fi
# 形式二
if [ -f .bash_profile ]
then echo ".bash_profile 文件存在"
else echo ".bash_profile 文件不存在"
fi
test 命令
if
结构往往与test
命令一起使用,有两种形式。
# 第一种形式
test 表达式
# 第二种形式
[ 表达式 ]
注意,第二种形式之中,[
和]
与内部的表达式之间都必须有空格。
if test -e filename
判断一个文件是否存在。
if test -e /tmp/foo.txt ; then
echo "Found foo.txt"
fi
上面的命令也可以写成下面的形式。
if [ -e /tmp/foo.txt ] ; then
echo "Found foo.txt"
fi
test
命令的参数如下。
-d file
:如果file
为目录,返回true
。-e file
:如果file
文件存在,返回true
。-f file
:如果file
文件存在,并且为一个常规文件,返回true
。-L file
:如果file
是一个符号链接,返回true
。-r file
:如果file
是一个文件,且用户有读权限,返回true
。-w file
:如果file
是一个文件,且用户有写权限,返回true
。-x file
:如果file
是一个文件,且用户有执行权限,返回true
。file1 -nt file2
:如果file1
比file2
更新(根据修改时间),返回true
。file1 -ot file2
:如果file1
比file2
更旧(根据修改时间),返回true
。-z string
:如果string
是一个空字符串,返回true
。-n string
:如果string
是一个非空字符串,返回true
。string1 = string2
:如果string1
与string2
相等,返回true
。string1 != string2
:如果string1
与string2
不相等,返回true
。
for 循环
foo 循环用于命令的重复执行。
for i in *.png; do
ls -l $i
done
函数
Bash 允许自定义函数,便于代码的复用。函数定义的语法如下。
funcname(){ ... }
下面是一个简单函数的例子。
hello() { echo "Hello $1"; }
上面代码中,函数体里面的$1
表示命令行的第一个参数。
调用方法如下。
$ hello world
hello world
数学运算
$((expression))
可以计算一个数学表达式。
$ echo $((2 + 2))
4
$ echo $((5/2))
2
$ echo $((5%2))
1
$ echo $(((5**2) * 3))
75
# 等同于
$ echo $(($((5**2)) * 3))
75
它支持的运算符如下。
+
加法-
减法*
乘法/
除法(整除,即商为整数)%
余数**
指数
子命令
一个 Bash 命令之中,可以使用$()
加入另一个命令的运行结果。
$ ls -l $(which cp)
上面命令之中,先运行which cp
(返回cp
命令的文件路径),然后显示这个文件的详细信息。
还有另一种较老的语法,使用反引号运行子命令,等同于$()
。
$ ls -l `which cp`
看完两件小事
如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:
- 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
- 关注公众号 「IT平头哥联盟」,公众号后台回复「资源」 免费领取我精心整理的前端进阶资源教程