2017-03-03 27 views
1

我需要执行下面的shell命令,并读出耙变量输出:流程单quoutes在耙/红宝石

`#{security_bin} find-identity -v -p codesigning #{keychain_path} | grep "$1" | awk -F\" '/"/ {print $2}' | head -n1` 

正如你所看到的,我使用反引号告诉耙解释器,“这是一个shell命令来执行”。问题在于它不起作用,因为在调用中单引号的使用会破坏解析或某些内容,而ruby不知道该条目结束的位置。

我得到的错误是:

rake xamarin:create_keychain 
Deleted the old one... 
About to retrieve identity... 
sh: -c: line 0: unexpected EOF while looking for matching `'' 
sh: -c: line 1: syntax error: unexpected end of file 

其内的流动情况如下(虽然我仍然怀疑上面提到的线功能:

def create_temp_keychain(keychain_path, p12path) 
    security_bin='/usr/bin/security' 

    delete_keychain(keychain_path) 

    `/usr/bin/security create-keychain -p tempass #{keychain_path}` 
    `#{security_bin} set-keychain-settings -lut 7200 #{keychain_path}` 
    `#{security_bin} unlock-keychain -p "tempass" #{keychain_path}` 
    `#{security_bin} import #{p12path} -P "" -A -t cert -f pkcs12 -k #{keychain_path}` 
    `#{security_bin} list-keychain -d user -s #{keychain_path} "login.keychain"` 
    puts "About to retrieve identity..." 
    identity_output = `#{security_bin} find-identity -v -p codesigning #{keychain_path} | grep "$1" | awk -F\" '/"/ {print $2}' | head -n1` 
    puts identity_output 
end 

什么在红宝石中执行以下命令的正确方法?

+0

你可以包括你得到的实际错误/堆栈跟踪? ruby允许单引号内反引号,所以我不认为这是你的问题。 – eiko

+0

@eiko谢谢。我已经更新了这个问题。我假设'|的部分awk -F \“'/”/ {print $ 2}'|头-n1'发生(-F \)是什么打破了东西。 –

+1

'awk -F \\“'...'应该可以工作。如果不是,请尝试三个反斜杠。 – mudasobwa

回答

0

双引号应该转义两次,如:

awk -F\\" '... 

为了在ruby中处理基于反引号的shell调用中的shell双引号。

正确的路线是:

`#{security_bin} find-identity -v -p codesigning #{keychain_path} | grep "$1" | awk -F\\" '/"/ {print $2}' | head -n1` 

信贷的答案去@mudasobwa。