gpt4 book ai didi

bash - osascript 中的多行字符串

转载 作者:行者123 更新时间:2023-11-29 09:26:47 32 4
gpt4 key购买 nike

我想在 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com