新手问题 关于 shell 字符串拼接问题,求解释

i5ting · June 20, 2014 · Last by appell replied at June 20, 2014 · 3998 hits

关于 shell 字符串拼接问题

def get_attr_by(path,name)
    `"/usr/libexec/PlistBuddy -c  'print #{name}' #{path}"`
end

这段代码报错

sh: /usr/libexec/PlistBuddy -c  'print CFBundleName' test/Payload/mysysy.app/Info.plist: No such file or directory

而下面是正常的

def get_attr_by(path,name)
    cmd = "/usr/libexec/PlistBuddy -c  'print #{name}' #{path}"
    `#{cmd}`
end

百思不得,求各位帮忙给个解释

/usr/libexec/PlistBuddy -c 'print #{name}' #{path} 不负责任猜下,双引号去掉,,,

No such file or directory: 'print CFBundleName'

第一个函数改成

def get_attr_by(path,name)
    `/usr/libexec/PlistBuddy -c  'print #{name}' #{path}`
end

Ruby 语法中 双引号是标记字符串的 而当使用了反引号之后 已经默认里面就是字符串了 不需要再额外加上双引号

You need to Sign in before reply, if you don't have an account, please Sign up first.