在循环bash脚本中执行程序

时间:2014-07-31 08:55:00

标签: bash for-loop executable

我想多次运行可执行文件run_ex来改变一些参数(文本文件),但我是bash脚本的新手,我无法弄清楚如何做到这一点......

#! /bin/bash
for ((hour=1; hour <= 9 ; hour++))
do
   printf "run executable for hour %d \n" $hour

   parameter1 ="/path1/file1_$hour.txt" 

   parameter2 = "/path2/file2_$hour.txt" 

   ./run_ex $parameter1 $parameter2

done

由于

2 个答案:

答案 0 :(得分:3)

变量赋值的=周围不允许

空格

  parameter1 ="/path1/file1_$hour.txt" 
  #         ^

改为写下:

  parameter1="/path1/file1_$hour.txt" 
  parameter2="/path2/file2_$hour.txt" 

答案 1 :(得分:1)

关注Sylvain Leroux's answer,您还应该将变量放在双引号内以防止分词和意外的路径名扩展:

#!/bin/bash
for ((hour=1; hour <= 9 ; hour++))
do
    printf "run executable for hour %d \n"" $hour"

    parameter1="/path1/file1_$hour.txt" 
    parameter2="/path2/file2_$hour.txt" 

   ./run_ex "$parameter1" "$parameter2"
done

同样使用大括号扩展,您可以将for ((hour=1; hour <= 9 ; hour++))简化为for hour in {1..9}; do

请参阅Word SplittingPathname or Filename Expansion