Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
传递要转换的.jpgs的.txt列表(bash)_Bash_Shell - Fatal编程技术网

传递要转换的.jpgs的.txt列表(bash)

传递要转换的.jpgs的.txt列表(bash),bash,shell,Bash,Shell,我目前正在做一个练习,要求我编写一个shell脚本,其功能是接受一个命令行参数,即目录。脚本获取给定的目录,并查找该目录及其子目录中的所有.jpgs,然后按照修改时间的顺序(最新的在底部)创建所有.jpgs的图像条 到目前为止,我写了: #!bin/bash/ dir=$1 #the first argument given will be saved as the dir variable #find all .jpgs in the given directory #then ls i

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

到目前为止,我写了:

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


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

xargs
是您的朋友:

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

将文件名列表放入名为
filelist.txt
的文件中,然后调用
convert
,文件名前面有一个符号和:

convert @filelist.txt -append result.jpg
这里有一个小例子:

# Create three blocks of colour
convert xc:red[200x100]    red.png
convert xc:lime[200x100]   green.png
convert xc:blue[200x100]   blue.png

# Put their names in a file called "filelist.txt"
echo "red.png green.png blue.png" > filelist.txt

# Tell ImageMagick to make a strip
convert @filelist.txt +append strip.png

因为总是有一些图像的名称中有一个讨厌的空格

# Make the pesky one
convert -background black -pointsize 128 -fill white label:"Pesky" -resize x100 "image with pesky space.png"

# Whack it in the list for IM
echo "red.png green.png blue.png 'image with pesky space.png'" > filelist.txt

# IM do your stuff
convert @filelist.txt +append strip.png

顺便说一句,如果文件名中有空格,解析
ls
的输出通常是不好的做法。如果您想跨目录查找图像列表并按时间排序,请查看以下内容:

# Find image files only - ignoring case, so "JPG", "jpg" both work
find . -type f -iname \*.jpg

# Now exec `stat` to get the file ages and quoted names
... -exec stat --format "%Y:%N {} \;

# Now sort that, and strip the times and colon at the start
... | sort -n | sed 's/^.*://'

# Put it all together
find . -type f -iname \*.jpg -exec stat --format "%Y:%N {} \; | sort -n | sed 's/^.*://'
find ...as above... > file list.txt
convert @filelist +append strip.jpg
现在,您可以将所有内容重定向到
filelist.txt
并调用
convert
,如下所示:

# Find image files only - ignoring case, so "JPG", "jpg" both work
find . -type f -iname \*.jpg

# Now exec `stat` to get the file ages and quoted names
... -exec stat --format "%Y:%N {} \;

# Now sort that, and strip the times and colon at the start
... | sort -n | sed 's/^.*://'

# Put it all together
find . -type f -iname \*.jpg -exec stat --format "%Y:%N {} \; | sort -n | sed 's/^.*://'
find ...as above... > file list.txt
convert @filelist +append strip.jpg
或者,如果您希望避免使用中间文件并一次性完成所有操作,您可以制作此怪物,其中
convert
从其标准输入流读取文件列表:

find ...as above... | sed 's/^.*://' | convert @- +append strip.jpg

谢谢你的提示!我已经查看了手册页,看起来这就是我想要做的。现在创建了一个名为IMAGESTRIP.jpg的文件,但是它只显示1个图像,而不是我需要附加的8个图像。我想我可以尝试一下while循环或类似的方法来将所有图像附加在一起……但是那一行越来越长了。是吗有什么方法可以将这一长行拆分为几行,以便于阅读?我对答案进行了一些解释。关于您的评论,它应该显示所有,可能不是所有的文件都是由xargs添加的,您可以通过替换convert by
echo
来确认它,以查看正在执行什么。关于拆分行,您可以然后继续下一行。列出了我想要制作条带的所有.jpg,但当我使用convert命令时,脚本只生成了最后一个/最新的一个。这可能是一个转换问题,请检查
convert
手册以查看您是否正确使用它。这非常有效!谢谢!问题:我为什么需要“@”签名以便将.txt文件作为参数?这只是语法,否则它会假定该文件是一个图像,而不是一个图像文件名列表。好吧,我说得太快了。我运行了一次脚本,它产生了我所期望的结果。我再次运行该脚本,为最终完成的工作挤出一些额外的多巴胺,但现在一直如此生成三个图像条中的一个图像条(因此图像1-8附加到图像1-8,再次附加到图像1-8,所有结果都是.jpg)。为什么它突然循环它?还有一个问题:我如何将.jpg的“result.jpg”名称设置为命令行参数的任意值,但名称中使用“\u”而不是“/”(例如:Exercise3/Test1/products Exercise3_Test1.jpg)我现在不在我的计算机上测试…也许可以尝试在文件名之间使用空格,而不是换行符。同样的结果。不过也不着急。无论如何,我感谢您的知识!