PDA

View Full Version : Bash scripting help/request



Shoeberto
06-04-2007, 06:28 PM
I have a folder with a bunch of subfolders that I want to compress invidiually using 7zip. I really have no clue how to go about it.

It should go something like so:
What is the nth's folder's name? Nth folder's name is "x". Compress "x" as "x".7z. n++. Loop.

(basically atm I can't be arsed to figure out how to do it myself yayayayay)

Endless
06-04-2007, 08:49 PM
Something like

for file in `ls -p1`;
do
if [ -d "$file" ]
command for 7zip with $file as the parameter
endif
done

Shoeberto
06-04-2007, 09:43 PM
I had to change a few things (fi is used instead of endif, and using ls -p made new directories with 7zip instead of just spitting out the file), but that works great. Much better than the weird crap I was trying to pull off with arrays, anyway. Thanks.

Shoeberto
06-05-2007, 12:43 AM
Quick problem:
It doesn't accept any directories that have spaces in them. Is there a workaround to this or am I just going to have to suck it up and rename my folders? (they're game titles, so I'd like to keep the spacing if possible)

edit: Nevermind, found a fix by putting "IFS=$'\n'" before the loop.

crono_logical
06-05-2007, 05:37 AM
I shall remember that - I've had problems with scripting and filenames with spaces in before too :p

Endless
06-05-2007, 07:27 AM
Gah, I had fixed the endif to fi while I was testing it and writing the post, but forgot to actually make the change in my post before submitting. xD

The ls -p was there because at first I wanted to test if a element in $file was a dir by checking if it ended with a "\", but the -d test made that useless, and I forgot to remove the p switch.