gpt4 book ai didi

linux - 这个 bash 文件名提取技术的用途?

转载 作者:IT王子 更新时间:2023-10-29 00:09:02 24 4
gpt4 key购买 nike

我有一部分 bash 脚本正在获取不带扩展名的文件名,但我试图了解这里到底发生了什么。 “%%”是干什么用的?有人可以详细说明 bash 在幕后做什么吗?如何在一般基础上使用这种技术?

#!/bin/bash

for src in *.tif
do
txt=${src%%.*}
tesseract ${src} ${txt}
done

最佳答案

它去掉了文件扩展名(此处:.tif),示例:

$ for A in test.py test.sh test.xml test.xsl; do echo "$A: ${A%%.*}"; done
test.py: test
test.sh: test
test.xml: test
test.xsl: test

来自 bash 手册:

   ${parameter%%word}
The word is expanded to produce a pattern just as in pathname expansion. If the
pattern matches a trailing portion of the expanded value of parameter, then the
result of the expansion is the expanded value of parameter with the shortest
matching pattern (the ``%'' case) or the longest matching pattern (the ``%%''
case) deleted. If parameter is @ or *, the pattern removal operation is applied
to each positional parameter in turn, and the expansion is the resultant list.
If parameter is an array variable subscripted with @ or *, the pattern removal
operation is applied to each member of the array in turn, and the expansion is
the resultant list.

关于linux - 这个 bash 文件名提取技术的用途?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/538504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com