Bash脚本无法使用字符串变量,可以直接在终端中正常运行

时间:2019-10-25 21:36:23

标签: bash imagemagick

我对Bash比较陌生。我编写了一个脚本,使用imagemagick的montage实用程序通过从文本文件中读取png文件列表来生成图像的蒙太奇:

IFS=$'\n'
count=1
for line in `cat pngListGr4`;
do
   montage -tile 4x0 $line $(printf "%03d" $count).png
   ((count=count + 1))
  done
unset IFS 

其中pngListGr4文件如下:

01.png 02.png 03.png 04.png
05.png 06.png 07.png 08.png
...

因此,我希望生成文件001.png和002.png作为文件1-4和5-8的蒙太奇。 但是相反,我遇到了错误:

  

montage-im6.q16:缺少图像文件名“ 001.png” @ error / montage.c / MontageImageCommand / 1795。

但是以下代码在终端中可以正常工作:

$ string_="01.png 02.png 03.png 04.png"
$ montage -tile 4x0 $string_ $(printf "%03d" $count).png

为什么我在bash脚本中使用字符串替换会出现此类问题?

1 个答案:

答案 0 :(得分:1)

您正在将IFS设置为if(Math.sqrt(Math.pow((this.X-Victim.X),2),Math.pow((this.Y-Victim.Y),2)) <= 100){ var angle = Math.atan2(this.X - Victim.X , this.Y - Victim.Y) * (180 / Math.PI); var flippedangle = 180 - angle; var fixedangle = (flippedangle + 360) % 360; var fixedlook = (this.LookAngle + 360) % 360; if(((fixedangle - (fixedlook + 80)) < 0 ? fixedangle - (fixedlook + 80) + 360 : fixedangle - (fixedlook + 80)) > (((fixedlook - 80) - (fixedlook + 80)) < 0 ? (fixedlook - 80) - (fixedlook + 80) + 360 : (fixedlook - 80) - (fixedlook + 80))){ socket.emit('HitTo',{UserName:Victim.UserName,hitval:20}); } } 来尝试读取行,但这意味着\n将不再在$string_命令中的空格处中断。更好的解决方案是使用montage循环,它既可以获取行,又可以将它们拆分为字段:

while read