bash - 传递 .jpg 的 .txt 列表进行转换 (bash)

标签 bash shell

我目前正在进行一项练习,该练习要求我编写一个 shell 脚本,该脚本的功能是采用单个命令行参数,即一个目录。该脚本获取给定的目录,找到该目录及其子目录中的所有 .jpg,并按照修改时间(最新的在底部)的顺序创建所有 .jpg 的图像条。

到目前为止,我已经写了:

#!bin/bash/

dir=$1 #the first argument given will be saved as the dir variable


#find all .jpgs in the given directory
#then ls is run for the .jpgs, with the date format %s (in seconds)
#sed lets the 'cut' process ignore the spaces in the columns
#fields 6 and 7 (the name and the time stamp) are then cut and sorted by modification date
#then, field 2 (the file name) is selected from that input
#Finally, the entire sorted output is saved in a .txt file

find "$dir" -name "*.jpg" -exec ls -l --time-style=+%s {} + | sed 's/  */ /g' | cut -d' ' -f6,7 | sort -n | cut -d' ' -f2 > jgps.txt

脚本按时间修改顺序正确输出目录的 .jpg。我目前正在努力的部分是如何将 .txt 文件中的列表提供给 convert -append 命令,这将为我创建一个图像条(对于那些不知道的人该命令,将输入的是: convert -append image1.jpg image2.jpg image3.jpg IMAGESTRIP.jpg其中 IMAGESTRIP.jpg 是由前 3 个组成的完整图像条文件的名称图片)。

我不太明白如何将 .txt 文件列表及其路径传递给此命令。我一直在搜索手册页以找到可能的解决方案,但没有出现可行的解决方案。

最佳答案

xargs 是你的 friend :

find "$dir" -name "*.jpg" -exec ls -l --time-style=+%s {} + | sed 's/  */ /g' | cut -d' ' -f6,7 | sort -n | cut -d' ' -f2 | xargs -I files convert -append files IMAGESTRIP.jpg

解释

xargs 的基本用法是:

find . -type f | xargs rm

也就是说,您向 xargs 指定一个命令,它会附加从标准输入接收到的参数,然后执行它。 avobe 行将执行:

rm file1 file2 ...

但是你还需要指定命令的最后一个参数,所以你需要使用 xarg -I 参数,它告诉 xargs 你将在后面使用的字符串来指示参数从哪里读取将放置标准输入。

因此,我们使用字符串files 来表示它。然后我们编写命令,将字符串 files 放在变量参数所在的位置,结果是:

xargs -I files convert -append files IMAGESTRIP.jpg

关于bash - 传递 .jpg 的 .txt 列表进行转换 (bash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32915841/

相关文章:

regex - Linux用bash脚本重命名多个文件+添加后缀

bash - Shell 命令 SET -X 在脚本中的意思

bash - 增量创建目录,将文件移动到目录中 - 循环

Bash 脚本进入正在运行的容器,然后从该容器运行另一个 bash 脚本

java - bash 命令在 tomcat/java Runtime.getRuntime.exec() 中失败,但可以从命令行运行

bash - 嵌套的 EOF

linux - 如何在 bash 中复制字符串?

bash - 右文本对齐 - bash

perl - 是否可以编写一个比 Perl 中的等效脚本更快的 shell 脚本?

bash - 在AWK中调用system()时出错