LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   HELP with BASH scripting (http://www.linuxquestions.org/questions/linux-newbie-8/help-with-bash-scripting-88954/)

atwah 09-03-2003 06:05 PM

HELP with BASH scripting
 
ok, a very very newbie question about BASH scripting.

I'm trying to rename multiple files from a set of directories, but
their names have spaces and I can't just list the names right.

The problem is in the "for file ... bla bla bla"


for file in $(find -type f -printf "\"%p\"\n" | grep .bak)
do
echo $file
done


in the following directory name:


dirname with spaces/filename.bak


produces this:


./dirname
with
spaces/filename.bak


that is 3 different things, instead of:

./dirname with spaces/filename.bak


I've even tried replacing the -printf with this:

-printf "\"%p\"\n"

but then it produces this:

"./dirname
with
spaces/filename.bak"


any help to get the name of the file right? what am I doing wrong?



Thnx

dubman 09-03-2003 08:17 PM

not sure if this helps....but hopw bout this syntax:

#!/bin/bash

tmpdir=/directory\name\with\spaces/sub\directory\name\with\spaces

for item in $tmpdir
do
echo $item
done

--hope this helps
dubman

dubman 09-03-2003 08:18 PM

sorry..that should be

#!/bin/bash

tmpdir=/directory\name\with\spaces/sub\directory\name\with\spaces/*

for item in $tmpdir
do
echo $item
done

Dark_Helmet 09-03-2003 09:27 PM

You need to change the IFS shell variable temporarily.

Pull up the bash man page and search for either IFS or "Word splitting"

EDIT:

Perhaps something like:
Code:

#!/bin/bash

OLD_IFS=$IFS

IFS="\n"

for file in $(find -type f -printf "\"%p\"\n" | grep .bak)
do
  echo $file
done

IFS=$OLD_IFS


dubman 09-04-2003 06:10 PM

aww yes, dark helmet, much better.

--dubman

unSpawn 09-04-2003 06:37 PM

OTOH, you could use a "while" loop. Useless example:
Code:

find <somedir> -type f -name \*.bak| while read; do
echo mv "$REPLY" "$(basename "$REPLY" .bak).abk"; done

This saves you one grep, and for the lazy, if you don't feed "read" a variable then it stuffs whatever it reads in the default var called "REPLY". Remember proper quoting is important when you (dont) expect spaces: check your script running it as "sh -x <script>" or "set -x" if you're working in the CLI.

atwah 09-09-2003 02:10 AM

sorry for the delay...

and thanx for your help. it was exactly what I needed. ;)


All times are GMT -5. The time now is 06:29 PM.