Solved

Simple Perl Script to remove folders in MAX OSX

Posted on 2013-06-27
6
237 Views
Last Modified: 2013-07-03
I need to do the following in a MAC terminal over and over as part of testing and want to include it in a .pl script:

#Remove Existing MySQL
sudo rm "/usr/local/mysql";
sudo rm -rf "/usr/local/mysql*";
sudo rm -rf "/Library/StartupItems/MySQLCOM";
sudo rm -rf "/Library/PreferencePanes/My*";
#sudo nano /etc/hostconfig  EDIT the MySQL bottom line out.
rm -rf "~/Library/PreferencePanes/My*";
sudo rm -rf "/Library/Receipts/mysql*";
sudo rm -rf "/Library/Receipts/MySQL*";
sudo rm -rf "/private/var/db/receipts/*mysql*";
#Removed Existing MySQL

How can I acheive that?  I tried with the syntax above, but it is obviously wrong.
0
Comment
Question by:weklica
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39283433
Why not just use a shell script?  Shell scripts pretty much use the same syntax as the command line.

If you really want to use a Perl script, this should work:
use strict;
use warnings;
foreach my $cmd (<DATA>) {
    next if ($cmd =~ m{^\s*#});
    system($cmd) == 0 or die "$cmd failed: $?";
}
__DATA__
#Remove Existing MySQL
sudo rm "/usr/local/mysql"
sudo rm -rf "/usr/local/mysql*"
sudo rm -rf "/Library/StartupItems/MySQLCOM"
sudo rm -rf "/Library/PreferencePanes/My*"
#sudo nano /etc/hostconfig  EDIT the MySQL bottom line out.
rm -rf "~/Library/PreferencePanes/My*"
sudo rm -rf "/Library/Receipts/mysql*"
sudo rm -rf "/Library/Receipts/MySQL*"
sudo rm -rf "/private/var/db/receipts/*mysql*"
#Removed Existing MySQL

Open in new window

0
 

Author Comment

by:weklica
ID: 39283439
I need to use perl because the rest of the script is in perl.  Do I need to do anything to isolate this from the rest of the script or just paste it above or below the rest of it?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39283453
The code you posted is shell commands, not Perl.  You can probably put it all in a shell script that you can call from Perl.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:weklica
ID: 39283462
I will just copy it above my perl script and hope it works.  not sure how to do anything in perl including the suggesting above, so i will let you know.  

Thanks
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 39283464
You could either create a shell script that you call from Perl (as DaveBaldwin said) or you could copy the code I provided in.

To copy my code into your script:
The part above __DATA__ can go anywhere
__DATA__ and below must go at the end of the script
If there is already a __DATA__ section in your script, it will not work

To write a shell script, create a new file with the below in it:
#!/bin/sh
#Remove Existing MySQL
sudo rm "/usr/local/mysql";
sudo rm -rf "/usr/local/mysql*";
sudo rm -rf "/Library/StartupItems/MySQLCOM";
sudo rm -rf "/Library/PreferencePanes/My*";
#sudo nano /etc/hostconfig  EDIT the MySQL bottom line out.
rm -rf "~/Library/PreferencePanes/My*";
sudo rm -rf "/Library/Receipts/mysql*";
sudo rm -rf "/Library/Receipts/MySQL*";
sudo rm -rf "/private/var/db/receipts/*mysql*";
#Removed Existing MySQL

Open in new window


And then put this in your Perl script:
# replace script.sh with whatever you named the file above
system("script.sh") == 0 or die "script.sh failed: $?";

Open in new window

0
 

Author Closing Comment

by:weklica
ID: 39297926
Thanks much!  Very helpful.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question