我有一个Perl脚本,它给我一个定义的列表随机数,对应于文件的行.我接下来要做的是使用从文件中提取这些行sed
.
#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var=$(perl test.pl test2 $count)
Run Code Online (Sandbox Code Playgroud)
该变量var
返回如下输出:cat last_queries.txt | sed -n '12p;500p;700p'
.问题是我无法运行最后一个命令.我试过$var
但输出不正确(如果我手动运行命令它工作正常,所以没有问题).这样做的正确方法是什么?
PS:当然我可以在Perl中完成所有工作,但我正在尝试学习这种方式,因为它可以在其他情况下帮助我.
hmo*_*liu 203
如果我理解正确,你只需要做(对不起,如果我错过了你的观点):
#!/bin/bash
count=$(cat last_queries.txt | wc -l)
$(perl test.pl test2 $count)
Run Code Online (Sandbox Code Playgroud)
但是,如果您想稍后调用perl命令,这就是您希望将其分配给变量的原因,那么:
#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var="perl test.pl test2 $count" # you need double quotes to get your $count value substituted.
...stuff...
eval $var
Run Code Online (Sandbox Code Playgroud)
根据bash的帮助:
~$ help eval
eval: eval [arg ...]
Execute arguments as a shell command.
Combine ARGs into a single string, use the result as input to the shell,
and execute the resulting commands.
Exit Status:
Returns exit status of command or success if command is null.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
226961 次 |
最近记录: |