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版)
86
5
5.7.3
 讨论
其他参数的可用形式分别为
${2}
${3}
${4}
${5}
等。单个数位的数字用不着花括号,
除非要区分变量名与其后出现的文本。典型的脚本只用到少部分参数,但如果涉及
${10}
那就得使用花括号了,否则
shell
会将
$10
理解为
${1}
后面紧跟着字符串
0
,如下所示。
$
cat tricky.sh
echo $1 $10 ${10}
$
./tricky.sh I II III IV V VI VII VIII IX X XI
I I0 X
$
10
个参数的值是
X
,但如果在脚本中写成
$10
,那么你在
echo
语句中得到的会是第一个
参数
$1
,后面紧跟着一个字符串
0
5.7.4
 参考
5.4
5.8
 遍历传入脚本的参数
5.8.1
 问题
你想对指定的一系列参数执行某些操作。编写
shell
脚本,对单个参数进行处理不是什么问
题,只需要用
$1
引用这个参数即可。但如果面对的是一大批文件呢?你可能想这样调用
脚本。
./actall *.txt
shell
会进行模式匹配,生成匹配
*.txt
模式(以
.txt
结尾的文件名)的文件名列表。
5.8.2
 解决方案
特殊的
shell
变量
$*
能够引用所有的参数,可以将其用于
for
循环,如例
5-2
所示。
5-2
 
ch05/chmod_all.1
#!/usr/bin/env bash
# 实例文件:chmod_all.1
#
# 批量修改文件权限
#
for
FN in $*
do
echo changing $FN
chmod 0750 ...
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