我正在迭代用于创建数据文件 ID 的索引。当我创建文件名(data_file)并回显它时,它会正确打印,但是当我尝试将该变量写入所需的文件时,它只放置第一个索引。
# copy files
cp student_t_sampler.c student_t_sampler_copy.c
cp post_process.py post_process_copy.py
run_mcmc=1
n_samples=1000
n_thin=100
nu=3.
mu=1.
sig=1.
start_data_file=\"data_file
end_data_file=.txt\"
seed=1
alpha=1.
for i in "10. 1" "1. 2" "0.1 3" "0.01 4"; do # i is alpha, num
IFS=' ' read -a myarray <<< "$i"
alpha=${myarray[0]}
num=${myarray[1]}
data_file=$start_data_file$num$end_data_file
echo $data_file
if [ $run_mcmc == 1 ]; then
sed -i "s/nnn/$n_samples/g" student_t_sampler_copy.c # set number of samples, 1000
sed -i "s/ttt/$n_thin/g" student_t_sampler_copy.c # set thinning, 5
sed -i "s/nununu/$nu/g" student_t_sampler_copy.c # set nu
sed -i "s/mumumu/$mu/g" student_t_sampler_copy.c # set mu
sed -i "s/sigsigsig/$sig/g" student_t_sampler_copy.c # set sig
sed -i "s/fff/$data_file/g" student_t_sampler_copy.c # data file
sed -i "s/sss/$seed/g" student_t_sampler_copy.c # set seed
sed -i "s/aaa/$alpha/g" student_t_sampler_copy.c # set alpha
make ./student_t_sampler_copy
./student_t_sampler_copy
fi
n_bins=100
# echo "$data_file"
sed -i "s/fff/$data_file/g" post_process_copy.py # set data file in python
sed -i "s/bbb/$n_bins/g" post_process_copy.py # set number of bins
sed -i "s/aaa/$alpha/g" post_process_copy.py # set set in plot title
sed -i "s/iii/$num/g" post_process_copy.py # set set in plot title
python post_process_copy.py
done
wait # wait for all processes to finish
输出如下
"data_file1.txt"
gcc -I/usr/include -L/usr/local/lib student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
"data_file2.txt"
gcc -I/usr/include -L/usr/local/lib student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
"data_file3.txt"
gcc -I/usr/include -L/usr/local/lib student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
"data_file4.txt"
gcc -I/usr/include -L/usr/local/lib student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
因此,即使它回显正确的数据文件,python 和 c 都只接收 data_file1.txt
答案1
student_t_sampler_copy.c
您在脚本开始时和更新副本的循环之外从模板生成student_t_sampler.c
,因此您仅在循环的第一次迭代中更新副本,所有其他迭代没有其他需要更改的内容。您必须在循环内复制文件(紧接在 后for
)。