Convert UNIX rights in a number in a sh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert UNIX rights in a number in a sh script
# 8  
Old 04-13-2015
did you try my code (awk) ? or

perl code from specified url.
Code:
perl -le'printf "%o", 07777 & (stat)[2] for @ARGV' <file>

# 9  
Old 04-13-2015
Try also
Code:
ls -l file | awk '{split ($1, X, ""); for (i=2; i<=10; i++) PERM+=2^(10-i)*(X[i]=="-"?0:1) ; printf "%o\n", PERM}'
644

# 10  
Old 04-13-2015
So you can get the textual value from ls -l so could you do the following:-
  • Trim off the first character and dispose (object type flag, e.g. pipe, directory, link, file etc)
  • For each of the nine remaining characters, look at the character and see if it is a hyphen -. Replace it with a zero if it is or a one if it is not.
  • You have a nine digit binary number so take each set of three digits and convert the number from binary to octal (or even decimal as it happens)
Would that logic help?

Is this what RudiC has suggested? I can't quite decipher it.



Kind regards,
Robin
# 11  
Old 04-13-2015
Yes. See here:
Code:
ls -l file | 
awk     '{split ($1, X, "")             # split the mode in $1 into single chars
          for (i=2; i<=10; i++)         # run across 9 chars starting from 2
                 PERM+=2^(10-i)*        # sum binary weight of position times
                        (X[i]=="-"?0:1) # 0 if char is a dash else 1
          printf "%o\n", PERM}'         # print octal value

This User Gave Thanks to RudiC For This Post:
# 12  
Old 04-13-2015
You could try something like this is bash/ksh

Code:
mode="-rwxr-x---"
v=${mode#?}
v=${v//-/0}
v=${v//[rwx]/1}
printf "%03o\n" $((2#$v))

Output:
Code:
750

Have you considered special file permissions (sticky bit, suid and sgid)? Perhaps you will need somthing like this:

Code:
#!/bin/sh
mode="-rwsr-x--x"
spec=$(echo $mode | sed 's/^...\(.\)..\(.\)..\(.\)/\1\2\3/')
spec=${spec//[x-]/0}
spec=${spec//[sStT]/1}
v=${mode#?}
v=${v//[-ST]/0}
v=${v//[rwxst]/1}
if [ $spec -gt 0 ]
then
   printf "%o%03o\n" $((2#$spec)) $((2#$v))
else
    printf "%03o\n" $((2#$v))
fi

output:
Code:
4751


Last edited by Chubler_XL; 04-13-2015 at 05:51 PM..
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert ascii character values to number that comes between the numbers in UNIX

I have variable that contains multiple values of number and also include overpunch(i.e. # $ % etc) character so we want to replace it with numbers. here are the example: Code: 11500#.0# 28575$.5$ 527#.7# 42".2" 2794 .4 2279!.9! 1067&.7& 926#.6# 2279!.9! 885".5" 11714$.4$ 27361'.1'... (1 Reply)
Discussion started by: nadeemrafikhan
1 Replies

2. Shell Programming and Scripting

Need a script for automation the convert a lot number audio files to another format

I have a lot number audio files in the MP3 proprietary format, I want to convert them to 'opus' the free and higher quality format, with keep metadata also. My selection command-line programs are SoX (Sound eXchange) for convert MP3 files to 'AIFF' format in order to keep quality and metadata*... (1 Reply)
Discussion started by: temp-usr
1 Replies

3. UNIX for Dummies Questions & Answers

Question about UNIX file rights

Hello experts, i am a Unix beginner and to test the rules of file rights (rwx) i created the file /root/testdir/subdir/test.sh and set the rights of testdir,subdir, test.sh (using chmod) to various configurations in order to get an idea in which case you need which rights. Test commands... (5 Replies)
Discussion started by: mike_bn
5 Replies

4. Shell Programming and Scripting

Convert unix script in to php

Hi All, I have one shell script and like to convert into GUI for user creation..Any way to convert shell script into php? Please find the below for my script. $ more user.sh #!/bin/ksh set -x pwfile="/etc/passwd" shadowfile="/etc/shadow" gfile="/etc/group" ######Want to check... (0 Replies)
Discussion started by: susindram
0 Replies

5. Shell Programming and Scripting

need script to convert number in hexadecimal

hi , i need a script to convert number into hexadecimal base for example: 237=>ED it s very important for me thank you in advance for you help (5 Replies)
Discussion started by: mips
5 Replies

6. Shell Programming and Scripting

convert unix script to perl

Hi, I have these lines in a unix script: FILEONE = /<filepath1>/<filename1.txt> FILENEW = /<filepath2>/<filename2.txt> head -5 $FILEONE | sed '1d' > $FILENEW PARAM1 = `cat $FILENEW | awk '{print $2;}' ` echo "Param1 Value: $PARAM1" What's the correct syntax of the above lines if same... (2 Replies)
Discussion started by: gholdbhurg
2 Replies

7. UNIX for Dummies Questions & Answers

Unix access rights

Hi, Is it true that if I am not the root I can not select access permissions to a file that I own so that my friend (who also isn't the root) can access that file? And is it true that the only way to accomplish it is to ask the root to "put" my friend into "my" group? Then I could simply set... (1 Reply)
Discussion started by: rudo
1 Replies

8. Shell Programming and Scripting

how to convert unix .ksh script to windows .batch script

I am using awk in my .ksh script but when I am trying to run in windows its not recognising awk part of the ksh script , even when I changed it to gawk it does not work, this is how my .ksh and .bat files look like. thanx. #!/bin/ksh egrep -v "Rpt 038|PM$|Parameters:|Begin |Date: |End... (1 Reply)
Discussion started by: 2.5lt V8
1 Replies

9. SuSE

UNIX - Linux NFS Rights need Help

Hello, I am running a Suse Linux server and I want to set up a NFS Server for a few Unix machines. For the root account at the unix client it works fine but it doesn't work for other users who have no root rights. I've used the no_root_squash and the rw option in the etc/export file. My folder... (7 Replies)
Discussion started by: Ald-Edv
7 Replies

10. Shell Programming and Scripting

C Shell Script to convert a number into minutes

Could anyone tell me how to write a C shell script according to the following requirement. Write a C shell script convertmin which will read in a number, thought of as representing minutes, and print out the number of hours/minutes it represents so: Note: you are required to check exception... (1 Reply)
Discussion started by: Ringo
1 Replies
Login or Register to Ask a Question