Sir*_*III 11 bash shell applescript osascript
我有一个我调用的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
Run Code Online (Sandbox Code Playgroud)
jm6*_*666 14
Shell变量不会在单引号内扩展.当你想要传递一个shell变量时,osascript你需要使用双""引号.问题是,你必须转义osascript中需要的双引号,如:
剧本
say "Hello" using "Alex"
Run Code Online (Sandbox Code Playgroud)
你需要逃脱报价
text="Hello"
osascript -e "say \"$text\" using \"Alex\""
Run Code Online (Sandbox Code Playgroud)
这不是很易读,因此使用bash的heredoc功能要好得多
text="Hello world"
osascript <<EOF
say "$text" using "Alex"
EOF
Run Code Online (Sandbox Code Playgroud)
你可以在里面免费编写多行脚本,它比使用多个-eargs 要好得多......