PDA

View Full Version : How to create a shell script and run it?




Killwin98
Mar 14, 2013, 04:11 AM
Hello,
I'm using mountain lion and I have obscured instructions on how to create a shell script and run it, and I don't understand how. Could you help me step by step please?

This is a part of the instructions:

"Add this to a resetPa.sh :

#!/bin/bash

rm -rfd ~/library/preferences/com.pa*
rm -rfd ~/library/preferences/pa/*
rm -rfd ~/library/preferences/pa

I searched in spotlight and I don't seem to have a file called resetPa.sh
Do I need to create it? How? And how do I continue from here?

Thanks for your help!



switon
Mar 14, 2013, 06:43 AM
Hello,
I'm using mountain lion and I have obscured instructions on how to create a shell script and run it, and I don't understand how. Could you help me step by step please?

This is a part of the instructions:

"Add this to a resetPa.sh :

#!/bin/bash

rm -rfd ~/library/preferences/com.pa*
rm -rfd ~/library/preferences/pa/*
rm -rfd ~/library/preferences/pa

I searched in spotlight and I don't seem to have a file called resetPa.sh
Do I need to create it? How? And how do I continue from here?

Thanks for your help!

Hi Killwin98,

First of all, allow me to describe what is happening and how we are going to proceed. Then I will give the exact commands to use.

Explanation:
If you use an editor, then you can create the "resetPa.sh" bash shell script. The line that starts with "#!/bin/bash" must be the first line of the file in order for this shell script to work correctly (it tells the terminal shell to use the bash shell to execute the following commands). The "rm -rfd ..." lines remove files from your own account's preferences directory, "~/Library/Preferences". The "-rfd" options tells the "rm" remove command to act recursively (-r), to force (-f) the remove, and to remove directories (-d) also. In other words, these commands will remove all files and subdirectories without asking for permission. As such, two of these three commands are actually redundant, but let's not get into that now.

Once you create the "resetPA.sh" file, then you make it executable by doing a "chmod" (change mode) command. Once it is executable, then you can execute the file.

Now for the commands to be typed into a Terminal window ... below I assume that you don't use an editor.

Open a Terminal window, the Terminal.app is located in the Applications -> Utilities folder. Click on it to open a Terminal window. In the Terminal window, type the following commands included below the "Code:" sections. You must hit the "Return" key at the end of each line to execute that line's instruction and proceed to the next line. That funny squiggle in front of "/Library" is the tilde character (far left shifted key on the second row of your keyboard). None of the following Terminal commands should return an error ... if they do, then something has gone wrong.

Check if you even have the "pa" subdirectory:
cd ~/Library/Preferences
ls -al com.pa*
ls -al pa
these commands should return a listing of these files and directories. If it doesn't, that is, if it says these files and directories do not exist, then you have been mislead and your "resetPa.sh" shell script will be useless. So, assuming that these files exist, proceed to the following. (Note: For instance, I do not have the "pa" directory in my ~/Library/Preferences directory.)

We are going to put the "resetPa.sh" shell script file in your Home folder. Remember to hit the "Return" key at the end of each line as you type it...

Create resetPA.sh file by typing the following in a Terminal window:
cd
cat > resetPa.sh
#!/bin/bash

rm -rfd ~/Library/Preferences/com.pa*
rm -rfd ~/Library/Preferences/pa/*
rm -rfd ~/Library/Preferences/pa

and now you must press control-D (^D), that is, hit the "control" key and hold it while hitting the "d" key on your keyboard. This creates the "resetPa.sh" file in your Home folder. Let's make certain that it is there.

ls -al resetPa.sh
this command should return a line similar to the following line, but with <username> replaced with your user name and the time stamp being the time you created your own file:
-rw-r--r--+ 1 <username> staff 118 Mar 14 05:14 resetPa.sh

We now change the mode of this shell file so that it is executable using the "chmod" command.
Change mode to executable:
chmod u+x resetPa.sh

And let's check that the file is now executable:
ls -al resetPa.sh
should return the following (note the rwx in the following line indicates that the file is readable, writable, and executable):
-rwxr--r--+ 1 <username> staff 118 Mar 14 05:14 resetPa.sh


The only thing left to do is to execute the file. There are several ways to do this, so below I have chosen one way.

Execute the "resetPa.sh" shell script:
. resetPa.sh
this command executes the "resetPa.sh" bash shell script which performs its tasks of deleting your preferences for "pa", whatever "pa" is. (Note the beginning dot followed by a space followed by the bash file name.)

Well, I hope all went well and you successfully created and executed the shell script to delete the "pa" files from your preferences.

Regards,
Switon

Killwin98
Mar 18, 2013, 12:27 PM
Hello Switon!
Thank you for your quick reply and sorry for the late answer.
Your instructions were so clear, and I really thank you for that. The only problem is that after running the script it couldn't remove the files and folders ("permission denied"), so I ran
sudo ./resetPa.sh
it asked me for my admin password, and it worked!

So again, thank you from the bottom of my heart!

switon
Mar 18, 2013, 06:39 PM
Hi Killwin98,

Glad it worked for you! Sorry I didn't think about you not having permission to delete the files. Basically, since the files were in your own ~/Library/Preferences directory, I assumed that you would have owned them since you should have written the files in the first place, but I guess not (for instance, in my ~/Library/Preferences directory not only do I own all of the files and subdirectories, but I also have R/W privileges on everything and thus I can delete any files and subdirectories with doing sudo). But, in any case, it was very smart of you to think to run the shell script under sudo so that with root privileges the files could be deleted.

Regards,
Switon