Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lnesland

macrumors newbie
Original poster
Jan 18, 2006
4
0
Sweden
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?
 

fimac

macrumors member
Jan 18, 2006
95
1
Finland
(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
 

reikon

macrumors newbie
Jan 15, 2006
20
0
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
 

lnesland

macrumors newbie
Original poster
Jan 18, 2006
4
0
Sweden
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?
 

reikon

macrumors newbie
Jan 15, 2006
20
0
lnesland said:
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?

Depends on what that small program is written in. OR if you use package maker, etc.
 

lnesland

macrumors newbie
Original poster
Jan 18, 2006
4
0
Sweden
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 ?
 

reikon

macrumors newbie
Jan 15, 2006
20
0
lnesland said:
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 ?

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
 

fimac

macrumors member
Jan 18, 2006
95
1
Finland
reikon said:
Or you can put all the files into an array in the bash script and use a for loop to loop through the array.
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.