我有一个我称之为使用的shell脚本 osascript
, 然后 osascript
调用shell脚本并传入我在原始shell脚本中设置的变量。我不知道如何将该变量从applescript传递到shell脚本。
如何将变量从shell脚本传递到applescript到shell脚本......?
如果我没有意义,请告诉我。
i=0
for line in $(system_profiler SPUSBDataType | sed -n -e '/iPad/,/Serial/p' -e '/iPhone/,/Serial/p' | grep "Serial Number:" | awk -F ": " '{print $2}'); do
UDID=${line}
echo $UDID
#i=$(($i+1))
sleep 1
osascript -e 'tell application "Terminal" to activate' \
-e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
-e 'tell application "Terminal" to do script "cd '$current_dir'" in selected tab of the front window' \
-e 'tell application "Terminal" to do script "./script.sh ip_address '${#UDID}' &" in selected tab of the front window'
done
Shell变量不会在单引号内扩展。当你想要传递shell变量时 osascript
你需要使用双倍 ""
引号。问题是,你必须转义osascript中所需的双引号,如:
剧本
say "Hello" using "Alex"
你需要逃脱报价
text="Hello"
osascript -e "say \"$text\" using \"Alex\""
这不是很易读,因此使用bash更好 heredoc
功能,像
text="Hello world"
osascript <<EOF
say "$text" using "Alex"
EOF
你可以在里面写一个免费的多行脚本,它比使用多个更好 -e
ARGS ...
Shell变量不会在单引号内扩展。当你想要传递shell变量时 osascript
你需要使用双倍 ""
引号。问题是,你必须转义osascript中所需的双引号,如:
剧本
say "Hello" using "Alex"
你需要逃脱报价
text="Hello"
osascript -e "say \"$text\" using \"Alex\""
这不是很易读,因此使用bash更好 heredoc
功能,像
text="Hello world"
osascript <<EOF
say "$text" using "Alex"
EOF
你可以在里面写一个免费的多行脚本,它比使用多个更好 -e
ARGS ...
您还可以使用运行处理程序或导出:
osascript -e 'on run argv
item 1 of argv
end run' aa
osascript -e 'on run argv
item 1 of argv
end run' -- -aa
osascript - -aa <<'END' 2> /dev/null
on run {a}
a
end run
END
export v=1
osascript -e 'system attribute "v"'
我不知道有什么方法可以获得STDIN。 on run {input, arguments}
仅适用于Automator。