bash:将脚本的输出作为脚本执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5017578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash: executing an output of a script as a script
提问by vmpstr
I'm writing a simple script to generate all combinations of a and b of a given length (say 10). I want to be able to do this on a command line (I know this is fairly easy if I just put everything in a bash script file and execute it). However, I was wondering if it's possible to do without any extra files. Here's what I have so far:
我正在编写一个简单的脚本来生成给定长度(比如 10)的 a 和 b 的所有组合。我希望能够在命令行上执行此操作(我知道如果我将所有内容都放在 bash 脚本文件中并执行它,这将相当容易)。但是,我想知道是否可以在没有任何额外文件的情况下进行。这是我到目前为止所拥有的:
n=10;
for i in `seq 1 1 $n`; do
echo "for a$i in {a..b}; do ";
done;
echo -n "echo ";
for i in `seq 1 1 $n`; do
echo -n '$'"a$i"; done;
echo;
for i in `seq 1 1 $n`; do
echo "done;";
done
(I formatted the code for readability, but it's actually all on one line run from a prompt)
(我格式化了代码以提高可读性,但实际上所有内容都在从提示开始的一行中运行)
This gives me the following output:
这给了我以下输出:
for a1 in {a..b}; do
for a2 in {a..b}; do
for a3 in {a..b}; do
for a4 in {a..b}; do
for a5 in {a..b}; do
for a6 in {a..b}; do
for a7 in {a..b}; do
for a8 in {a..b}; do
for a9 in {a..b}; do
for a10 in {a..b}; do
echo $a1$a2$a3$a4$a5$a6$a7$a8$a9$a10
done;
done;
done;
done;
done;
done;
done;
done;
done;
done;
which is just fine. If I copy that and paste it back on the command line, it works like a charm and gives me the result.
这很好。如果我复制它并将其粘贴回命令行,它就像一个魅力,并给我结果。
The question is how do I do this with just the initial script, without copy-pasting and without redirecting anything to files.
问题是我如何仅使用初始脚本执行此操作,而无需复制粘贴且无需将任何内容重定向到文件。
I've tried sticking $( ) around the script, but that gives me "No command 'for' found'", since it's not really a command but a bash builtin. I've tried putting eval somewhere before this, but I just keep getting more errors. I'm a bit stuck, so any help would be greatly appreciated.
我试过在脚本周围粘贴 $( ) ,但这给了我“找不到命令'for' found'”,因为它不是真正的命令而是内置的bash。我试过在此之前将 eval 放在某个地方,但我只是不断收到更多错误。我有点卡住了,所以任何帮助将不胜感激。
(Btw, just to reiterate, I'm doing this more or less to just learn bash more -- that's why I don't want to redirect the output to a file and then execute that file. I know how to do that part, but I don't know how to just do it from command line)
(顺便说一句,重申一下,我这样做或多或少只是为了更多地学习 bash —— 这就是为什么我不想将输出重定向到一个文件然后执行该文件。我知道如何做那部分,但我不知道如何从命令行执行此操作)
回答by wich
You need to use an eval, $() gives you a string.
你需要使用一个 eval, $() 给你一个字符串。
eval $( echo echo foo )
Another option is to stick into a subshell and pipe it to a bash:
另一种选择是坚持一个子外壳并将其通过管道传输到 bash:
(echo echo foo) | /bin/bash
回答by Paused until further notice.
You can do for i in $(seq $n)instead of seq 1 1 $n.
你可以for i in $(seq $n)代替seq 1 1 $n.
You can do for ((i=1; i<=$n; i++))and avoid calling an external utility.
您可以这样做for ((i=1; i<=$n; i++))并避免调用外部实用程序。
You can do this (slightly hacky with only one loop):
你可以这样做(只有一个循环有点hacky ):
$ a=A; b=B; n=4; s=''; for ((i=1;i<=n;i++)); do s+="{$a..$b}"; done; eval echo "''" $s"$'\n'"
or this (highly hacky without any loops):
或者这个(非常hacky,没有任何循环):
$ a=A; b=B; n=4; eval echo "''" $(printf "{$a..$b}%.0s" $(eval echo "{1..$n}"))"$'\n'"
Either one will get you this:
任何一个都会让你得到这个:
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB

