looking for help on script to capture file permission.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting looking for help on script to capture file permission.
# 8  
Old 11-15-2010
One way. Inefficient but works for me. Based originally on a post on unix.com which used "dc" and totaly re-written to ensure that we always present a 4-digit octal number to "chmod" because Oracle makes use of SUID and SGID.

Code:
find /opt/ora10g/oracle -type f -print|while read filename
do
        # 10 character permissions from "ls"
        perms=`ls -lad "${filename}"|awk '{print $1}'`
        #
        # Convert to four 3-digit binary numbers
        binary1=`echo "${perms}"|cut -c4,7,10|tr xstST- 011110`
        binary2=`echo "${perms}"|cut -c2-4|tr rwsxtST- 11111000`
        binary3=`echo "${perms}"|cut -c5-7|tr rwsxtST- 11111000`
        binary4=`echo "${perms}"|cut -c8-10|tr rwsxtST- 11111000`
        #
        # Convert binary to octal
        octal1=`echo "obase=8;ibase=2;${binary1}"|bc`
        octal2=`echo "obase=8;ibase=2;${binary2}"|bc`
        octal3=`echo "obase=8;ibase=2;${binary3}"|bc`
        octal4=`echo "obase=8;ibase=2;${binary4}"|bc`
        #
        # Concatonate each octal digit
        octal="${octal1}${octal2}${octal3}${octal4}"
        # Output the final "chmod" command
        echo "chmod ${octal} \"${filename}\""
done


Last edited by methyl; 11-15-2010 at 01:24 PM.. Reason: lose the sort and other test code
# 9  
Old 11-15-2010
It could be shorten a little bit
like

Code:
...
binary1=`echo "$1"|cut -c4,7,10|tr xstST- 011110`
binary2=`echo "$1"|cut -c2-10|tr rwsxtST- 11111000`
# Convert binary to octal
octal=`echo "obase=8;ibase=2;${binary1}${binary2}"|bc`
echo "$octal"
...

This User Gave Thanks to ctsgnb For This Post:
# 10  
Old 11-15-2010
Interesting ctsgnb. By concatonating the binary before executing "bc" you avoid the issue with leading zeros. I haven't tested it but on paper it looks good.

There will be a more efficient solution but it is important that a solution gives the correct answer.


Btw. anurag.singh was close but does not take into account SUID, SGID or Sticky Bit.

To be fair to the person who posted the version using "tr" and "dc", here is the link:
https://www.unix.com/shell-programmin...sh-script.html
The post was quite close to the solution but did not properly deal with permissions containing the Sticky Bit, had issues with leading zeros in octal, and had a couple of minor bugs. The IDEA of using "tr" twice was brilliant and I commend the poster for this.

Last edited by methyl; 11-15-2010 at 08:48 PM.. Reason: Complete backtrack
# 11  
Old 11-16-2010
as far as i have tested, it gives a correct result :

Code:
conv_perm(){
binary1=`echo "$1"|cut -c4,7,10|tr xstST- 011110`
binary2=`echo "$1"|cut -c2-10|tr rwsxtST- 11111000`
octal=`echo "obase=8;ibase=2;${binary1}${binary2}"|bc`
echo "$octal"
}

$ ls -l dbs* | awk '{print$1,$9}'
-rwxr-xr-x dbshut
-rwsr-s--- dbsnmp
-rwxr-xr-x dbsnmp0
-rwxr-xr-x dbsnmpj
-rw-r--r-- dbsnmpj0
-rwsr-s--- dbsnmp.sav
-rwxr-xr-x dbsnmpwd
-rwxr-xr-x dbstart
$ ls -l dbs* | while read a b b b b b b b b
> do
> printf "%4d %s\n" $(conv_perm $a) $b
> done
 755 dbshut
6750 dbsnmp
 755 dbsnmp0
 755 dbsnmpj
 644 dbsnmpj0
6750 dbsnmp.sav
 755 dbsnmpwd
 755 dbstart
$

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on script to capture info on log file for a particular time frame

Hi I have a system running uname -a Linux cmovel-db01 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux I would like to capture the contents of /var/log/syslog from 11:00AM to 11:30AM and sent to this info via email. I was thinking in set a cron entry at that... (2 Replies)
Discussion started by: fretagi
2 Replies

2. Shell Programming and Scripting

Script to capture string in a log file

Dear all, I have a log file to be analysed. this log file contains vaiours lines of code starting with date timestamp. if my search string is exception then that resepective log statement starting from the date is required. example: 2014/10/01 16:14:44.459|>=|E|X|19202496|2832|... (5 Replies)
Discussion started by: shravee
5 Replies

3. Shell Programming and Scripting

set -x within script and capture as a file

Okay, I've been working on a script for providing information on the progress of a backgrounded ditto command. Thanks to google and a lot of searching I've resolved all but one very odd error. At this point, I want to use xtrace (set -x) to try to uncover the issue. I have found several examples... (2 Replies)
Discussion started by: reid
2 Replies

4. Shell Programming and Scripting

AWK Script to Capture Each Line of File As Variable

Hi All, I'm working on creating a parts database. I currently have access to a manufacturer database in HTML and am working on moving all of the data into a MySQL db. I have created a sed script that strips out the HTML and unnecessary info and separates the script into one line for each field.... (3 Replies)
Discussion started by: dkr
3 Replies

5. Shell Programming and Scripting

shl script capture a file

need to be able to capture a file with the following conditions: The filenames are, for example, 3526_332840.dat, where 3526 is constant, and 332840 is a sequential number which is always a couple hundred greater than the previous day's file. I want to be able to change this script to acoomplish... (1 Reply)
Discussion started by: rechever
1 Replies

6. Shell Programming and Scripting

script to capture content of deleted file

I need to capture the content of a file before its being deleted. This file gets deleted immediately after it is created. I use the below shell command in the command prompt, but I'm not getting the desired result. bash-3.00# while true; do cat file* > tempfile; done; What I'm trying here... (5 Replies)
Discussion started by: bjawasa
5 Replies

7. Shell Programming and Scripting

Capture Shell Script Output To A File

Hi, I am running a shell script called dbProcess.sh which performs shutdown and startup of various Oracle instances we have.At the time of execution the script produces the following output to the command line window $./dbProcess.sh stop #### Run Details ###### Hostname : server-hop-1... (4 Replies)
Discussion started by: rajan_san
4 Replies

8. Shell Programming and Scripting

Script to capture new lines in a file and copy it to new file

Hi All, I have a file that gives me new line/output every 5 minutes. I need to create a script that capture new line/output besides "IN CRON_STATUS", in this case the new output is "begin ------ cron_status.sh - -----------". I want this script to capture the line starting from "begin ------... (0 Replies)
Discussion started by: fara_aris
0 Replies

9. Shell Programming and Scripting

file permission script

Hi all, I need one script, that will give the out put like all files having 777 permissions and full path from home directory. example:i created 777 permissions files three in my home directory and subdirectories also. i want out put like ./xxxxx/aaa.txt ./xxxxx/zzz/yyy.txt ... (3 Replies)
Discussion started by: krishna176
3 Replies
Login or Register to Ask a Question