Многострочные строки в osascript - PullRequest
3 голосов
/ 04 августа 2011

Я хочу сделать это в 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

Выможно увидеть, что все строки есть.

Как мне это исправить?

Ответы [ 2 ]

4 голосов
/ 04 августа 2011

Просто добавьте without altering line endings (хотя это добавит завершающий символ новой строки).

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.
# ...
0 голосов
/ 04 августа 2011

Возможно, вам придется распечатать всю командную строку во временном файле, а затем вызвать osascript для файла, вместо того, чтобы пытаться поместить многострочное значение в одну строку.

...