LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2005, 10:57 PM   #1
kule
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 87

Rep: Reputation: 16
bash for + find problem


I made a script that changes ownership and chmod of files and dirs in a folder from which I execute it. Here is the script:
Code:
#!/bin/bash
#

EXECMOD=700
FILEMOD=600
USER=root
GROUPE=root

for FILE in $( find . )
    do
    #Change ownerships
    chown -v $USER:$GROUPE "$FILE"

    #Give folders exec mod
    if [ -d "$FILE" ]
        then
        chmod -v $EXECMOD "$FILE"

    #If file is a bash script give it exec mod
    elif head -n 1 "$FILE" | grep "#!/bin/bash" 1>/dev/null || head -n 1 "$FILE" | grep "#!/bin/sh" 1>/dev/null
        then
        chmod -v $EXECMOD "$FILE"

    #If file ends with *.sh, *.csr, *.bin or starts with rc.*, give exec mod
    elif [ -f "$FILE" ] && echo "$FILE" | grep *.sh 1>/dev/null || echo "$FILE" | grep *.csh 1>/dev/null || echo "$FILE" | grep *.bin 1>/dev/null || echo "$FILE" | grep rc.* 1>/dev/null
        then
        chmod -v $EXECMOD "$FILE"

    #Else give plain (noexec)  mod
    else
        chmod -v $FILEMOD "$FILE"
    fi
    done
My problem is that when "find" finds a file which has a space in its filename, var $FILE becomes part of the filename and not the whole name of a file, for example:

find found:
U2 - Bad.mp3

$FILE="U2", then $FILE="-" and $FILE=Bad.mp3 and it should be $FILE="U2 - Bad.mp3".

Is there a way this could be done? Or do you have a better idea for my script
 
Old 02-21-2005, 11:11 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
find . | read FILE
do
...
done
 
Old 02-22-2005, 04:40 AM   #3
kule
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 87

Original Poster
Rep: Reputation: 16
I don't know how to put that into a loop.
And I think that doesn't work.

Last edited by kule; 02-22-2005 at 05:09 AM.
 
Old 02-22-2005, 05:47 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
I don't know how to put that into a loop.
That way (sorry I missed the while in my previous reply):
Code:
find . | while read file
do
...
done
Quote:
And I think that doesn't work.
Well, you should as it does ...
 
Old 02-22-2005, 09:14 AM   #5
kule
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 87

Original Poster
Rep: Reputation: 16
I'll try it. Thanks in advance.
 
Old 02-22-2005, 11:08 AM   #6
kule
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 87

Original Poster
Rep: Reputation: 16
It doesn't work, it works same as:

for FILE in $( find . )

it splits the filename into parts if there is a space in it
 
Old 02-22-2005, 11:45 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by kule
It doesn't work, it works same as:

for FILE in $( find . )

it splits the filename into parts if there is a space in it
Creating test files:
Code:
mkdir test
cd test
touch aaa
touch bbb
touch "ccc ddd"
touch eee
Run shell code:
Code:
find .  | while read file ; do  echo $file; done
Output:
Code:
.
./aaa
./bbb
./ccc ddd
./eee
Seems you were wrong...
 
Old 02-22-2005, 02:50 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Thanks for your support, Hko
 
Old 02-22-2005, 06:19 PM   #9
kule
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 87

Original Poster
Rep: Reputation: 16
well it didn't work for me... because I had:

Code:
find .  | while read file ; do chmod 644 $file; done
and not echo, but it worked when I put "" between $file:


Code:
find .  | while read file ; do chmod 644 "$file"; done
Thanks for help.
 
Old 02-23-2005, 04:48 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Okay, your initial script had not this bug, thus the confusion.
 
  


Reply



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 shell 'find' problem. ayiiq180 Linux - Software 1 04-03-2005 11:51 AM
trouble with find command in bash vrdhananjay Linux - Newbie 3 10-30-2004 11:37 PM
bash find results bendeco13 Linux - General 6 10-26-2004 11:48 AM
bash connot find the command Dunadan1821 Linux - Software 9 08-30-2004 08:04 AM
how to find out if argument is number: bash kubicon Linux - Newbie 1 09-19-2003 04:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration