LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-03-2003, 05:05 PM   #1
atwah
LQ Newbie
 
Registered: Sep 2003
Location: London
Distribution: Mandrake
Posts: 4

Rep: Reputation: 0
Question 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
 
Old 09-03-2003, 07:17 PM   #2
dubman
Member
 
Registered: Jan 2003
Distribution: Redhat 9, Fedora Core 1, Suse 8
Posts: 188

Rep: Reputation: 30
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
 
Old 09-03-2003, 07:18 PM   #3
dubman
Member
 
Registered: Jan 2003
Distribution: Redhat 9, Fedora Core 1, Suse 8
Posts: 188

Rep: Reputation: 30
sorry..that should be

#!/bin/bash

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

for item in $tmpdir
do
echo $item
done
 
Old 09-03-2003, 08:27 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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

Last edited by Dark_Helmet; 09-03-2003 at 08:30 PM.
 
Old 09-04-2003, 05:10 PM   #5
dubman
Member
 
Registered: Jan 2003
Distribution: Redhat 9, Fedora Core 1, Suse 8
Posts: 188

Rep: Reputation: 30
aww yes, dark helmet, much better.

--dubman
 
Old 09-04-2003, 05:37 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,369
Blog Entries: 54

Rep: Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869Reputation: 2869
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.
 
Old 09-09-2003, 01:10 AM   #7
atwah
LQ Newbie
 
Registered: Sep 2003
Location: London
Distribution: Mandrake
Posts: 4

Original Poster
Rep: Reputation: 0
sorry for the delay...

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
Bash Scripting help jgtg32a Programming 5 09-06-2005 09:38 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM


All times are GMT -5. The time now is 07:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration