查找并替换 for 循环 shell
Find and replace for loop shell
使用下面的命令我的脚本成功执行。
for file in collectorlist* ; do mv "$file" "${/bak_/{file}}" ; done
现在我想将 bak_{file} 移动到正在重置为原始文件名的 {file}。我尝试了以下方法,抛出错误。感谢您的建议。
for file in bak_collectorlist*; do mv "$file" "${/bak_/{file}}" ; done
您需要将第二个 运行 中的文件名从 bak 反转为原始
for file in bak_collectorlist*; do mv "./bak_/${file}" "$file" ; done
使用:
for file in bak_collectorlist*; do mv "$file" "${file/bak_/}" ; done
来自 bash 人:
${parameter/pattern/string
} Pattern substitution
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pattern
against its value is replaced with string. If pattern begins with /,
all matches of pattern are replaced with string. Normally only the
first match is replaced. If pattern begins with #, it must match
at the beginning of the expanded value of parameter. If pattern
begins with %, it must match at the end of the expanded value of
parameter. If string is null, matches of pattern are deleted and the
/ following pattern may be omitted. If parameter is @ or *, the
substitution 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 substitution operation is
applied to each member of the array in turn, and the expansion is the
resultant list
.
演示
$ ll bak_collectorlist*
-rw-r----- 1 klashxx klashxx 2 Jun 19 09:13 bak_collectorlist
-rw-r----- 1 klashxx klashxx 2 Jun 19 09:13 bak_collectorlist2
$
$ for file in bak_collectorlist*;do echo "$file" "${file/bak_/}" ; done
bak_collectorlist collectorlist
bak_collectorlist2 collectorlist
使用下面的命令我的脚本成功执行。
for file in collectorlist* ; do mv "$file" "${/bak_/{file}}" ; done
现在我想将 bak_{file} 移动到正在重置为原始文件名的 {file}。我尝试了以下方法,抛出错误。感谢您的建议。
for file in bak_collectorlist*; do mv "$file" "${/bak_/{file}}" ; done
您需要将第二个 运行 中的文件名从 bak 反转为原始
for file in bak_collectorlist*; do mv "./bak_/${file}" "$file" ; done
使用:
for file in bak_collectorlist*; do mv "$file" "${file/bak_/}" ; done
来自 bash 人:
${parameter/pattern/string
} Pattern substitutionThe pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the substitution 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 substitution operation is applied to each member of the array in turn, and the expansion is the resultant list
.
演示
$ ll bak_collectorlist*
-rw-r----- 1 klashxx klashxx 2 Jun 19 09:13 bak_collectorlist
-rw-r----- 1 klashxx klashxx 2 Jun 19 09:13 bak_collectorlist2
$
$ for file in bak_collectorlist*;do echo "$file" "${file/bak_/}" ; done
bak_collectorlist collectorlist
bak_collectorlist2 collectorlist