简体   繁体   中英

Error when executing sudo rm commands in shell script (osascript) on MacOS

I want to learn about scripting in mac, this is my first script based on what I found on the web, i'm just trying to delete some files (MySQL) after show some dialogs, the dialogs works fine, but when I put the sudo rm lines it doesn't work, it says Syntax Error , this is my code:

#!/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

EDIT: I try like @that other guy says like this:

`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"`

But if one command fail the execution just stop.

Thanks to @thatotherguy, @CharlesDuffy and @zneak, here is the script working perfectly:

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM