- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: script to check file owner permission
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2011 01:36 AM
01-26-2011 01:36 AM
script to check file owner permission
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2011 01:39 AM
01-26-2011 01:39 AM
Re: script to check file owner permission
chmod u+w dest/foo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2011 01:52 AM
01-26-2011 01:52 AM
Re: script to check file owner permission
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2011 06:29 AM
01-26-2011 06:29 AM
Re: script to check file owner permission
> 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)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011 07:24 PM
02-08-2011 07:24 PM
Re: script to check file owner permission
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011 08:43 PM
02-08-2011 08:43 PM
Re: script to check file owner permission
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011 10:25 PM
02-08-2011 10:25 PM
Re: script to check file owner permission
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011 11:10 PM
02-08-2011 11:10 PM
Re: script to check file owner permission
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 12:38 AM
02-09-2011 12:38 AM
Re: script to check file owner permission
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 12:58 AM
02-09-2011 12:58 AM
Re: script to check file owner permission
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 01:56 AM
02-09-2011 01:56 AM
Re: script to check file owner permission
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 06:12 AM
02-09-2011 06:12 AM
Re: script to check file owner permission
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 06:35 AM
02-09-2011 06:35 AM
Re: script to check file owner permission
> 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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 07:48 AM
02-09-2011 07:48 AM
Re: script to check file owner permission
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 09:52 AM
02-09-2011 09:52 AM
Re: script to check file owner 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 12:39 PM
02-09-2011 12:39 PM
Re: script to check file owner permission
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2011 01:18 PM
02-09-2011 01:18 PM
Re: script to check file owner permission
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...