How to create a shell script and run it?

Discussion in 'Mac Programming' started by Killwin98, Mar 14, 2013.

  1. Killwin98 macrumors member

    Jan 24, 2013
    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 :


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

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

    Thanks for your help!
  2. switon, Mar 14, 2013
    Last edited: Mar 14, 2013

    switon macrumors 6502a

    Sep 10, 2012
    RE: Creating and executing a bash shell script...

    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.

    If you use an editor, then you can create the "" 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 "" 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 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*
    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 "" 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 "" shell script file in your Home folder. Remember to hit the "Return" key at the end of each line as you type it...

    Create file by typing the following in a Terminal window:
    cat >
    rm -rfd ~/Library/Preferences/*
    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 "" file in your Home folder. Let's make certain that it is there.

    ls -al
    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
    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
    And let's check that the file is now executable:
    ls -al
    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

    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 "" shell script:
    this command executes the "" 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.

  3. Killwin98 thread starter macrumors member

    Jan 24, 2013
    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 ./
    it asked me for my admin password, and it worked!

    So again, thank you from the bottom of my heart!
  4. switon macrumors 6502a

    Sep 10, 2012
    RE: great! ...

    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.


Share This Page