让终端通过bash for循环运行程序时出现问题
$i在程序参数行中似乎不起作用,但我得到了数据文件。这就是错误所在让终端通过bash for循环运行程序时出现问题,bash,loops,terminal,Bash,Loops,Terminal,$i在程序参数行中似乎不起作用,但我得到了数据文件。这就是错误所在 #!/bin/bash for i in {10..11} do ./duffing -a 1 -b -1 -u 0.25 -w -1 -A 0.4 -t $i | ./stroboscopic > $i.data done 这是我在duffing程序中解析参数的地方: error converting i to a double for max-time: leftover characters: i Ba
#!/bin/bash
for i in {10..11}
do
./duffing -a 1 -b -1 -u 0.25 -w -1 -A 0.4 -t $i | ./stroboscopic > $i.data
done
这是我在duffing程序中解析参数的地方:
error converting i to a double for max-time:
leftover characters: i
Bad input data
error converting i to a double for max-time:
leftover characters: i
Bad input data
我用-T10直接从终端运行程序,所以我很困惑为什么程序拒绝接受来自脚本的输入 我不认为这里的错误来自于外壳。它来自“duffing”脚本/程序中的代码。你能发布吗?用
set-x打开shell调试,你会看到$i
的占位符被扩展成{1..11}
,否则你会在那里输入错误或者duffing
;-)还有,你的意思是#/bin/bash
你说呢?祝你好运数据文件的名称是10.data
和11.data
(它们应该是这样的),还是它们的名称是i.data
(正如您在第一个命令中,$i
似乎扩展为i
),数据文件的名称是10.data和11.data。我设置了-x,发现shell无法读取“|”和“>”管道符号。如何让shell读取它们?您确定没有使用单引号或转义字符吗?这是你的真实剧本吗?
void parse_args(int argc, char **argv, state_t *state, system_t *system,
simulation_t *simulation, int *read_initial, int *print_final)
{
int ch;
duffing_state_t *duffing = (duffing_state_t *)system->dx_dt_state;
double dtemp;
size_t i;
while (1) {
ch = getopt_long(argc, argv, short_options, long_options, NULL);
if (ch == -1)
break;
switch(ch) {
case 'd':
simulation->dt = safe_strtod(optarg, "time-step");
break;
case 't':
simulation->t_max = safe_strtod(optarg, "max-time");
break;
case 'T':
duffing->T = safe_strtod(optarg, "transient-time");
break;
case 'x':
state->x[0] = safe_strtod(optarg, "x0");
break;
case 'v':
state->x[1] = safe_strtod(optarg, "v0");
break;
case 'm':
system->m = safe_strtod(optarg, "mass");
break;
case 'a':
duffing->a = safe_strtod(optarg, "alpha");
break;
case 'b':
duffing->b = safe_strtod(optarg, "beta");
break;
case 'u':
duffing->u = safe_strtod(optarg, "mu");
break;
case 'w':
duffing->w = safe_strtod(optarg, "omega");
break;
case 'A':
duffing->A = safe_strtod(optarg, "amplitude");
break;
case 'E':
simulation->step_fn = &euler_step;
break;
case 'M':
simulation->step_fn = &midpoint_step;
break;
case 'R':
simulation->step_fn = &rk4_step;
break;
case 'i':
*read_initial = 1;
break;
case 'f':
*print_final = 1;
break;
case '?':
exit(EXIT_FAILURE);
default:
fprintf(stderr, "?? getopt returned character code 0%o ??\n", ch);
}
}
/* convert input from periods to seconds */
simulation->t_max *= 2*M_PI / duffing->w;
duffing->T *= 2*M_PI / duffing->w;
return;
}