By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,482 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,482 IT Pros & Developers. It's quick & easy.

Unix script for permissions

P: 16
How do i write a script that takes a file as an argument and determines what permissions the owner, group and everybody has for the file?

I want to view roughly like this.
READ WRITE EXECUTE
OWNER MATT.BALLANCORE YES YES NO
GROUP USERS YES NO NO
EVERYBODY NO NO NO
Jan 15 '08 #1
Share this Question
Share on Google+
3 Replies

numberwhun
Expert Mod 2.5K+
P: 3,503
How do i write a script that takes a file as an argument and determines what permissions the owner, group and everybody has for the file?

I want to view roughly like this.
READ WRITE EXECUTE
OWNER MATT.BALLANCORE YES YES NO
GROUP USERS YES NO NO
EVERYBODY NO NO NO

What have you tried thus far to do this? This sounds a bit like school work to me. If it is, then please post your script to do this and we can help you troubleshoot it, but please know that posting schoolwork on the site (especially expecting the answer(s)) is against the TSDN Posting Guidelines.

Regards,

Jeff
Jan 15 '08 #2

P: 16
What have you tried thus far to do this? This sounds a bit like school work to me. If it is, then please post your script to do this and we can help you troubleshoot it, but please know that posting schoolwork on the site (especially expecting the answer(s)) is against the TSDN Posting Guidelines.

Regards,

Jeff
Hi Jeff, thanks for the reply.

This isn't homework, its an exercise apparrently designed to further my learning. However i'm not learning. I've been asked to further research on how to code this. Let me add, i am a complete newbie to unix. I'm familiar with the bash interface but scripting i'm finding hard to grasp. So far from my online training i've come across the the chmod command which is for permissions. I understand echo will print the text desired and > will redirect it to a file. However i have no clue to how to form the syntax. I'm use to C++ programming style and UNIX truely baffles me. Your help will be much apprieciated.
Jan 16 '08 #3

P: 16
My latest attempt is :-

ls -la|grep " $1"$ | tr "\t" " " | tr -s " " | tr " " "\n" > tfile
permissions=$(head -1 tfile)
owner=$(head -3 tfile|tail -1)
rm tfile
powner=$(print -- $permissions|cut -c 2-4)
pgroup=$(print -- $permissions|cut -c 5-7)
pusers=$(print -- $permissions|cut -c 8-10)
print Filename: $1
print Owner: $owner
printf "Permissions:\tRead\tWrite\tExecute\n"

case $powner in
---)
printf "Owner:\t \tNO\tNO\tNO\n"
;;
r--)
printf "Owner:\t\tYES\tNO\tNO\n"
;;
rw-)
printf "Owner:\t\tYES\tYES\tNO\n"
;;
r-x)
printf "Owner:\t\tYES\tNO\tYES\n"
;;
rwx)
printf "Owner:\t\tYES\tYES\tYES\n"
;;
-w-)
printf "Owner:\t\tNO\tYES\tNO\n"
;;
-wx)
printf "Owner:\t\tNO\tYES\tYES\n"
;;
--x)
printf "Owner:\t\tNO\tNO\tYES\n"
;;
esac

case $pgroup in
---)
printf "Group:\t\tNO\tNO\tNO\n"
;;
r--)
printf "Group:\t\tYES\tNO\tNO\n"
;;
rw-)
printf "Group:\t\tYES\tYES\tNO\n"
;;
r-x)
printf "Group:\t\tYES\tNO\tYES\n"
;;
rwx)
printf "Group:\t\tYES\tYES\tYES\n"
;;
-w-)
printf "Group:\t\tNO\tYES\tNO\n"
;;
-wx)
printf "Group:\t\tNO\tYES\tYES\n"
;;
--x)
printf "Group:\t\tNO\tNO\tYES\n"
;;
esac

case $pusers in
---)
printf "Users:\t\tNO\tNO\tNO\n"
;;
r--)
printf "Users:\t\tYES\tNO\tNO\n"
;;
rw-)
printf "Users:\t\tYES\tYES\tNO\n"
;;
r-x)
printf "Users:\t\tYES\tNO\tYES\n"
;;
rwx)
printf "Users:\t\tYES\tYES\tYES\n"
;;
-w-)
printf "Users:\t\tNO\tYES\tNO\n"
;;
-wx)
printf "Users:\t\tNO\tYES\tYES\n"
;;
--x)
printf "Users:\t\tNO\tNO\tYES\n"
;;
esac

doesn't work tho :(

apparrently can be done within 10-25 lins without tmp file or the use of awk or sed commands
Jan 16 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.