使用WHILE循环而不是FOR循环重命名文件

科学9

考虑一下,我们有很多名称为的照片DSC_20170506_170809.JPEG为了重命名照片,使它们遵循模式Paris_20170506_170809.JPEG,我编写了下面的脚本,效果很好。

for file in *.JPEG; do mv ${file} ${file/DSC/Paris}; done

我的问题是,我们如何使用while循环而不是循环来编写此脚本for

don_crissti

while这里使用循环没有错您只需要正确地做一下即可:

set -- *.jpeg
while (($#)); do
mv -- "${1}" "${1/DSC/Paris}"
shift
done

while上面循环与for循环一样可靠(它将与任何文件名一起使用),而在许多情况下,后者是最适合使用的工具,而前者是一种有效的替代方法1,它有其用途(例如,以上可以一次处理三个文件,也可以仅处理一定数量的参数,等等)。


所有这些命令(setwhile..do..doneshift)都记录在外壳手动和他们的名字是不言自明的...

set -- *.jpeg
# set the positional arguments, i.e. whatever that *.jpeg glob expands to

while (($#)); do
# execute the 'do...' as long as the 'while condition' returns a zero exit status
# the condition here being (($#)) which is arithmetic evaluation - the return
# status is 0 if the arithmetic value of the expression is non-zero; since $#
# holds the number of positional parameters then 'while (($#)); do' means run the
# commands as long as there are positional parameters (i.e. file names)

mv -- "${1}" "${1/DSC/Paris}"
# this renames the current file in the list

shift
# this actually takes a parameter - if it's missing it defaults to '1' so it's
# the same as writing 'shift 1' - it effectively removes $1 (the first positional
# argument) from the list so $2 becomes $1, $3 becomes $2 and so on...
done

1:它不能替代文本处理工具,因此切勿使用while循环来处理文本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用for循环批量重命名文件

来自分类Dev

使用for循环重命名pdf文件列表

来自分类Dev

使用循环重命名多个文件

来自分类Dev

对于使用从txt文件读取的行的循环重命名文件

来自分类Dev

使用foreach循环在Powershell中批量重命名文件

来自分类Dev

使用R循环进行文件下载和重命名

来自分类Dev

使用循环创建副本并重命名文件

来自分类Dev

使用foreach循环在Powershell中批量重命名文件

来自分类Dev

使用foreach循环重命名文件夹匹配模式

来自分类Dev

重命名重复名称的While循环

来自分类Dev

使用while循环而不是for循环

来自分类Dev

重命名文件Bash脚本-循环问题

来自分类Dev

VBScript继续循环以重命名文件

来自分类Dev

Maya Python:使用for循环重命名对象

来自分类Dev

使用Jython在循环中重命名变量

来自分类Dev

使用循环重命名 R 中的字段

来自分类Dev

在 Python 中使用 while 循环而不是 for 循环

来自分类Dev

使用游标而不是While循环

来自分类Dev

使用异步任务读取文件流,而不是无尽的while循环

来自分类Dev

使用for循环python重命名目录中的文件名

来自分类Dev

For循环复制粘贴文件夹并使用增量对其重命名

来自分类Dev

使用正则表达式在for循环中重命名bash中的文件

来自分类Dev

从数组列表重命名文件内容的批量循环

来自分类Dev

循环两个文件(重命名和移动)

来自分类Dev

如何批量添加attrib到FOR循环中的重命名文件

来自分类Dev

在foreach循环中输出时如何重命名文件

来自分类Dev

使用break而不是while(condition)的While循环

来自分类Dev

while循环-完成<命令而不是完成<文件

来自分类Dev

用于循环重命名文件的For循环,并提示每个文件名

Related 相关文章

热门标签

归档