PDA

View Full Version : How to make a simple bash script?




lnesland
Jan 18, 2006, 05:25 AM
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
Jan 18, 2006, 06:25 AM
(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:
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
/Users/me/Applications/appl
/Users/me/Library/appl/appl.plist
/path/to/something
Open Terminal, and type:
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
#!/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:
cat files.lst | ./remove_files.sh

reikon
Jan 18, 2006, 06:56 AM
Or you can put all the files into an array in the bash script and use a for loop to loop through the array.


#!/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
Jan 18, 2006, 07:42 AM
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
Jan 18, 2006, 07:43 AM
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
Jan 18, 2006, 07:58 AM
OK. Maybe I can write it in C++ in that program was it called ASS?

reikon
Jan 18, 2006, 07:59 AM
OK. Maybe I can write it in C++ in that program was it called ASS?

AppleScript Studio? lol

lnesland
Jan 18, 2006, 08:06 AM
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
Jan 18, 2006, 08:18 AM
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:

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

gekko513
Jan 18, 2006, 08:30 AM
You can easily call a shell script with an automator action.

fimac
Jan 18, 2006, 08:55 AM
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:

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