为什么此命令的行为会有所不同,具体取决于是从terminal.app还是从scala程序调用它? [英] Why does this command behave differently depending on whether it's called from terminal.app or a scala program?

查看:39
本文介绍了为什么此命令的行为会有所不同,具体取决于是从terminal.app还是从scala程序调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在提取file.cbz以显示其内容.

So I'm working on extracting a file.cbz to display it's contents.

我希望通过以下代码进行处理:

I was hoping to go about it with the following code:

println("7z x -y \"" + filePath + "\"" + s" -o$tempLocation")
         if (("7z x -y \"" + filePath + "\"" + s" -o$tempLocation").! == 0){ //I would have liked to do the whole thing with string interpolation, but I didn't work. Seems to be this bug https://issues.scala-lang.org/browse/SI-6476
             Some(new File(tempLocation))
         }else{
         println("Something went wrong extracting the file, probably an incorrect path. Path: " + filePath)
         null
     }

编译并运行它时,得到以下输出:

When I compile and run this, I get the following output:

7z x -y "/path/to/file/test.cbz" -o/tmp/CViewer-temporary-storage

ERROR: No more files
"



System ERROR:
Unknown error: -2147024872

7-Zip [64] 15.14 : Copyright (c) 1999-2015 Igor Pavlov : 2015-12-31
p7zip Version 15.14.1 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64)

Scanning the drive for archives:
Something went wrong extracting the file, probably an incorrect path. Path: /Users/Matt/learning-scala/learning-GUI/test.cbz

Process finished with exit code 0

当我在terminal.app中运行命令时,它运行正常,并且得到以下信息:

When I run the command in terminal.app, it works fine, and I get the following:

Matt$ 7z x -y "/Users/Matt/learning-scala/learning-GUI/test.cbz" -o/tmp/CViewer-temporary-storage

7-Zip [64] 15.14 : Copyright (c) 1999-2015 Igor Pavlov : 2015-12-31
p7zip Version 15.14.1 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64)

Scanning the drive for archives:
1 file, 19587584 bytes (19 MiB)                   

Extracting archive: /path/to/file/test.cbz
--
Path = /path/to/file/test.cbz
Type = Rar
Physical Size = 19587584
Solid = -
Blocks = 33
Multivolume = -
Volumes = 1

Everything is Ok 

Files: 33
Size:       20400561
Compressed: 19587584

这似乎是告诉7z提取一个空目录的结果.

That seems to be the result when 7z is told to extract an empty directory.

推荐答案

您似乎在想Scala支持类似Bash的语法和取消引用规则.据我所知,当您在 String 上调用时,Scala的 ProcessBuilder 没有用于转义字符串的语法.相反,它似乎天真地在空格上分割了这些内容,然后将其作为您的命令执行.如果您不需要在命令中嵌入任何空格,或者如果插值变量从不包含空格,则此方法效果很好.

You seem to be thinking Scala supports Bash-like syntax and unquoting rules. From what I can tell Scala's ProcessBuilder doesn't have a syntax for unescaping strings when you call ! on a String. What it seems to do instead is naively split on whitespace and then execute those pieces as your command. This works well if you don't need to embed any whitespace in your command or if your interpolated variables never include whitespace.

解决方法是使用 Seq ,以便您可以分别定义每个参数,而不必担心转义或转义.所以你执行行:

The fix is to use Seq so that you can define each argument individually, without worrying about escaping or unescaping. So you exec line:

"7z x -y \"" + filePath + "\"" + s" -o$tempLocation").!

成为

Seq("7z", "x" "-y", filePath, s"-o$tempLocation").!

注意:根据脚本最佳实践,您应该始终使用正在执行的命令的完整路径.对我来说,应该是:

Note: per scripting best practices you should always use the full path to the command you are executing. For me this would be:

Seq("/usr/local/bin/7z", "x" "-y", filePath, s"-o$tempLocation").!

这篇关于为什么此命令的行为会有所不同,具体取决于是从terminal.app还是从scala程序调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆