c - 从 bash 运行时,for 循环在 c 文件中被跳过

标签 c bash text-files

我正在运行一个生成数据并将其写入文本文件的 c 文件。下面是一个 bash 脚本,它针对不同的参数多次运行该文件。当我自己运行 c 代码时,没有任何问题。当我使用下面的脚本运行它时,没有数据保存到文本文件中(它们仍然被创建)。似乎是打印到文本文件所在的 for 循环被跳过了(通过在此 for 循环前后放置 print 语句来确定)。

#!/bin/bash 

make studentt_sampler

# arguments to pass to ./studentt_sampler
no_samples=5
nu=3.0
mu=1.0
sigma=1.0
data_files=("data_file_0p01.txt" "data_file_0p1.txt" "data_file_1.txt" "data_file_10.txt")
proposal_sigma=(0.01,0.1,1.0,10.0)

# arguments to pass to post_process.py
figure_files=("chain_0p01.pdf" "chain_0p1.pdf" "chain_1.pdf" "chain_10.pdf")


for i in `seq 0 3`;
do
    #echo ${data_files[$i]}
    ./studentt_sampler no_samples nu mu sigma "${data_files[$i]}" "${proposal_sigma[$i]}" &
    # ./post_process.py echo ${data_files[$i]} echo ${figure_files[$i]}

done

wait

c文件的主要作用是

int main(int argc, char *argv[]) {
    /* Initialization */
    const gsl_rng_type * T;
    gsl_rng * r;


    /* set iteration variables and the order of the student-t distribution
     * from the command line arguments */
    int i, itr = atoi(argv[1]);

    /* parameters of student t distributions */
    double nu = atof(argv[2]); 
    double mu = atof(argv[3]);
    double sigma = atof(argv[4]);

    /* store the parameters in param_type struct */
    struct param_type params = {nu,mu,sigma};

    /* open text file for writing  and make sure it works*/

    printf("%s\n",argv[5]);
    FILE *f = fopen(argv[5], "w"); 

    if (f == NULL) {
        printf("Error opening file!\n");
        exit(1);
    }

    /* allocate memory for generator and set its seed */
    r = gsl_rng_alloc(gsl_rng_mt19937); 
    gsl_rng_set(r,1); 

    /* Start initial value */
    double x_cur = 1.0; 
    double proposal_sigma = atof(argv[6]);
    double alpha;
    double x_prop;
    int accept; /* keep track of acceptance rate */
    double u; /* to make decision of accept proposal or not */
    double accept_prob;

    /* Start the MCMC */
    for (i=0;i<itr;i++) {
        printf("here!!!\n");
        /* propose a new x */
        x_prop = gsl_ran_gaussian(r,proposal_sigma) + x_cur;

        /* Calculate acceptance probability */
        accept_prob = lklhood(x_prop, &params)/lklhood(x_cur, &params);
        alpha = GSL_MIN(1.0,accept_prob);

        /* Accept or not, decide */
        u = gsl_ran_flat(r,0.0,1.0);
        if (u < alpha) {
            x_cur = x_prop;
            accept = 1;
        }/* print to data file */
        else {
            accept = 0;
        }
        fprintf(f," %.5f %i\n",x_cur,accept);
    } 

    /* Clean up time */
    fclose(f);


    return 0;
}

感谢您的帮助。

最佳答案

您是否可能忘记了您提供给程序的变量中的 $,即它不应该是:

./studentt_sampler $no_samples $nu $mu $sigma "${data_files[$i]}" "${proposal_sigma[$i]}" &

关于c - 从 bash 运行时,for 循环在 c 文件中被跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40880507/

相关文章:

CSV 与 TXT 文件中的 Java BufferedReader 行为

c - 如何接受一组数字,如 {301,102,99,202,198,103} 并丢弃 ~100?

linux - socket编程问题ipv6+udp

git - git 中的 ANSI 颜色显示不正确

linux - Bash 脚本中的数组长度为 1

linux - 读取文本文件的系统调用

php - 将 .txt 文件的最后 25 行发布到网站?

c - 我怎样才能在暴力攻击中最好地使用 "parallelise"一组四个嵌套的 for() 循环?

c - 在 STM32 - C 上处理图像

linux - 如何删除超过 X 天的文件并打印已删除文件的列表