Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieving file permissions 1

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
I have to design a file transfer system. The file transfers must run under a specific, non root, user and the files which need to be sent may not be owned by that user. What I want to do is something like
Code:
FPERM=$(getperms input_file)
chmod 666 input_file
su - txuser -c "sendfile input_file"
chmod $FPERM input_file
but I'm having problems writing 'getperms'. I've been looking at something like
Code:
getperms ()
  {
   pstring=$(ls -l $1|awk '{print $1}')
   PT1=0
   PT2=0
   PT3=0
   [[ (echo $pstring|cut -c2) = '-' ]] || (( PT1 += 4 ))
   [[ (echo $pstring|cut -c3) = '-' ]] || (( PT1 += 2 ))
   [[ (echo $pstring|cut -c4) = '-' ]] || (( PT1 += 1 ))
   [[ (echo $pstring|cut -c5) = '-' ]] || (( PT2 += 4 ))
   [[ (echo $pstring|cut -c6) = '-' ]] || (( PT2 += 2 ))
   [[ (echo $pstring|cut -c7) = '-' ]] || (( PT2 += 1 ))
   [[ (echo $pstring|cut -c8) = '-' ]] || (( PT3 += 4 ))
   [[ (echo $pstring|cut -c9) = '-' ]] || (( PT3 += 2 ))
   [[ (echo $pstring|cut -c10) = '-' ]] || (( PT3 += 1 ))
   echo $PT1$PT2$PT3
  }
but there must be a better way. Any ideas?

Ceci n'est pas une signature
Columb Healy
 
Thanks feherke but I'm on AIX, and management are a bit fussy about installing FOSS packages.

Ceci n'est pas une signature
Columb Healy
 
Did you consider to create a tar file containing one or more files?
Transfer the tar file, untar on the target system,
and permissions should the same...
 
Hi

Just played with the idea. Not a nice solution but maybe gives you a better idea.
Code:
[blue]master #[/blue] ls -l lasagna.txt                                
-rw-r--r--    1 master    users         793 2005-04-26 11:49 lasagna.txt
[blue]master #[/blue] perm=`ls -l lasagna.txt | cut -c 2-10 | sed 'y/rwx-/1110/;s/\(.\)\(.\)\(.\)/$((\1*4+\2*2+\3))/g;s/^/echo /' | sh`
[blue]master #[/blue] echo $perm
644
Tested with GNU [tt]sed[/tt], [tt]bash[/tt] and [tt]ksh[/tt].

Feherke.
 
Wow!

feherke, that's opened up a whole new range of ideas. Thanks. I'm not sure it's going to be easy to maintain!!

Ceci n'est pas une signature
Columb Healy
 
Hi

columb said:
I'm not sure it's going to be easy to maintain!!
Easy as maintaining your solution. Transformed in [tt]function[/tt] is no much difference. Just less piping, so may be faster.
Code:
getpermsed()
{
  ls -l "$1" | \
  cut -c 2-10 | \
  sed 'y/rwx-/1110/;s/\(.\)\(.\)\(.\)/$((\1*4+\2*2+\3))/g;s/^/echo /' | \
  sh
}
But hoinz's suggestion with [tt]tar[/tt] is much more promising, while Unix [tt]sed[/tt] seems unpredictable for me. Then maybe [tt]awk[/tt], that is much portable :
Code:
getoptawk()
{
  ls -l "$1" | \
  awk '{delete n;for(i=0;i<3;i++)for(j=0;j<3;j++)n[i]+=(substr($1,10-(i*3+j),1)!="-")*2^j;print n[2]n[1]n[0]}'
}

Feherke.
 
Thanks for all this. Not being the world's greatest sed expert I find long sed expressions daunting when I don't know what they're supposed to do so when I come back to this code in six months time... (I suppose a few comments in my code might help!)

My preferred answer is the awk solution. I can't use the tar answer because the problem is all about 'txuser' being the only user authorised to run the Connect Direct software we use for file transfers, and how to allow this usr the ability to read files that it hasn't got permissions to.

The simple answer is to copy the files to a holding area, chmod them 666, send them, and then delete them, but I'd prefer not to have to copy lots of potentially large files around.

Once again, thanks for your help

Ceci n'est pas une signature
Columb Healy
 
Hi,

let me comment on this:
I can't use the tar answer because the problem is all about 'txuser' being the only user authorised to run the Connect Direct software we use for file transfers, and how to allow this usr the ability to read files that it hasn't got permissions to.

'txuser' for sure is not the only user with the ability to run tar command;
what about some other user for creating a tar file, then chmod 666 tarfile, then let txuser transfer, ...

hope this helps
 
hoinz

Duh! Sorry, I didn't understand your answer at first. Of course, now I've grasped it, it's simple and easy.

Thanks

Ceci n'est pas une signature
Columb Healy
 
Hi

My I have a question, just for curiosity ? How usual is to have [tt]perl[/tt] installed on Unix systems ? ( Yeah, know, is free, open source... )
Code:
echo /file/name | perl -ne 'chomp;print substr sprintf("%6o",(stat)[2]),3'

Feherke.
 
feherke

Yes, I do have perl, and that's a very elegant answer. Have a star.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top