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
变量
105
5.23.4
 参考
Cameron Newham
所著的
Learning the bash Shell, 3rd Edition
O
Reilly
出版),
157~161
页,
其中讲述了有关数组的更多信息
7.15
13.4
5.24
 转换大小写
5.24.1
 问题
数码相机里全是一堆大写字母命名的文件,如
IMG0001.JPG
。你想将文件名改成小写,但
又不愿意挨个重新输入。
5.24.2
 解决方案
bash 4.0
中的几个运算符可以在引用变量名时转换其大小写。如果变量
$FN
中包含一个需
要转换成小写的文件名(字符串),那么
${FN,,}
会返回全部是小写形式的字符串。与此类
似,
${FN^^}
会返回全部是大写形式的字符串。甚至还有
${FN~~}
,它可以切换大小写,将
所有的小写字母转换成大写,大写字母转换成小写。
以下的
for
循环会将所有
.JPG
类型的文件名更改成小写字母。
for
FN in *.JPG
do
mv "$FN" "${FN,,}"
done
或者写成单行脚本:
for
FN in *.JPG;
do
mv "$FN" "${FN,,}" ;
done
bash 4
或更高版本中还有另外一种方法:你可以将变量声明成始终为小写字母的类型。赋
给该变量的任何文本都会转换成小写。要想在
for
循环中使用这种方法来重命名文件,使
用简单的赋值即可,无须任何字符串操作。
declare -l lcfn # 变量内容会转换成小写字母
for
FN in *.JPG ...
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