如何在终端提示符(bash)中使用变量来修改文件名?

How to use variables in terminal prompt (bash) to modify files' names?

我想要运行这样的命令:

$ valgrind --leak-check=full  ./program < speed-01.in

我有一些像这样的测试以不同的后缀结尾,如 -02-03 等等。

我不想编写 bash 脚本,而是想 运行 这些测试一个接一个,同时仅更改最后一位数字,如下所示:

$ valgrind --leak-check=full  ./program < speed-0${A}.in ${A}=1

但是,在这种情况下引入变量似乎不是正确的方法。

我的问题是:在这种情况下如何使用变量?甚至有可能以这样的方式写出整个想法确实有任何意义吗?

for f in speed-*.in; do
  valgrind --leak-check=full  ./program <"$f"
done

...或者,如果您真的出于某种原因想要数数...

for ((a=0; a<9; a++)); do
  printf -v num '%02d' "$a" # add a leading 0 only if number is less than 10
  valgrind --leak-check=full ./program <"speed-${num}.in"
done

现在,如果你想用不同的值手动运行这个很容易,只需定义一个函数:

leakcheck() {
  local num
  printf -v num '%02d' ""
  valgrind --leak-check=full ./program <"speed-${num}.in"
}

...那么您可以运行...

leakcheck 1
leakcheck 2
...

另一种方式,使用你的例子是

A=1
valgrind --leak-check=full  ./program < speed-0$((A++))

然后,再重复最后一行最多九次。

例如,如果您想要 01..12,这将变得更加复杂。