The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to gather weblogic jvm heap size stats galenw Shell Programming and Scripting 2 02-07-2008 01:34 PM
How to perform addition of two numbers in shell scripting in Solaris-10 krevathi1912 SUN Solaris 9 11-29-2007 09:36 AM
Unix shell scripting to find latest file having timestamp embedded... kaushik25 AIX 2 08-06-2007 10:42 PM
Scripting file permission problems... Heron Shell Programming and Scripting 3 03-24-2005 10:34 AM
unix to win2k permission dozy Windows & DOS: Issues & Discussions 0 04-21-2004 04:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-22-2008
ajilesh ajilesh is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 16
Gather File permission during scripting on unix as numbers.

Hi,

I have a script with following file permission on box.
-rwxr-xr-x 1 root system 15347 Aug 14 15:08 b_reboot.ksh

Without calculating or watching at -rwxr-xr-x (permission's) of this above mentioned file. I would like to get the file permission assigned to a file.

Basically looking for after issuing some commands <file name>, I should get the permission of provided file in a variable as 755. (as a number, either 755/644..or what ever it is.)

Example,

$some_command <file_name>
$permission=$?

And the output which I looking forward is like this.
$print ${permission}
755

Do we have any command which can substitute "some_command" like this on Unix.?

Note:
------
I don't like to go for the manipulation using the ls -l |grep <filename> |awk '{print $1}'
Looking for any thing which can be easily done to retrieve permission of file in numbers.
  #2 (permalink)  
Old 08-22-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
do you mean set file permissions to 755?
[code]chmod 755 filename[/url]

If you mean get the permissions, and you are in Linux check out the stat command. That would be
Code:
man 1 stat 
#or
stat --help
  #3 (permalink)  
Old 08-22-2008
james.witte james.witte is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
from what I read, ajilesh is wanting to see what the permissions are in a non-volatile way. I don't know how you do this in csh, but in ksh you can do this:

if [[ -x /usr/lib/sendmail ]] this is true if the user doing the condition, can execute /usr/lib/sendmail
if [[ -r /usr/lib/sendmail ]] this is true if the user doing the condition, can read /usr/lib/sendmail
if [[ -w /usr/lib/sendmail ]] this is true if the user doing the condition, can write /usr/lib/sendmail

but this only checks the permissions of the user performing the, in this case, if/then/else has those permissions. I do not know of any way to convert rwxrw-rw- to the octal 755 to then confirm you have the correct permissions on said file or directory. I have run into this same problem in my own scripting because the file I was checking on needed to be 766 and I was trying to track down when the permissions got changed so I could figure out what was changing the permissions, the only option I found was to ls -l <file> then pipe that into a grep on "rwxrw\-rw\-" to confirm (yes, \- is necessary to ensure it doesn't try to treat -rw as an option some how) it had the correct permissions.

while using chmod is a good suggestion, chmod sets the permissions without regard to the previous state.
  #4 (permalink)  
Old 08-22-2008
ajilesh ajilesh is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 16
James, well, you are correct. I'm looking for any way to convert rwxrw-rw- to the octal 755.. Do we have any internal unix commands where we can trace the octal which is assigned to a file or directory directly.
  #5 (permalink)  
Old 08-22-2008
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
find . -name "*" -maxdepth 1 -printf '%m %p\n'
or
stat -c "%a %n" *
  #6 (permalink)  
Old 08-22-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
Quote:
Originally Posted by sudhamacs View Post
find . -name "*" -maxdepth 1 -printf '%m %p\n'
or
stat -c "%a %n" *
both are not working in AIX..
  #7 (permalink)  
Old 08-22-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If you have Perl, you can use that as a workaround.

Code:
perl -e 'for (@ARGV) { printf "%04o %s",  (stat)[2] & 07777, $_ }' files ...
Sponsored Links
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0