script help for permissions

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?
FirstManAsked:
Who is Participating?
 
ghostdog74Connect With a Mentor Commented:
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
 
FirstManAuthor Commented:
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
 
ghostdog74Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.