[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

script help for permissions

Posted on 2007-10-07
3
Medium Priority
?
305 Views
Last Modified: 2010-04-21
Hi,

I have a question that Im having difficulty with and require assistance. I want to write a simple shell script that holds a file as an argument. The main purpose of the script is to determine what permissions the owner, group and everybody has for the file passed in.

This is what I have produced so far

#!/bin/bash

perm=$(ls -l $1 | cut -f1 -d" ")
group=$(ls -l $1 | cut -f4 -d" " )
owner=$(ls -l $1 | cut  -f3 -d" ")
or=$(echo $perm | cut -b2)
ow=$(echo $perm | cut -b3)
ox=$(echo $perm | cut -b4)
gr=$(echo $perm | cut -b5)
gw=$(echo $perm | cut -b6)
gx=$(echo $perm | cut -b7)
er=$(echo $perm | cut -b8)
ew=$(echo $perm | cut -b9)
ex=$(echo $perm | cut -b10)

echo "For file: $1"
echo -e "\t\tREAD\tWRITE\tEXEC"
echo -e "\t\t----\t-----\t----"

echo -e "$owner\t\t$or\t$ow\t$ox"
echo -e "$group\t\t$gr\t$gw\t$gx"
echo -e "other\t\t$er\t$ew\t$ex"

I am using a LINUX OS and the output of the ls l command is shown below

total 264
-rw-r--r-- 1 adam.adebisi users   166 Sep 23 01:13 anotherdir
drwxr-xr-x 2 adam.adebisi users  4096 Sep 18 18:24 backups
-rw-r--r-- 1 adam.adebisi users  4687 Sep 26 00:30 big.demo
-rw-r--r-- 1 adam.adebisi users   348 Oct  1 14:59 checklength
drwxr-xr-x 9 adam.adebisi users  4096 Sep 26 15:03 coursework
-rw-r--r-- 1 adam.adebisi users   971 Sep 23 01:49 cust
-rw-r--r-- 1 adam.adebisi users    26 Sep 18 18:27 data1
-rw-r--r-- 1 adam.adebisi users     0 Sep 18 18:27 data2
-rw-r--r-- 1 adam.adebisi users  1194 Sep 26 02:01 demo
-rw-r--r-- 1 adam.adebisi users   922 Oct  4 13:01 dirfile
-rw-r--r-- 1 adam.adebisi users  2098 Sep 26 15:08 dir_listing
-rw-r--r-- 1 adam.adebisi users   196 Oct  3 18:02 display
-rw-r--r-- 1 adam.adebisi users    17 Oct  4 12:58 dog
-rw-r--r-- 1 adam.adebisi users     0 Oct  1 13:57 empty
-rw-r--r-- 1 adam.adebisi users    30 Oct  4 18:24 example
-rw-r--r-- 1 adam.adebisi users    51 Oct  4 18:27 example2
-rw-r--r-- 1 adam.adebisi users     0 Oct  4 21:00 file
-rw-r--r-- 1 adam.adebisi users    15 Sep 26 01:04 file1
-rw-r--r-- 1 adam.adebisi users    54 Sep 21 16:49 file2
-rw-r--r-- 1 adam.adebisi users    61 Sep 21 17:16 file3
-rw-r--r-- 1 adam.adebisi users    47 Sep 21 16:58 file4
-rw-r--r-- 1 adam.adebisi users    49 Sep 21 17:08 file5
-rw-r--r-- 1 adam.adebisi users    14 Sep 20 14:16 file8
-rw-r--r-- 1 adam.adebisi users     5 Oct  6 18:38 fileName
-rw-r--r-- 1 adam.adebisi users   470 Oct  6 20:06 firstline
-rw-r--r-- 1 adam.adebisi users   453 Oct  3 18:56 info
-rw-r--r-- 1 adam.adebisi users   232 Sep 21 13:02 io.demo
-rw-r--r-- 1 adam.adebisi users   152 Oct  1 14:50 letters
-rw-r--r-- 1 adam.adebisi users   122 Oct  5 11:30 me
-rw-r--r-- 1 adam.adebisi users   513 Oct  7 11:38 middleline
-rw-r--r-- 1 adam.adebisi users    22 Sep 18 18:17 myfile
-rwxr-xr-x 1 adam.adebisi users    93 Oct  5 11:12 myFirstScript
-rw-r--r-- 1 adam.adebisi users   192 Oct  1 14:57 name_age
-rw-r--r-- 1 adam.adebisi users     3 Oct  3 18:51 new
-rw-r--r-- 1 adam.adebisi users     0 Oct  6 18:18 newfile
-rw-r--r-- 1 adam.adebisi users     0 Oct  4 13:06 outfile
-rw-r--r-- 1 adam.adebisi users   422 Oct  7 12:09 permissions
-rw-r--r-- 1 adam.adebisi users   187 Sep 18 18:17 poem
-rw-r--r-- 1 adam.adebisi users   187 Sep 18 18:23 poem_backup
-rw-r--r-- 1 adam.adebisi users   246 Oct  1 15:01 response
-rw-r--r-- 1 adam.adebisi users  2098 Sep 18 18:19 rootdir
-rw-r--r-- 1 adam.adebisi users   251 Oct  7 11:37 script
-rw-r--r-- 1 adam.adebisi users   124 Oct  1 14:24 shifter
-rw-r--r-- 1 adam.adebisi users     1 Oct  2 13:21 sports
-rw-r--r-- 1 adam.adebisi users   182 Oct  1 14:22 squares
-rw-r--r-- 1 adam.adebisi users   214 Oct  1 14:31 sumints
-rw-r--r-- 1 adam.adebisi users  2776 Oct  5 11:53 system
-rw-r--r-- 1 adam.adebisi users  2712 Oct  4 13:04 tempfile
-rw-r--r-- 1 adam.adebisi users     0 Oct  3 13:20 testme
-rw-r--r-- 1 adam.adebisi users   498 Oct  1 14:16 travel_time
-rw-r--r-- 1 adam.adebisi users 57344 Oct  3 19:24 typescript
-rw-r--r-- 1 adam.adebisi users   905 Oct  3 18:50 user_list
-rw-r--r-- 1 adam.adebisi users    54 Oct  6 19:58 username
-rwxr-xr-x 1 adam.adebisi users   414 Oct  6 14:37 userprocesses
-rw-r--r-- 1 adam.adebisi users    92 Oct  1 14:19 users
-rw-r--r-- 1 adam.adebisi users  6930 Oct  6 12:37 xx

Can u help you with this?
0
Comment
Question by:FirstMan
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
ghostdog74 earned 1500 total points
ID: 20030353
a good start.
If you have GNU find, you can use the -printf option to print out the permissions in octal
eg
find /path -name "*txt" -printf "%#m:filename:%f"
0
 

Author Comment

by:FirstMan
ID: 20030439
Hi,

Thanks for your reply. I'm kinda new to UNIX, so what is a GNU and do i need to add it to the script??
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20031716
you can see the version of your find command by typing

# find --version.

or do a man find, then see if you have that option. Else, just execute the find command i posted to see if there are errors saying -printf option is not available.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question