我想$var in在 bash 中使用带有范围的 shell 括号扩展。简单地放置{$var1..$var2}不起作用,所以我去了“横向”......
以下工作,但它有点笨拙。
# remove the split files
echo rm foo.{$ext0..$extN} rm-segments > rm-segments
source rm-segments
Run Code Online (Sandbox Code Playgroud)
有没有更“正常”的方式?
以下是我要完成的某种伪代码:
#!/bin/bash
# I already have the variable below figured out (positive integer):
numlines=$([returns number of lines containing specific characters in a file])
# This is basically what I want to do with it:
for i in {1..$numlines}; do
# the part below is already figured out as well:
do some other stuff
done
Run Code Online (Sandbox Code Playgroud)
通过在“{1..n}”序列中插入实际数字,我可以从命令行很好地执行它。我只需要知道是否可以在这里包含一个变量以及如何去做。
export了{1..${numlines}}{1.."$numlines"}$:{1..\$numlines}我是否需要使用set -[something]命令才能扩展此变量?我什至尝试过某些形式的使用eval...都无济于事。
我只需要知道我是否遗漏了一些简单或晦涩的东西,或者在我浪费时间之前这是否可能。
我可以拼凑出一种非常非常hackish 的方式来让它根据需要工作,但如果可能的话,我想避免这种情况,并学习正确的方法来做这件事。
我可以在{}扩展中使用变量而不吸引eval吗?如果是这样,如何?
这不起作用:
$ touch 1.foo 1.bar
$ ls 1.{foo,bar}
1.bar 1.foo
$ extensions=foo,bar
$ ls 1.{$extensions}
ls: cannot access 1.{foo,bar}: No such file or directory
Run Code Online (Sandbox Code Playgroud)
它适用于eval:
$ eval ls 1.{$extensions}
1.bar 1.foo
Run Code Online (Sandbox Code Playgroud) 我有一个程序可以生成可变数量的图片,顺序如下:
1.jpg, 2.jpg [...] 10.jpg..etc
我不能*.jpg用作输入,因为 10 将在 2、3 等之前处理,所以我想使用:
convert page_{1..$N}.jpg out.pdf
Run Code Online (Sandbox Code Playgroud)
但不起作用。命令应该如何?