3

これを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

すべての行がそこにあることがわかります。

これを修正するにはどうすればよいですか?

4

2 に答える 2

4

追加するだけ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.
# ...
于 2011-08-04T11:58:29.503 に答える
0

osascript複数行の値を 1 行に収めようとするのではなく、コマンド文字列全体を一時ファイルに出力してから、そのファイルを呼び出す必要がある場合があります。

于 2011-08-04T04:08:26.507 に答える