bash - osascript 中的多行字符串

标签 bash applescript osascript

我想在 bash 中执行此操作:

read -r -d '' script <<'EOF'
echo 1
echo 2
echo 3
EOF

osascript -e "do shell script \"$script\" with administrator privileges"

# Output: 3
# Expected: 1
# 2
# 3

只执行最后一行。

但是,如果我这样做:

osascript -e "\"$script\""

# Output: echo 1
# echo 2
# echo 3
# Expected: echo 1
# echo 2
# echo 3

你可以看到所有的线都在那里。

我该如何解决这个问题?

最佳答案

只需添加而不改变行尾(尽管这会添加尾随换行符)。

read -r -d '' script <<'EOF'
echo 1
echo 2
echo 3
EOF

osascript -e "do shell script \"$script\" with administrator privileges without altering line endings" | sed '$d'


# See:
# "do shell script in AppleScript",
# http://developer.apple.com/library/mac/#technotes/tn2065/_index.html
# ...
# By default, do shell script transforms all the line endings in the result to 
# Mac-style carriage returns ("\r" or ASCII character 13), and removes a single 
# trailing line ending, if one exists. This means, for example, that the result
# of do shell script \"echo foo; echo bar\" is \"foo\\rbar\", not the 
# \"foo\\nbar\\n\" that echo actually returned. You can suppress both of 
# these behaviors by adding the "without altering line endings" parameter. For 
# dealing with non-ASCII data, see Dealing with Text.
# ...

关于bash - osascript 中的多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6935924/

相关文章:

javascript - 如何通过 osascript 处理同一应用程序的两个实例

swift - AppleScript(或 OSA)的功能是 Cocoa 的子集吗

Applescript、POSIX 文件名和 Mountain Lion

macos - 如何通过 mac 终端模拟鼠标点击?

bash - 在 mac 上匹配 sed 中的重复字符

performance - AppleScript 最小化所有可见窗口的速度非常慢。如何加快速度?

python - 如何以及在何处通过基于 macOS Python 的应用程序上的 native GUI 最好地检索 sudo 密码 - (同时维护交互式输出流(stdout))

regex - 查找所有文件,例如shell 中的 *.xml 文件

bash - shell : get function's body by it's name

bash - sed- 删除不包含模式的行