Solved

Simple Perl Script to remove folders in MAX OSX

Posted on 2013-06-27
6
239 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script help 12 106
Check file date before getting folder path 1 84
PERL variable conundrum 9 95
Perl modules on linux ec2 3 123
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

713 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