Permissions script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Permissions script
# 1  
Old 01-10-2010
Permissions script

Hi there,

I've been having some frustrating problems with a permissions script. Basically I want it to to determine what permissions the owner, group and everybody has for the file passed in.
Here's what I've got:

Code:
#!bin/bash
echo -n "Enter filename: "
read file
owner=$( ls -l $file | cut -c 2,3,4 )
group=$( ls -l $file | cut -c 5,6,7 )
users=$( ls -l $file | cut -c 8,9,10 )
ownername=$( ls -l $file | cut -d " " -f 3 )

oread=NO
owrite=NO
oexec=NO
gread=NO
gwrite=NO
gexec=NO
uread=NO
uwrite=NO
uexec=NO

case $owner in
       *r*)    oread=YES ;;
       *w*)    owrite=YES ;;
       *x*)    oexec=YES ;;
esac

case $group in
        *r*)    gread=YES ;;
        *w*)    gwrite=YES ;;
        *x*)    gexec=YES ;;
esac

case $users in
        *r*)    uread=YES ;;
        *w*)    uwrite=YES ;;
        *x*)    uexec=YES ;;
esac
echo "                          READ    WRITE   EXECUTE" > perm
echo "OWNER $ownername  $oread  $owrite         $oexec" | tr '[a-z]' '[A-Z]' >> perm
echo "GROUP USERS               $gread  $gwrite         $gexec" >> perm
echo "EVERYBODY         $uread  $uwrite         $uexec" >> perm

It seems to display "YES" for all the permissions, even if this is not true. Any help would be appreciated
# 2  
Old 01-10-2010
Why did you chosen a case statement, it is wrong because, in a case statement only one case can be right.

So it means, if you have read permission, then you should not have write & execute. But this will not be the case at anytime.

Choose some other way, it is better.
# 3  
Old 01-10-2010
Ahh I see. I suppose another way would be to use loads of if statements then?
# 4  
Old 01-10-2010
I see why you're frustrated. I think you're thinking the case statement will continue to check each case even if it finds some that are true. Unfortunately there isn't a 'continue' command to allow this so when the first case it finds is true, it will break out.

What you will need to do it iterate through each permission and check if it is set or not. I've test this code out for you. Replace your 3 case statements with a for loop for each owner, group, and user set. Let me know how it goes.

Code:
for perm in r w x
do
  # Check if $perm is set, but we don't care to see the output
  echo $owner | grep ${perm} > /dev/null
  if [ $? -eq 0 ]   # if the result was true, then it is set
  then
    case $perm in
      r) oread=YES ;;
      w) owrite=YES ;;
      x) oexec=YES ;;
    esac
  fi
done

# 5  
Old 01-10-2010
Thank you very much, that works great now.
One more question, I don't quite understand why this line is used:
Code:
 echo $owner | grep ${perm} > /dev/null

Why are curly brackets used and why is the output directed to /dev/null ?

Last edited by user_invalid; 01-10-2010 at 09:52 AM..
# 6  
Old 01-10-2010
I have a habit of using curly braces around my variables to explicity state when the name of a variable starts and ends. Here's one example where it would be needed:

Code:
year=2010
month=01
echo ${year}_${month}   # echoes 2010_01
echo $year_$month   # echoes 01 because it looked for a variable called $year_ which I didn't define

Also without directing it to /dev/null, the output would show on the screen. Although your script would work, the end user doesn't really need to see this output. It was only done to test whether the permission was present, so we just needed "$?" that contained the success/failure code of that command.

Let me know if this helps or if you have any other questions.
# 7  
Old 01-17-2010
Sorry for the late reply. Thanks for all that it really helps and my bash scripting is coming along great now, learning so much everyday its a great feeling. I'm sure I'll be back at some point with more problems that frustrate me!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script for mkdir with permissions

Hello, I'm pretty new to scripting and trying to do a simple (well, I thought so) administrator task. I'm using bash. I want to create 10 directories under the one directory and apply permissions at the same time. I've worked out the make directories part: mkdir /userdata/folder{1..50}... (7 Replies)
Discussion started by: jimothy007
7 Replies

2. Shell Programming and Scripting

Help on script to change permissions

Hi All I have the following script that is supposed to change permissions of incoming files to a directory, but it does not seem to do what I want, please can you help: mkdir -p /tmp/tmpdir find /moneta_polled01/sgsn/ -exec ls -l {} \; |grep -v rwxrwxrwx |awk '{print $9}' >... (4 Replies)
Discussion started by: fretagi
4 Replies

3. Shell Programming and Scripting

Help on script to change permissions

Hi I have written the following script that later I want to put in cron,: #!/bin/bash _find="/usr/bin/find" _paths="/moneta_polled01/mediation_gsm /moneta_polled01/mediation_mmsc" for d in $_paths do $_find $d -type f -exec chmod 777 {} \; done but it does not seem to be... (8 Replies)
Discussion started by: fretagi
8 Replies

4. Shell Programming and Scripting

Script Permissions

I have a script were I issue the following command: perl -p -i -e "s/$v31a/$db2/" $dbn31 My script fails with permission error. However, at the command line I can edit the directory in question. I am using uname 002 in my script. So, why can i update manually but not in a script? (10 Replies)
Discussion started by: Harleyrci
10 Replies

5. Shell Programming and Scripting

How to check file permissions from a script.

hello, I have to write a script to run the other script inside it.So iam planning to write like this? first check the perimissions of the file. Alogorthim ---------- if(!filepermissions == execute) then echo" Permissions denined" else execute the script. file name is : load_mf.sh... (1 Reply)
Discussion started by: rajkumar_g
1 Replies

6. Shell Programming and Scripting

Call other script having different Permissions

Oops, infact i caught my mistake.... (1 Reply)
Discussion started by: suhail.sadaqat
1 Replies

7. UNIX for Dummies Questions & Answers

Permissions to files using batch script.

Hi all, I am using a batch script that ftps the compressed xml files to the Unix environment from the Windows environment and i perform this using the below line of coding: echo cd %VAR_TR_FTP_DIR% > %TR_FTP_BATCH_FILE BatchNum% Echo lcd "%VAR_TR_SOURCE_PATH_BatchNum%">> %TR_FTP_BATCH_FILE... (1 Reply)
Discussion started by: Codesearcher
1 Replies

8. UNIX for Dummies Questions & Answers

Changing permissions in a shell script

Hi All, I have a shell script which we keep on changing permissions on a On-Demand Basis-->for e:g--from 400(Read only) to 740(Execute permission) etc. Is there any way by which I can view the history of the script?-->I am interested in finding out the date-time stamps when the script's... (2 Replies)
Discussion started by: DevotedPupil
2 Replies

9. Shell Programming and Scripting

Modify Perl script to work with txt - Permissions script

Hi I have this code, and i want work with a ls -shalR output in .txt What i need read to do this?? Where start? #!/usr/bin/perl # Allrights- A perl tool for making backups of file permissions # Copyright (C) 2005 Norbert Klein <norbert@acodedb.com> # This program is free... (1 Reply)
Discussion started by: joangopan
1 Replies

10. Shell Programming and Scripting

Shell script permissions

Hi, I'm trying to execute a shell script from application (Informatica). The script works fine when I execute it from command line but fails when executed from Informatica. The tricky part is that there is other scripts in the same folder that can be executed succesfully from both command line... (1 Reply)
Discussion started by: tmikahan
1 Replies
Login or Register to Ask a Question