Skip to Content
bash shell脚本编程经典实例 (第2版)
book

bash shell脚本编程经典实例 (第2版)

by Carl Albing, JP Vossen
January 2021
Intermediate to advanced
581 pages
15h 7m
Chinese
Posts & Telecom Press
Content preview from bash shell脚本编程经典实例 (第2版)
脚本编程基础:
shell
变量
97
5.16.3
 讨论
如上所示,用于替换的值并不一定非得是字符串常量。它可以是更为复杂的
shell
表达式
的结果,其中包括在子
shell
中运行的命令(如上述示例所示)。如果
$BASE
不存在,那么
shell
会运行内建命令
pwd
(以获得当前目录),并使用其返回的字符串。
那么,我们可以在该运算符(以及其他类似运算符)右侧做些什么?
bash
手册页对于出现
在运算符右侧的内容是这么表述的:“……要经过波浪号扩展、参数扩展、命令替换以及
算术扩展”。
具体含义如下。
参数扩展
意味着可以使用其他变量,如
${BASE:=${HOME}}
波浪号扩展
意味着可以使用
~bob
这样的表达式,它会扩展成用户
bob
的主目录。可以
通过
${BASE:=~uid17}
将默认值设置为用户
uid17
的主目录,但注意不要给
~uid17
加引号,
因为波浪号扩展不会在引号中执行。
该示例用到的就是
命令替换
,它将命令的输出结果作为变量的值,其语法为
$(
cmds
)
算术扩展
意味着可以用
$(( ))
语法执行整数算术运算。例如:
echo ${BASE:=/home/uid
$((
ID+1
))
}
5.16.4
 参考
5-1
2.15
5.13
6.1
5.17
 对不存在的参数输出错误消息
5.17.1
 问题
这些能够设置默认值的便捷写法都挺酷,但有时你需要强制用户指定某个值,否则就无法
继续往下进行。用户有可能会遗漏某个参数,因为他们确实不知道该怎样调用脚本。你希
望能给用户点提示,省得他们自己瞎猜。相较于堆砌成堆的 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

bash 网络安全运维

bash 网络安全运维

Paul Troncone, Carl Albing
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9787115553782