2016-11-16 71 views
2

我想将用户的命令行历史记录中的sting拆分为命令行参数。举例来说,我想将字符串拆分为命令行参数

program argument escaped\ argument.txt -o 'another escaped.output' 

分成

$v[1]: program 
$v[2]: argument 
$v[3]: escaped argument.txt 
$v[4]: -o 
$v[5]: another escaped.output 

我已经尝试每一个可能的解决方案,我可以,但鱼自动引用变量,没有我的解决方案的工作。

谢谢!

+0

你想存储到'bash'数组? – Inian

+0

@Inian成鱼(https://fishshell.com/)数组。 – boramalper

+0

“鱼自动引用变量” - 呃,不。它只是不分裂它。 – faho

回答

4

目前,没有干净的方式来做到这一点在鱼。

我能想出的最好的是一个相当黑客:

set s "program argument escaped\ argument.txt -o 'another escaped.output'" 
set oldcmd (commandline) 
commandline -r -- $s 
set v (commandline -o) 
commandline -r -- $oldcmd 
printf '%s\n' $v 

这种滥用鱼的命令行缓冲区,并适当令牌化的内容。

3

这是你需要的地方eval

$ set s "program argument escaped\ argument.txt -o 'another escaped.output'" 
$ eval set v $s 
$ printf "%s\n" $v 
program 
argument 
escaped argument.txt 
-o 
another escaped.output 
+1

请注意,这个_will_会在参数中运行任何命令替换和类似操作,并且没有简单的方法。 – faho