gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:27:46 25 4
gpt4 key购买 nike

我目前正在进行一项练习,该练习要求我编写一个 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/

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