Problem with shell script on Mac (osascript dialogs)

3

I am creating a script to delete some files (my purpose is to learn to use the dialogs and execute some tasks), I was forming it based on examples in the network, the dialogs work well, except when I want to execute the commands sudo , tell me:

  

syntax error: End of line expected but identifier was found. (-2741)

At first I tried to create a function to call it but it did not work either, this is what I have so far:

#!/bin/sh
osascript <<EOT
 app "System Events"
set answer to the button returned of (display dialog "Deseas eliminar MySQL?" buttons {"Si", "No"} default button 2)
#display dialog "You selected " & answer
if answer = "No" then
display dialog "Cancelado" buttons {"Aceptar"}
else
if (answer = "Si")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 "Se ha eliminado MySQL, es necesario reiniciar.\nDeseas reiniciar ahora?" buttons {"Si","Reiniciar mas tarde"}default button 2)
end if
end if
return  -- Suppress result
end
EOT
    
asked by Angel Montes de Oca 18.09.2017 в 18:10
source

1 answer

2

You can launch osascript as sudo from the beginning instead of using sudo in each command and use do shell script "código" by putting a ";" to separate each command:

#!/bin/sh
sudo osascript <<EOT
 app "System Events"
set answer to the button returned of (display dialog "Deseas eliminar MySQL?" buttons {"Si", "No"} default button 2)
#display dialog "You selected " & answer
if (answer = "No") then
display dialog "Cancelado" buttons {"Aceptar"}
else
if (answer = "Si")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 "Se ha eliminado MySQL, es necesario reiniciar.\nDeseas reiniciar ahora?" buttons {"Si","Reiniciar mas tarde"}default button 2)
if (answer2 = "Si")then
tell app "System Events" to restart

end if
end if
return  -- Suppress result
end
EOT
    
answered by 19.09.2017 / 18:17
source