Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-07-2010, 03:04 AM   #1
LQ Newbie
Registered: May 2010
Posts: 7

Rep: Reputation: 0
Question problem with blank spaces in shell script


I did the following script:
DIR=`pwd`;for i in `find . -name GESTION -type d`;do cd $i/..;setfacl -R -m g:directores:rwx,
GESTION;echo $DIR'/'$i;cd $DIR;done
This code do the following actions:

1. Starting inside a folder, it's searching for any folder called "GESTION"
2. Every time a folder called "GESTION" is found, then the script move to its parent folder, and then it applies ACL permissions to the folder GESTION by using the command setfacl.

The problem is that the script doesn't works when there was found a folder with a blank space in its name.
I mean:

/dir1/dir2/dir3/GESTION --> this works fine

/dir1/dir2/dir 3/GESTION --> this doesn't works
/dir1/dir2/dir3/2. GESTION --> this doesn't works

Thank you for your help.
Old 05-07-2010, 03:27 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
For convenience, here's the script prettified (but I'm not sure about the line folding for g:directores:rwx,
for i in `find . -name GESTION -type d`
    cd $i/..
    setfacl -R -m g:directores:rwx,GESTION
    echo $DIR'/'$i
    cd $DIR
Regards "/dir1/dir2/dir3/2. GESTION --> this doesn't works", I don't think the find command would find it because -name GESTION would not match "2. GESTION" ???

I am not familiar with setfacl but the man page does not require a pathless value for file in
setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
Would the following work (assuming directories called *GESTION* are to be processed)
while IFS= read -r -d '' dir
   setfacl -R -m g:directores:rwx,"$dir"
done < <(find . -type d -name '*GESTION*' -print0)

Last edited by catkin; 05-07-2010 at 03:30 AM. Reason: gibberish->sense
Old 05-07-2010, 03:31 AM   #3
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33

about IFS
change the IFS (internal field separator) value to newline only, etc, before the part with the spaces in it is evaluated.
Old 05-07-2010, 03:35 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Your problem is word splitting. On some operations your shell automatically breaks strings up based on the settings of the internal field separator. The easiest way to handle spaces in a script is to reset the IFS so that it ignores spaces (it's usually set to space/tab/newline).

oldIFS=$IFS    #save the initial settings first if you need to reset it back for some reason.

IFS=$'\n'    #set IFS to newline only.

...your script.
Note that the above only works in some shells, particularly current bash versions. For others you'll have to use a literal newline.
If you don't set the IFS, or even if you do, you need to properly quote the output of variables to prevent word splitting. e.g.:
for i in <list of directories>; do
  echo "$i"
PS: it would be helpful if you formatted your code so that it was a bit more readable when posting here. It was a bit hard to read that run-on command.

PPS: In most modern shells, "``" has been superceded by $() for embedded commands, for several reasons.

Last edited by David the H.; 05-07-2010 at 03:40 AM. Reason: added pps
Old 05-07-2010, 04:54 AM   #5
LQ Newbie
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0

Thank you very much for all your answers!!

The final code is this:

for i in $(find . -name "$WORD" -type d);do 
   cd $i/..;setfacl -R -m g:calidad:rx $WORD; 
   echo $DIR'/'$i >> $LOG; 
   cd $DIR; 
and it's working fine.


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
[SOLVED] SED - how to remove blank spaces carolflb Linux - Newbie 2 01-30-2010 06:02 AM
list filenames with spaces in a shell script xp_newbie Programming 6 03-15-2009 07:46 PM
omit white spaces for pattern matching in shell script bharatbsharma Programming 4 09-18-2008 08:25 PM
Problem with SED and blank spaces BigLarry Programming 2 06-10-2004 04:57 AM
mp3 files - replacing blank spaces with _ script? mymojo Linux - Newbie 4 12-08-2003 03:33 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:47 AM.

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