How to make a simple bash script?

Discussion in 'Mac Programming' started by lnesland, Jan 18, 2006.

  1. macrumors newbie

    Joined:
    Jan 18, 2006
    Location:
    Sweden
    #1
    Anyone know how to make a simple bash script in 10.4? Basically I am kind of new to bash. All I need to do is make the program to do is go to this folder delete a file, then move to the next folder remove the whole folder, next folder and remove this file and so on. Only 10-15 steps. Today I have to fully uninstall this certain program, and I want to automate it through a bash or some other programming language.. Anybody have a clue how to do this?
     
  2. macrumors member

    Joined:
    Jan 18, 2006
    Location:
    Finland
    #2
    (I have to say, this seems to be a rather specific task that does not benefit much from scripting. But all learning is good :) and I hope the following information is useful for you.)

    Use google to find an introduction to bash, e.g. http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html, and to find manual pages, e.g. http://www.hmug.org/man/1/rm.php. You can also read man pages in Terminal, type:
    Code:
    man rm
    If we assume you have made a list of the files and folders, then it is enough to issue the bash remove command, "rm":

    files.lst
    Code:
    /Users/me/Applications/appl
    /Users/me/Library/appl/appl.plist
    /path/to/something
    Open Terminal, and type:
    Code:
    cat files.lst | while read line ; do rm -rf "$line" ; done
    If you prefer to see this as a bash script, then:

    remove_files.sh
    Code:
    #!/bin/bash
    while read line ; do rm -rf "$line" ; done
    Notice how the "cat files.lst" is missing from the script: this was done to make the script more general (you can use the script with other lists of files). Furthermore, I used the while loop so that filenames with spaces in them would be handled correctly. To run this from Terminal, type:
    Code:
    cat files.lst | ./remove_files.sh
     
  3. macrumors newbie

    Joined:
    Jan 15, 2006
    #3
    Or you can put all the files into an array in the bash script and use a for loop to loop through the array.

    Code:
    #!/bin/bash
    
    THE_FILES=("/Path/to/File" "/path/to/file" "/path/to/folder")
    
    for (( i = 0; i < ${#THE_FILES[@]}; i++ ))
    do
    	rm -rf ${THE_FILES[$i]}
    	echo "Removing ${THE_FILES[$i]}"
    done
    
     
  4. thread starter macrumors newbie

    Joined:
    Jan 18, 2006
    Location:
    Sweden
    #4
    Thanks :) What if I want to rap this into a small program? Like "hit this button" or run this program and hit enter to uninstall these files?
     
  5. macrumors newbie

    Joined:
    Jan 15, 2006
    #5
    Depends on what that small program is written in. OR if you use package maker, etc.
     
  6. thread starter macrumors newbie

    Joined:
    Jan 18, 2006
    Location:
    Sweden
    #6
    OK. Maybe I can write it in C++ in that program was it called ASS?
     
  7. macrumors newbie

    Joined:
    Jan 15, 2006
    #7
    AppleScript Studio? lol
     
  8. thread starter macrumors newbie

    Joined:
    Jan 18, 2006
    Location:
    Sweden
    #8
    Yes :) So with AppleScript Studio I can make this small program delete files and folders I assume? Just like run the program and hit enter and it will delete those files? Or can I just use bash and save this and just run it ?
     
  9. macrumors newbie

    Joined:
    Jan 15, 2006
    #9
    Yeah you can do that. AppleScript:

    Code:
    display dialog "Do you want to remove THE_APP?" buttons {"Cancel", "OK"} default button 2
    set theButton to button returned of result
    if theButton = "OK" then
    	do shell script "
    #!/bin/bash
    
    THE_FILES=(\"/Path/to/File\" \"/path/to/file\" \"/path/to/folder\");
    
    for (( i = 0; i < ${#THE_FILES[@]}; i++ ))
    do
    	rm -rf ${THE_FILES[$i]}
    	echo \"Removing ${THE_FILES[$i]}\"
    done
    "
    end if
    Be sure to escape the quotes within the quotes. :p
     
  10. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #10
    You can easily call a shell script with an automator action.
     
  11. macrumors member

    Joined:
    Jan 18, 2006
    Location:
    Finland
    #11
    Indeed. But there was a minor quoting mistake, the variable should be quoted so that filenames with embedded spaces are handled correctly:

    Code:
    rm -rf [B]"[/B]${THE_FILES[$i]}[B]"[/B]
    BTW, the following is a neat way to see what is going on in your script, without using debugging echos :)
    Code:
    bash -x remove_files.sh
     

Share This Page