Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript做shell脚本_Javascript_Macos_Shell_Applescript - Fatal编程技术网

javascript做shell脚本

javascript做shell脚本,javascript,macos,shell,applescript,Javascript,Macos,Shell,Applescript,新手问题- 既然如此,我如何将applescript设置为(do shell script“ls”)更改为javascript语法 根据我在StandardAdditions.sdef中读到的内容,“doShellScript 方法:使用“sh”shell执行shell脚本“是命令,所以我尝试了,doshell脚本“ls”,脚本编辑器(脚本语言设置为JavaScript)返回 “错误-2700:脚本错误。” 您必须告诉JXA脚本使用标准添加 app = Application.currentAp

新手问题-

既然如此,我如何将applescript
设置为(do shell script“ls”)
更改为javascript语法

根据我在StandardAdditions.sdef中读到的内容,
“doShellScript 方法:使用“sh”shell执行shell脚本“
是命令,所以我尝试了,
doshell脚本“ls”
,脚本编辑器(脚本语言设置为JavaScript)返回

“错误-2700:脚本错误。”


您必须告诉JXA脚本使用标准添加

app = Application.currentApplication()
app.includeStandardAdditions = true

sourcePath = "/Applications"

// Do not use! Unsafe path-quoting can lead to injection and unwanted behavior! See the update below for the safe version!
app.doShellScript("ls '" + sourcePath + "'").split("\r")
更新: 在@foo因为使用了一种相当不安全的路径引用方法而拍手之后编辑,新的答案是:

app = Application.currentApplication()
app.includeStandardAdditions = true

sourcePath = "/Applications"

// Safe version!
app.doShellScript("ls '" + sourcePath.replace("'", "'\\''") + "'")

享受吧,迈克尔/汉堡

成功了。谢谢你。@Shoo:你的例子太危险了!如果sourcePath包含一个引号,例如“/Users/jsmith/Bob's stuff”,会发生什么情况?将输入放入新的shell脚本时,必须对其进行清理,例如,
doshell脚本(“ls'+sourcePath.replace”(“'”,“\\'”)+”)
。如果您不这样做,您的shell脚本最多只能运行失败;在最坏的情况下,它会做完全错误的事情。例如,如果您使用的是
rm-rf
,该怎么办?如果你有另一个名为“Bob”的文件夹,上面满是重要的文件,上帝会帮助你的,因为惊喜-现在你没有了。总是消毒,消毒,消毒@福:是的,你当然是对的,谢谢!我的快速书面解决方案只涉及了
doShellScript
,但作为社区,我们在回答问题时应该小心,特别是对于新手,甚至对于稍后将阅读答案的新手!我会更新我的答案。@CRGreen我引用了你的话。你的回答被拍打在手上,正好说到点子上:-D