我正在尝试使用Shell脚本将一些文本写入位于主库/ Application Support文件夹的子文件夹中的xml文件中 做shell脚本“ echo'”和theText&“'>”和thePath。
没有密码,我得到一个“ sh:/ Library / Application:权限被拒绝”,这是完全合乎逻辑的。 如下面的代码所示,添加用户名和密码后,我不再遇到任何错误,但是没有文本写入文件。 如果我输入了错误的用户名或密码,它会提示我“用户名和密码不正确”,这表明确实在考虑密码。 我是在尝试做一些不可能的事情,还是我的代码缺少了什么?
set thePath to POSIX path of ("/Library/Application Support/MyApp/Stuff/test.xml" as text)
set theText to "<ProductSpecific><Visibility type=\"Number\">3</Visibility></ProductSpecific>"
set theScript to "echo '" & theText & "' > " & thePath
do shell script theScript user name "myshortusername" password "mypassword" with administrator privileges
我应该将文本写入/ Library / Application Support / MyApp / Stuff / test.xml 但是什么也没写,尽管我也没有收到错误信息!哦,如果我将文件移动到桌面并更改路径,一切正常!
答案 0 :(得分:0)
错误的原因是Application Support
中的空格字符。您必须逃脱整个路径。
强烈建议始终使用quoted form of
set theScript to "echo " & quoted form of theText & " > " & quoted form of thePath
旁注:
字符串路径已经是POSIX路径(并且已经是文本),因此您可以省略两者
set thePath to "/Library/Application Support/MyApp/Stuff/test.xml"