LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-07-2010, 04:04 AM   #1
argie01
LQ Newbie
 
Registered: May 2010
Posts: 7

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


Hi,

I did the following script:
Code:
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, 04:27 AM   #2
catkin
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,
GESTION)
Code:
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
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
Code:
setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
Would the following work (assuming directories called *GESTION* are to be processed)
Code:
while IFS= read -r -d '' dir
do
   setfacl -R -m g:directores:rwx,"$dir"
done < <(find . -type d -name '*GESTION*' -print0)

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

Rep: Reputation: 33
IFS

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

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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).

Code:
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.
Code:
IFS='
'
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.:
Code:
for i in <list of directories>; do
  echo "$i"
done
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 04:40 AM. Reason: added pps
 
Old 05-07-2010, 05:54 AM   #5
argie01
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Talking

Thank you very much for all your answers!!

The final code is this:

Code:
oldIFS=$IFS;
IFS=$'\n'; 
WORD='7. GESTION'; 
LOG='/home/samba/logN04.txt';
DIR=$(pwd); 
for i in $(find . -name "$WORD" -type d);do 
   cd $i/..;setfacl -R -m g:calidad:rx $WORD; 
   echo $DIR'/'$i >> $LOG; 
   cd $DIR; 
done;
IFS=$oldIFS
and it's working fine.
 
  


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


All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration