Operating System - HP-UX
1826414 Members
4251 Online
109692 Solutions
New Discussion

Re: script to check file owner permission

 
kholikt
Super Advisor

script to check file owner permission

Hi,

I would like to run a script to copy the file to a new location but the owner "w" bit must be enabled before it reach the destination.

For example, if I have file with 444 permission then the script when it copy to the destination it should be 644. I was wondering is there any unix utility that can copy the file to destination and forgo all the original file permission.
abc
16 REPLIES 16
Dennis Handly
Acclaimed Contributor

Re: script to check file owner permission

cp foo dest/foo
chmod u+w dest/foo
Jose Mosquera
Honored Contributor

Re: script to check file owner permission

Hi,

If you wants keep file ownership (user/group) you must execute:
#cp -p myfile /target_dir

In this case you are also copying the original file permision, so you must execute:
#chmod u+w /target_dir/myfile

Rgds.

Steven Schweda
Honored Contributor

Re: script to check file owner permission

> [...] the owner "w" bit must be enabled
> before it reach the destination.

Why "before"? Why wouldn't "after" work as
well? If you _really_ need to set a file's
properties _before_ it reaches its
destination, then you may need to copy it to
some temporary intermediate location, change
its properties there, and then copy it to its
final destination (preserving those
properties). But, until you explain why, I
doubt that you really need to go through all
that.

And what do you want the UID/GID of the copy
to be? And what about the date-time values?


> If you wants keep file ownership
> (user/group) you must execute:

You "must"? Really? So, a "tar" (or "cpio"
or "pax", or ...) pipeline can _not_ do the
job? I think that you may be wrong about
that.

One should be careful when using words like
"must" this way. There is almost never only
one way to solve any computer-related
problem.

> #cp -p [...]

What does "-p" do to solve the problem (as
stated)?
kholikt
Super Advisor

Re: script to check file owner permission

Hi,

This is an application requirements that the script when it start, it will check for the owner write permission bit first before a copy operation take place. If the file owner permission has "write bit enabled" I will go ahead copy the file to destination else I will do nothing on it.

For example, file2 and file3 will passed the test and copy to the destination. file1 and file4 will fail so even though file4 has write permission bit on the group and other.

-r--r--r-- 1 root root 0 Feb 8 16:52 file1
-rw-r--r-- 1 root root 0 Feb 8 16:52 file2
-rwxrwxrwx 1 root root 0 Feb 8 16:52 file3
---xrwxrwx 1 root root 0 Feb 8 16:52 file4

I have write some part of the script in the attachment but I still have issue for the check permission function as it always return the file has write permission. What is the best way to check a file owner permission bit other than [ -w "$file]

One more thing I wish the script will work like a normal cp command so it will accept the following syntax

script.sh file* /dest/
script.sh src_folder /dest/dest_folder
script.sh file1 /dest/file5

However, the first and second always return "command usage is not correct"

How should I go around with this
abc
Steven Schweda
Honored Contributor

Re: script to check file owner permission

Is it time to start a new thread for a new
question?

> [...] it always return the file has write
> permission. [...]

Who's doing the test? For "root", perhaps
every file is "writable".

> [...] What is the best way [...]

I don't know the best way. One possible way
would be to look at the permissions in an
"ls -l" report:

$ ls -l 361861.txt 368513.txt
-r--r--r-- 1 sms sms 3194 Sep 7 22:35 361861.txt
-rw-r--r-- 1 sms sms 725 Feb 8 22:20 368513.txt

$ ls -l 361861.txt | sed -e 's/..\(.\).*/\1/'
-

$ ls -l 368513.txt | sed -e 's/..\(.\).*/\1/'
w


> [...] I wish the script will work like a
> normal cp command [...]

Did you try anything like:

echo ">${SRC}<" ">${TGT}<"
or:
set -v
or:
set -x

so that you could see what your script is
trying to do?

Perhaps something more like:

cp -pr "$@"

would help.
kholikt
Super Advisor

Re: script to check file owner permission

Thanks for the quick advice, the sed is indeed a quick fix for that. I also out find why the argument parts is not working if the users specify multiple file by using file*

It look file the value will return all the file thus doesn't match the number of arguments

[root@ns80gcs0 tmp]# ./script.sh file* /tmp/test
+ '[' 5 -ne 2 ']'
+ src=file1
+ TGT=file2
+ echo 'file1 file2 file3 file4 /tmp/test'
file1 file2 file3 file4 /tmp/test
++ ls file1
+ for file in '`ls $SRC`'
++ fc_checkperm file1
+++ ls -l file1
+++ sed -e 's/..\(.\).*/\1/'
++ BIT=-
++ case $BIT in
++ echo NO_WRITE
+ CON_VALUE=NO_WRITE
+ echo NO_WRITE
NO_WRITE
+ echo file1
file1
+ case $CON_VALUE in
+ echo 'You have no write'
You have no write
abc
kholikt
Super Advisor

Re: script to check file owner permission

my biggest problem is with the following parts

this will work if the user is specify one file. In the scenario of multiple file* this will break as file* will return all the file with similar filename and this will return the multiple value for $1 and break the script.

if [ $# -gt 1 ]
then
src="$1"
echo $SRC
TGT="$2"
abc
kholikt
Super Advisor

Re: script to check file owner permission

It seem like the major problem is with file* since it is going to return file1 file2 file3 and file4 and screw up the script.

I have found a way to deal with this is to use script.sh "file*" /dest

However by doing this I need the user to specify "" and no robust enough. Is there anyway that I can deal with this,
abc
Dennis Handly
Acclaimed Contributor

Re: script to check file owner permission

>In the scenario of multiple file* this will break as file* will return all the file with similar filename

You can tell the user to use quotes:
./script.sh "file*" /tmp/test

Or turn off globbing:
set -f
./script.sh file* /tmp/test
set +f
Jose Mosquera
Honored Contributor

Re: script to check file owner permission

Hi,

Please try with attached script, you just need defines the variable SOURCE_DIR inside.
I hope that does not have syntax errors, I don't have an environment available for test it.

Rgds
Bill Hassell
Honored Contributor

Re: script to check file owner permission

Specifying a regular expression on the command line will never work the way you want. The shell (as you have seen) changes the string into all the matching filenames. Your script is designed to copy one or more files to a single destination, so change the script to:

script.sh

That may seem backwards to some users but it makes your script easier to parse.

A second way is to parse the command line with awk, something like this:

DEST=$(echo "$@" | awk '{print $NF}')
SOURCE=$(echo "$@" | awk '{ for (n=1; n
So now, $DEST is the destination (last item on the line) and $SOURCE is one or more filenames as the source.


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: script to check file owner permission

> Specifying a regular expression on the
> command line will never work the way you
> want. [...]

Never is a long time.

If the goal is to pass all the command-line
arguments to "cp", then I don't immediately
see what's wrong with using
"$@"
. But feel free to enlighten me. Doesn't
"cp" accept multiple "source_file" arguments?


> [...] That may seem backwards [...]

Sure may. So why do it that way?
kholikt
Super Advisor

Re: script to check file owner permission

The reason for not using cp "@$" is because before the file copy take place I need to check the write bit permission.
abc
Dennis Handly
Acclaimed Contributor

Re: script to check file owner permission

>The reason for not using cp "@$" is because before the file copy take place I need to check the write bit permission.

What will you do if one fails, skip one or abandon all of them?

You can copy all of $@ to a shell array but limited to 1023 values.

And then you can loop on each of them and exclude only the bad ones.
Or you could use shift and write them to a file.

As Bill said, swapping the order will make your shell scripting easier. Or you may have to go with perl.
Bill Hassell
Honored Contributor

Re: script to check file owner permission

>> If the goal is to pass all the command-line
arguments to "cp", then I don't immediately
see what's wrong with using
>> "$@"

The script currently wants source and destination in that order. When the regular expression (file*) is used followed by the destination, the script has to extract the expanded list but not include the last item because that's the destination. Then extract the last item in a variable list of items.

True, if you beat the end user of the script into always enclosing RE's with quotes, then there will be only 2 arguments on the command line and by carefully enclosing the multi-filename variable, it could indeed be passed to cp.

Or the script can bail out with helpful messages if the arglist is more than 2 arguments, like "enclose multiple input files in quotes" ...


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: script to check file owner permission

Hi:

Another way to pass your file name arguments before your destination directory argument is to use this perl snippet to move the last argument to the beginning:

# cat .exchange
#!/usr/bin/sh
echo "was: $@"
EXCHANGED=$(echo $@ | perl -nle '@a=split;push @a,shift @a;print "@a"')
echo "is : ${EXCHANGED}"

# ./exchange DST f1 f2 f3
was: DST f1 f2 f3
is : f1 f2 f3 DST

Regards!

...JRF...