在MacOS上的shell脚本(osascript)中执行sudo rm命令时出错

时间:2017-09-18 16:31:46

标签: macos shell

我想学习mac中的脚本,这是我的第一个基于我在网上找到的脚本,我只是在显示一些对话框后试图删除一些文件(MySQL),对话框工作正常,但是我把sudo rm lines它不起作用,它说Syntax Error,这是我的代码:

#!/bin/sh
osascript <<EOT
 app "System Events"
set answer to the button returned of (display dialog "You want to delete MySQL?" buttons {"Yes", "No"} default button 2)
if (answer = "No") then
display dialog "Canceled" buttons {"ok"}
else
if (answer = "Yes")then
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /usr/local/mysql*
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /var/db/receipts/com.mysql.*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*
launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysql.plist
launchctl unload -w ~/Library/LaunchDaemons/com.st.plist
set answer to the button returned of (display dialog "MySQL deleted, restart necesary.\nrestart now?" buttons {"Yes","Restart Later"}default button 2)
end if
end if
return  -- Suppress result
end
EOT

编辑: 我试着像其他人这样说:

`do shell script "sudo rm /usr/local/mysql && 
sudo rm -rf /usr/local/var/mysql && 
sudo rm -rf /usr/local/mysql* && 
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist && 
sudo rm -rf /Library/StartupItems/MySQLCOM && 
sudo rm -rf /Library/PreferencePanes/My* && 
sudo rm -rf /var/db/receipts/com.mysql.* && 
sudo rm -rf /Library/Receipts/MySQL* && 
sudo rm -rf /private/var/db/receipts/*mysql* && 
launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysql.plist && 
launchctl unload -w ~/Library/LaunchDaemons/com.st.plist"`

但如果一个命令失败,执行就会停止。

1 个答案:

答案 0 :(得分:1)

感谢@thatotherguy,@ CharlesDuffy和@zneak,这里的脚本完美无缺:

#!/bin/sh
sudo osascript <<EOT
 app "System Events"
set answer to the button returned of (display dialog "You want to delete MySQL?" buttons {"Yes", "No"} default button 2)
if (answer = "No") then
display dialog "Canceled" buttons {"ok"}
else
if (answer = "Yes")then
do shell script "sudo rm /usr/local/mysql;
rm -rf /usr/local/var/mysql;
rm -rf /usr/local/mysql*;
rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist;
rm -rf /Library/StartupItems/MySQLCOM;
rm -rf /Library/PreferencePanes/My*;
rm -rf /var/db/receipts/com.mysql.*;
rm -rf /Library/Receipts/MySQL*;
rm -rf /private/var/db/receipts/*mysql*;
launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysql.plist;
launchctl unload -w ~/Library/LaunchDaemons/com.st.plist;"

set answer2 to the button returned of (display dialog "MySQL deleted, restart necesary.\nrestart now?" buttons {"Yes","Restart Later"}default button 2)
if (answer2 = "Yes")then
tell app "System Events" to restart

end if
end if
return  -- Suppress result
end
EOT