码迷,mamicode.com
首页 > 系统相关 > 详细

shell批处理文件,并将运算结果返回

时间:2018-09-18 14:26:43      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:path   批处理文件   mod   linu   one   遍历文件夹   文件中   print   别人   

问题背景是这样的:别人用C++写了一个算法,算法内部比较复杂,但是呢,对于编译好的文件用起来比较方便,比如在linux终端,my_program 1.png 2.txt这样就可以用,但是这样只能够输入一张图片,有没有可能在shell里面写循环,然后每次都调用这个函数呢?

函数的原型是

./lens_distortion_correction_division_model_1p example/pattern.png pattern_canny.png pattern_hough.png pattern_corrected_image.png 0.8 0.0 1.0 3.0 2.0 primitives.txt
lens_distortion_correction_division_model_1p是可执行的文件, 后面是输入输出的参数,最后的primitives.txt程序最终输出的结果,这个txt文件中只有某一行是我想要的
直接上代码
 1 #!/bin/bash
 2 
 3 filenames=$(ls ./image_dataset/*.png)
 4 for file in ${filenames}
 5 do
 6 {
 7  echo ${file}
 8  image_name=${file%.*}  # 去掉文件名的后缀
 9  #echo $image_name
10  image_file_name=${image_name##*/}  # 去掉反斜杠前面多于的前缀, 得到裸的文件名
11  imagepath=./intern_image/
12  
13  ./lens_distortion_correction_division_model_1p ${file} ${imagepath}${image_file_name}_candy.png ${imagepath}${image_file_name}_hough.png ${imagepath}${image_file_name}_correct.png 0.8 0.0 4.0 3.0 2.0 primitives2.txt
14  a=$(sed -n "5p" primitives2.txt | awk {print $7}) # 获取畸变参数
15  echo $a  # 打印畸变参数
16  full_image_file_name=${file##*/}
17  echo $full_image_file_name $a >> rs.txt
18 }& #多线程加速
19 done
20 wait

最要的步骤是用$(ls ./image_dataset/*.png)来遍历文件夹下所有png后缀的文件,然后将这个文件作为我刚刚说到的编译好的文件的输入变量,然后对于输出的primitives2.txt获取特定的某一行,将这某一行连同文件名称保存到rs.txt文本下

在最后的中括号用&符号以及wait来进行多线程加速

shell批处理文件,并将运算结果返回

标签:path   批处理文件   mod   linu   one   遍历文件夹   文件中   print   别人   

原文地址:https://www.cnblogs.com/yongjieShi/p/9668508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!