Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File permission 2

Status
Not open for further replies.

onceagain

Programmer
Apr 5, 2004
4
US
Hi,

I need to write a shell script for changing the permissions for certain files in our system (example all files with extension .rpt). The script has to generate a backup script which will have chmod command to change the permissions back to the original. I have written upto a point that I use "find" command to find the files and save the output in a .lst file and do an ls -l on each line in the file after looping and using awk to get the original permission of format (rwxr-x---). This is available in a variable now. I am stuck here. How could I generate the chmod command while looping through the above string (stored in the variable). Any help??

Thanks in advance,

onceagain
 
Hi onceagain,

It is so surprising that you have a similar question which I am attending to. Let us see who will be able to help us out. (Check my post "loop through a string")

Thanks
dbadmin
 
What permissions are you trying to give each file? I don't quite understand that. Why do you have to test it?

There are seveal ways to process a bunch of files without creating a temporary file. Something like...
Code:
find / -name '*.rpt' -exec chmod 750 {} \;
This command will set all files ending with [tt].rpt[/tt] to [tt]rwxr-x---[/tt].

Hope this helps.
 
I reread your post. If you're trying to have it create a script that will restore the [tt].rpt[/tt] files' permissions to their original values if they happen to change, then this might do it...
Code:
#!/bin/ksh

SCRIPT=~/restore_rpt_perms.sh

function mkperm {
    [[ $2 = "---" ]] && return

    print "${3}${1}=$(print ${2}|sed 's/-//g')"
}

find / -type f -name '*.rpt' -print | while read FILE
do
    PERMS=$(ls -l ${FILE}|awk '{print $1}')
    PERMS=${PERMS#?}

    U=${PERMS%??????}
    G=${PERMS#???} ; G=${G%???}
    O=${PERMS#??????}

    print "chmod $(mkperm u "${U}")$(mkperm g "${G}" ",")$(mkperm o "${O}" ",") ${FILE}"
done > ${SCRIPT}

chmod +x ${SCRIPT}
This will produce a script with lines that set the perms in absolute mode, such as...
Code:
chmod u=rwx,g=rx,o=r /path_to/filename.rpt
Let me know if this is what you're looking for.

Hope this helps.
 
Hi

Have you read about [tt]stat[/tt] ? I think this is what you need :
Code:
[blue]master # [/blue] ls -l *able
total 0
-r--r-----    1 master  users           0 2005-11-04 08:56 readable
---x--x---    1 master  users           0 2005-11-04 08:57 runnable
--w--w----    1 master  users           0 2005-11-04 08:56 writable

[blue]master # [/blue] stat -c "chmod %a '%n'" *able > setpermback.sh

[blue]master # [/blue] cat setpermback.sh
chmod 440 'readable'
chmod 110 'runnable'
chmod 220 'writable'

[blue]master # [/blue] chmod ug-rwx *

[blue]master # [/blue] ls -l *able
----------    1 master  users           0 2005-11-04 08:56 readable
----------    1 master  users           0 2005-11-04 08:57 runnable
----------    1 master  users           0 2005-11-04 08:56 writable

[blue]master # [/blue] . setpermback.sh

[blue]master # [/blue] ls -l *able
-r--r-----    1 master  users           0 2005-11-04 08:56 readable
---x--x---    1 master  users           0 2005-11-04 08:57 runnable
--w--w----    1 master  users           0 2005-11-04 08:56 writable

Feherke.
 
I don't think you'll find stat on many non-Linux systems. onceagain didn't say what he/she is using... handy tip though.

Annihilannic.
 
Hi

Hmm... Yes, the [tt]stat[/tt] I use is from the GNU fileutils package. But [tt]ch(mod|grp|own)[/tt] are also from that package. I hoped that is a common utility.

Feherke.
 
Hi SamBones,

Thanks a lot for the script. I tried it and is working OK if I have all permissions assigned for a file (for owner, group and others) but when there is no permission for any of these, then the script fails, because the restore_rpt_perms.sh file will have entries like this

chmod u=w,g=w,o= /usr/home/data1.rpt

which will not execute. I am trying to fix it. Would you please help me??

Thanks again
onceagain
 
Hi SamBomes,

I got it. In the [[ $2 = "---" ]] && return, we only need one square brackets like this [ $2 = "---" ]

Hope this will help dbadmin also.

Thanks anyway.
onceagain
 
What OS are you on? That should work in a real Korn shell.

But, if it works, it works I guess.

[bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top