Solved

Simple Perl Script to remove folders in MAX OSX

Posted on 2013-06-27
6
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
Six Sigma Control Plans

615 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