Bash使用$ {variable}将参数传递给C代码

时间:2016-06-19 17:40:26

标签: bash variables arguments

我的bash脚本无法正常工作,因为它没有将$ {var}转换为其值。

我的代码:

#!/bin/bash

make huffmanTree.exe
file="timesSeq.txt"
echo "Times Huffman Tree Sequential" > ${file}
echo "" >> ${file}

function fn_calculate_times {
    for i in ${folder}/*.*
    do
        echo ${i} times >> ${file}
        avg=0
        for j in {1..5}
        do
            start=`date +%s`
            echo "${folder}/${i}"
            ./huffmanTree.exe "$folder/$i"
            end=`date +%s`
            echo "    time = `expr $end - $start` s >> ${file}"
            avg=`expr $avg + $end - $start`
        done;
        echo "    average = `expr $avg / $j`"
    done;
    echo "" >> ${file}
}

echo "TXT's" >> ${file}
folder="test-texts"
fn_calculate_times

echo "IMG's" >> ${file}
folder="test-images"
fn_calculate_times

我的输出(多次):

  

测试图像/测试图像/ IMG-输入200MB.jpg
  错误:文件无法打开。
      time = 0 s>> timesSeq.txt
      平均= 0左右

好像$ folder / $ i似乎没有被翻译成test-texts / text-name.txt
- 脚本的其余部分似乎有效 - 我已尝试将{}用于$ {folder} / $ {i)。相同的结果。
- 我在Mac上。

我错过了什么?

//求助:阅读评论。

1 个答案:

答案 0 :(得分:0)

发现问题了!当我认为它只是“*。*”因此传递参数为$ folder / $ i重复$ folder时,$文件夹/*.*中的$ i占用整个var“$ folder /*.*”部分呵呵。

正确的代码:

#!/bin/bash

make huffmanTree.exe
file="timesSeq.txt"
echo "Times Huffman Tree Sequential" > ${file}
echo "" >> ${file}

function fn_calculate_times {
    for i in ${folder}/*.*
    do
        echo ${i} times >> ${file}
        avg=0
        for j in {1..5}
        do
            start=`date +%s`
            echo "${folder}/${i}"
            ./huffmanTree.exe "$i"
            end=`date +%s`
            echo "    time = `expr $end - $start` s >> ${file}"
            avg=`expr $avg + $end - $start`
        done;
        echo "    average = `expr $avg / $j`"
    done;
    echo "" >> ${file}
}

echo "TXT's" >> ${file}
folder="test-texts"
fn_calculate_times

echo "IMG's" >> ${file}
folder="test-images"
fn_calculate_times