在 Linux shell 中, % 做什么,例如:
for file in *.png.jpg; do
mv "$file" "${file%.png.jpg}.jpg"
done
Run Code Online (Sandbox Code Playgroud)
Mar*_*ost 31
当%在模式被用来${variable%substring}将返回的内容variable与最短次数substring从后面删除variable。
此函数支持通配符模式 - 这就是它接受星号(星号)作为零个或多个字符的替代的原因。
应该提到的是,这是 Bash 特定的 - 其他 linux shell 不一定包含此功能。
如果您想了解有关 Bash 中字符串操作的更多信息,我强烈建议您阅读此页面。在其他方便的功能中,它 - 例如 - 解释了它的%%作用:)
编辑:我忘了提,当它在模式的二手$((variable%number))或$((variable1%$variable2))的%字符会模运算符的功能。DavidPostill 在他的回答中有更具体的文档链接。
当%在不同的上下文中使用,它应该被视为唯一的规则字符。
${parameter%word}${parameter%%word}这个词被扩展以产生一个模式,就像在文件名扩展中一样。如果模式匹配parameter扩展值的尾随部分,则扩展的结果是删除了最短匹配模式(案例)或最长匹配模式(案例)的参数值。如果参数是或模式移除操作依次应用于每个位置参数,扩展就是结果列表。如果参数是一个带有 或下标的数组变量
‘%’‘%%’‘@’‘*’,‘@’‘*’,模式移除操作依次应用于数组的每个成员,扩展就是结果列表。
通过试验,我发现当字符串用大括号(大括号)括起来时,会丢弃 % 之后的匹配项。
为了显示:
touch abcd # Create file abcd
for file in ab*; do
echo $file # echoes the filename
echo $file% # echoes the filename plus "%"
echo ${file%} # echoes the filename
echo "${file%}" # echoes the filename
echo
echo "${file%c*}" # Discard anything after % matching c*
echo "${file%*}" # * is not greedy
echo ${file%c*} # Without quotes works too
echo "${file%c}" # No match after %, no effect
echo $file%c* # Without {} fails
done
Run Code Online (Sandbox Code Playgroud)
这是输出:
abcd
abcd%
abcd
abcd
ab
abcd
ab
abcd
abcd%c*
Run Code Online (Sandbox Code Playgroud)
bash) 中,有什么作用%?for file in *.png.jpg; do
mv "$file" "${file%.png.jpg}.jpg"
done
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,%是模式匹配运算符(注意它也可以是模运算符)。
${var%$Pattern}, ${var%%$Pattern}
${var%$Pattern}从匹配的后端$var的最短部分中删除。$Pattern$var
${var%%$Pattern}从匹配的后端$var的最长部分中删除。$Pattern$var示例:参数替换中的模式匹配
Run Code Online (Sandbox Code Playgroud)#!/bin/bash # patt-matching.sh # Pattern matching using the # ## % %% parameter substitution operators. var1=abcd12345abc6789 pattern1=a*c # * (wild card) matches everything between a - c. echo echo "var1 = $var1" # abcd12345abc6789 echo "var1 = ${var1}" # abcd12345abc6789 # (alternate form) echo "Number of characters in ${var1} = ${#var1}" echo echo "pattern1 = $pattern1" # a*c (everything between 'a' and 'c') echo "--------------" echo '${var1#$pattern1} =' "${var1#$pattern1}" # d12345abc6789 # Shortest possible match, strips out first 3 characters abcd12345abc6789 # ^^^^^ |-| echo '${var1##$pattern1} =' "${var1##$pattern1}" # 6789 # Longest possible match, strips out first 12 characters abcd12345abc6789 # ^^^^^ |----------| echo; echo; echo pattern2=b*9 # everything between 'b' and '9' echo "var1 = $var1" # Still abcd12345abc6789 echo echo "pattern2 = $pattern2" echo "--------------" echo '${var1%pattern2} =' "${var1%$pattern2}" # abcd12345a # Shortest possible match, strips out last 6 characters abcd12345abc6789 # ^^^^ |----| echo '${var1%%pattern2} =' "${var1%%$pattern2}" # a # Longest possible match, strips out last 12 characters abcd12345abc6789 # ^^^^ |-------------| # Remember, # and ## work from the left end (beginning) of string, # % and %% work from the right end. echo exit 0
源参数替换
%modulo 或 mod(返回整数除法运算的余数)
Run Code Online (Sandbox Code Playgroud)bash$ expr 5 % 3 25/3 = 1,余数为 2
来源运营商
| 归档时间: |
|
| 查看次数: |
6295 次 |
| 最近记录: |