testing file permissions.....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting testing file permissions.....
# 1  
Old 01-22-2010
testing file permissions.....

script name: filetest.sh
Code:
if [ $# -eq 0 ] ; then
echo " You didn't enter any argument"
elif [ ! -e $1 ] ; then
echo " file not exist"
elif [ ! -r $1 ] ; then
echo " file not readable"
elif [ ! -w $1 ] ; then
echo " file not writable"
else
echo " file both readable and writable"
fi

running like...

$ ./filetest filename

but it showing.....both readable and writable

for those file which are not readable and writable

Last edited by Scott; 01-22-2010 at 05:23 AM.. Reason: Please use code tags
# 2  
Old 01-22-2010
This script should work. I also did use the code & verify. Can you check file permissions & file ownership details?
# 3  
Old 01-22-2010
Agreed the script works in a Posix shell as a non-root user.
It gives misleading answers if you are user "root.
# 4  
Old 01-22-2010
Using the elif construct; the script would only inform you the file was not readable even if it was not readable and not writeable. These conditions would probably be better expressed as individual if statements so you get a complete report. You could also add a check for executable as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File and directory testing

original post -- I have a korn shell script that does some things that depend on creating and writing a file in a directory. I'm looking for a more elegant/efficient way to do the check than what I'm using now: if ] then print "Creating ${STGDIR}/${SHOW}" mkdir... (3 Replies)
Discussion started by: Dalej
3 Replies

2. Shell Programming and Scripting

Changing file permissions of a file created by another user

Hi, I have used expdp for datapump. The .dmp file is created by the "oracle" user. my requirement is to make a zipped file of this .dmp file. What i am trying to do is change the permissions of this .dmp file from 0640 to 0644 and then do a gzip and zip it. Is there any way i can change... (3 Replies)
Discussion started by: qwertyu
3 Replies

3. Shell Programming and Scripting

Testing for one word in a file

I am trying to test the output of a file. What I have so far is this: if ]; then echo "yes";fi The problem with this is it works but I only want it to print out if the file contains the word "Compacted." The == sign means identical or equal to so it won't work. I tried ~ but that doesn't... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. Shell Programming and Scripting

ksh; Change file permissions, update file, change permissions back?

Hi, I am creating a ksh script to search for a string of text inside files within a directory tree. Some of these file are going to be read/execute only. I know to use chmod to change the permissions of the file, but I want to preserve the original permissions after writing to the file. How can I... (3 Replies)
Discussion started by: right_coaster
3 Replies

5. Shell Programming and Scripting

testing if a file is a directory

i have written this simple script called isdir.sh #! /bin/bash dir=$1 _ls=`ls $dir` for file in $_ls do if then echo "D $file" fi donethe output is not right. for example $ ./isdir.sh src ***no output*** but i have in ~/src some directories drwxr-xr-x 2... (5 Replies)
Discussion started by: and77
5 Replies

6. Shell Programming and Scripting

Retain file permissions when saving .sh file from internet [OS X]

Hello. I have written a bash script that I am sharing with an OS X community I am a member of. The purpose of the script is to execute a series of commands for members without them having to get involved with Terminal, as it can be daunting for those with no experience of it at all. I have renamed... (4 Replies)
Discussion started by: baza210
4 Replies

7. Shell Programming and Scripting

Testing for empty file

Hello, I need to determine if a file I have is empty or not. How can I go about doing this in shell scripting? Some sample code would be appreciated? Thanks, (6 Replies)
Discussion started by: mojoman
6 Replies

8. HP-UX

To give the "unzip" permissions & "create" file permissions

Hi, I am a Unix Admin. I have to give the permissions to a user for creating new file in a directory in HP-Ux 11.11 system since he cannot able to create a new file in the directory. Thanks in advance. Mike (3 Replies)
Discussion started by: Mike1234
3 Replies

9. UNIX for Dummies Questions & Answers

Testing existence of a file /directory

hey guys How can i test existence of a file /directory in a directory in a script thanks (2 Replies)
Discussion started by: ajaya
2 Replies

10. UNIX for Dummies Questions & Answers

testing for file size in script

Has anyone got a few tips on how I can test if the file size is 0? I am moving files on a regular basis from one location to another with ftp. The files which are 0 bytes in size we want to discard. Thankyou in advance. (3 Replies)
Discussion started by: Ivo
3 Replies
Login or Register to Ask a Question