how to create AppleScript app to run a set of terminal commands - macos

How would I go about creating an AppleScript command that when I just run the script (or double click it in Finder?), it would run a set of terminal commands? The set of commands completely remove MySQL, and it has become a pain to constantly write them out. The commands are:
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm /etc/my.cnf
There is also another command sudo nano /etc/hostconfig that opens a file and I need to delete a line from the file, but that seems like it'd be too hard to code, so I guess I can do that by hand. But it would be a huge help to do this automatically with a single script.
Would it just be a bunch of these commands?
do shell script (...)
Thanks,
Hristo

Yes, you would use do shell script.
However, for the commands where you execute as super user (sudo) you would instead use with administrator privileges. So, for sudo rm /usr/local/mysql you'd do:
do shell script "rm /usr/local/mysql" with administrator privileges
If your list of commands is long, then it might just be easier to put all your commands into a single shell script file, and then execute that shell script using do shell script:
do shell script "/path/to/shell/script" with administrator privileges

You don't actually need to use AppleScript for this - just put all the shell commands in a text file and give it a .command suffix and make sure it's executable (e.g. chmod +x my_script.command) - this will make it double-clickable in the Finder.

I find that .scpt files work best but this is just a preference thing.
Open "Script Editor" and add the following command:
sudo rm -rf /usr/local/mysql; sudo rm -rf /usr/local/mysql*; sudo rm -rf /Library/StartupItems/MySQLCOM; sudo rm -rf /Library/PreferencePanes/My*; sudo rm -rf /Library/Receipts/mysql*; sudo rm -rf /Library/Receipts/MySQL*; sudo rm /etc/my.cnf; say job completed successfully" with administrator privileges

Related

How to delete contents of user home dir safely via bash

I am writing a bash script to do a account restore. The contents of the home dir is zipped up using this command.
sudo sh -c "cd /home/$username; zip -0 -FS -r -b /tmp /home/0-backup/users/$username.zip ."
This works as expected.
If the user requests a restore of their data, I am doing the following
sudo sh -c "cd /home/$username; rm -rf *"
Then
sudo -u $username unzip /home/0-backup/users/$username.zip -d /home/$username/
This works as expected.
However you can see the flaw in the delete statement, if the username is not set. We delete all users home dir. I have if statements that do the checking to make sure the username is there. I am looking for some advice on a better way to handle resetting the users account data that isn't so dangerous.
One thought I had was to delete the user account and then recreate it. Then do the restore. I think that this would be less risky. I am open to any suggestions.
Check the parameters first.
Then use && after cd so that it won't execute rm if the cd fails.
if [ -n "$username" ] && [ -d "/home/$username" ]
then
sudo sh -c "cd '/home/$username' && rm -rf * .[^.]*"
fi
I added .[^.]* in the rm command so it will delete dot-files as well. [^.] is needed to prevent it from deleting . (the user's directory) and .. (the /home directory).

Makefile running command with whoami

I am trying to use a makefile to setup my machine. I am trying to setup FZF and have the following code. However, it seems to be replacing that command with empty space instead of the user I am logged as. I have SSH'd into the Pi, so not sure if that is the cause or if it's something else.
linuxfxf:
sudo mkdir -p /usr/local/opt
sudo chown -R $(whoami) /usr/local/opt
$ is a special character for make: it introduces a make variable. If you want to run a recipe and have the shell see the $ you have to escape it:
linuxfxf:
sudo mkdir -p /usr/local/opt
sudo chown -R $$(whoami) /usr/local/opt
Or you could use the old-school syntax `whoami` instead.

File permissions, root bash script, edit by user

I have a script that needs to be ran as root. In this script I create directories and files. The files and directories cannot be modified by the user who ran the script (unless there root of course).
I have tried several solutions found here and other sites, first I tried to mkdir -m 777 the directories as so:
#!/bin/bash
...
#Check execution location
CDIR=$(pwd)
#File setup
DATE=$(date +"%m-%d_%H:%M:%S")
LFIL="$CDIR/android-tools/logcat/logcat_$DATE.txt"
BFIL="$CDIR/android-tools/backup/backup_$DATE"
mkdir -m 777 -p "$CDIR/android-tools/logcat/"
mkdir -m 777 -p "$CDIR/android-tools/backup/"
...
I have also tried touching every created file and directory with the $USER as root, like so:
#!/bin/bash
...
#Check execution location
CDIR=$(pwd)
#File setup
DATE=$(date +"%m-%d_%H:%M:%S")
LFIL="$CDIR/android-tools/logcat/logcat_$DATE.txt"
BFIL="$CDIR/android-tools/backup/backup_$DATE"
mkdir -p "$CDIR/android-tools/logcat/"
mkdir -p "$CDIR/android-tools/backup/"
sudo -u $USER touch "$CDIR/"
sudo -u $USER touch "$CDIR/android-tools/"
sudo -u $USER touch "$CDIR/android-tools/logcat/"
sudo -u $USER touch "$CDIR/android-tools/backup/"
sudo -u $USER touch "$CDIR/android-tools/logcat/logcat_*.txt"
sudo -u $USER touch "$CDIR/android-tools/logcat/Backup_*"
...
I have also tried manually running sudo chmod 777 /android-tools/*, and sudo chmod 777 /* from the script directory, gave no errors, but I still cannot delete the files without root permission.
Heres the full script, It's not done yet. Don't run it with an android device connected to your computer.
http://pastebin.com/F20rLJQ4
touch doesn't change ownership. I think you want chown.
If you're using sudo to run your script, $USER is root, but $SUDO_USER is the user who ran sudo, so you can use that.
If you're not using sudo, you can't trust $USER to be anything in particular. The caller can set it to anything (like "root cat /etc/shadow", which would make your above script do surprising things you didn't want it to do because you said $USER instead of "$USER").
If you're running this script using setuid, you need something safer, like id -u, to get the calling process's legitimate UID regardless of what arbitrary string happens to be in $USER.
If you cover both possibilities by making makestuff.sh like this:
# $SUDO_USER if set, otherwise the current user
caller="${SUDO_USER:-$(id -u)}"
mkdir -p foo/bar/baz
chown -R "$caller" foo
Then you can use it this way:
sudo chown root makestuff.sh
sudo chmod 755 makestuff.sh
# User runs it with sudo
sudo ./makestuff.sh
# User can remove the files
rm -r foo
Or this way (if you want to use setuid so regular users can run the script without having sudo access -- which you probably don't, because you're not being careful enough for that):
sudo chown root makestuff.sh
sudo chmod 4755 makestuff.sh # Danger! I told you not to do this.
# User runs it without sudo
./makestuff.sh
# User can remove the files
rm -r foo

how to script folder deletion on MAC

I believe it should be straight forward but either I am having a bad day or I simply can't find what I am looking for.
Please help.
I need to run following commands in MAC Terminal in order to get rid of following entries:
sudo rm -Rf /Applications/Network\ Connect.app
sudo rm -Rf /Library/Frameworks/net.juniper.DSApplicationServices.framework
sudo rm -Rf /Library/Frameworks/net.juniper.DSCoreServices.framework
sudo rm -Rf /Library/Frameworks/net.juniper.DSNetworkDiagnostics.framework
sudo rm -Rf /Library/Internet\ Plug-ins/net.juniper.DSSafariExtensions.plugin
sudo rm -Rf /Library/Widgets/Network\ Connect.wdgt
sudo rm -Rf /usr/local/juniper
sudo rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.bom
sudo rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.plist
sudo rm -Rf ~/Library/Preferences/ncproxyd.plist
It does it's job but it's not exactly elegant. I was also thinking about providing this to my colleagues so I wanted to create some sort of .bat file for MAC.
I really spent about half day trying to figure it out but it doesn't work :(
Can somebody help me to create a .sh file or bash file which will do execute the commands above?
Create a script file, let's say it's called deletion.sh and add the lines: -
#!/bin/bash
rm -Rf /Applications/Network\ Connect.app
rm -Rf /Library/Frameworks/net.juniper.DSApplicationServices.framework
rm -Rf /Library/Frameworks/net.juniper.DSCoreServices.framework
rm -Rf /Library/Frameworks/net.juniper.DSNetworkDiagnostics.framework
rm -Rf /Library/Internet\ Plug-ins/net.juniper.DSSafariExtensions.plugin
rm -Rf /Library/Widgets/Network\ Connect.wdgt
rm -Rf /usr/local/juniper
rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.bom
rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.plist
rm -Rf ~/Library/Preferences/ncproxyd.plist
Then, in terminal you need to set the executable flag to the script: -
chmod +x deletion.sh
Note that the executable flag may be removed when the script is copied to another machine or network drive, so you may have to do that after copying.
Finally, you can call the script with sudo
sudo ./deletion.sh
If you want to create a batch file, you have one. That list of commands is your shell script. To execute it, just save hem into a file add the bash command to the front of that file's name:
$ bash commands_I_want_to_execute.txt
If you want to get fancy, you can put a shebang on the top and set the execution bit using chmod. That will make your script a real shell script.
However, in order for your shell script to be found, you need to either prefix it with a path, or put it in a directory that's included in your PATH. Here, I'll just prefix it:
$ chmod a+x commands_I_want_to_execute.txt # Suffix doesn't really matter. It's executable
$ ./commands_I_want_to_execute.txt # Now this will be executed
If you are really bothered by the suffix, change it with the mv command:
$ mv commands_I_want_to_execute.txt commands_I_want_to_execute.sh
NOTE: If you create a file with Text Edit, create it as a plain text file and not as a RTF file.

How to uninstall Jenkins?

This is probably very simple, but I can't find any hint anywhere. So how one is supposed to do that, in general and specifically on Mac?
These instructions apply if you installed using the official Jenkins Mac installer from http://jenkins-ci.org/
Execute uninstall script from terminal:
'/Library/Application Support/Jenkins/Uninstall.command'
or use Finder to navigate into that folder and double-click on Uninstall.command.
Finally delete last configuration bits which might have been forgotten:
sudo rm -rf /var/root/.jenkins ~/.jenkins
If the uninstallation script cannot be found (older Jenkins version), use following commands:
sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm -rf /Applications/Jenkins "/Library/Application Support/Jenkins" /Library/Documentation/Jenkins
and if you want to get rid of all the jobs and builds:
sudo rm -rf /Users/Shared/Jenkins
and to delete the jenkins user and group (if you chose to use them):
sudo dscl . -delete /Users/jenkins
sudo dscl . -delete /Groups/jenkins
These commands are also invoked by the uninstall script in newer Jenkins versions, and should be executed too:
sudo rm -f /etc/newsyslog.d/jenkins.conf
pkgutil --pkgs | grep 'org\.jenkins-ci\.' | xargs -n 1 sudo pkgutil --forget
You are right, it is simple. Run (admin password required):
'/Library/Application Support/Jenkins/Uninstall.command'
It may be necessary to do this with admin privileges using sudo.
Keep in mind, that in Terminal you need to add backslash before space, so the proper copy/paste will be
/Library/Application\ Support/Jenkins/Uninstall.command
p.s. sorry for the late answer :)
run this on Terminal:
sh "/Library/Application Support/Jenkins/Uninstall.command"
There is no uninstaller. Therefore, you need to:
Delete the directory containing Jenkins (or, if you're deploying the war -- remove the war from your container).
Remove ~/.jenkins.
Remove you startup scripts.
My Jenkins version: 1.5.39
Execute steps:
Step 1. Go to folder /Library/Application Support/Jenkins
Step 2. Run Uninstall.command jenkins-runner.sh file.
Step 3. Check result.
It work for me.
Run the following commands to completely uninstall Jenkins from MacOS Sierra. You don't need to change anything, just run these commands.
sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm -rf /Applications/Jenkins '/Library/Application Support/Jenkins' /Library/Documentation/Jenkins
sudo rm -rf /Users/Shared/Jenkins
sudo rm -rf /var/log/jenkins
sudo rm -f /etc/newsyslog.d/jenkins.conf
sudo dscl . -delete /Users/jenkins
sudo dscl . -delete /Groups/jenkins
pkgutil --pkgs
grep 'org\.jenkins-ci\.'
xargs -n 1 sudo pkgutil --forget
Salam
Shah
On Mac; these two below commands completely remove Jenkins from your machine. just open your Terminal and execute them:
'/Library/Application Support/Jenkins/Uninstall.command' and
sudo rm -rf /var/root/.jenkins ~/.jenkins
Thanks

Resources