shell script to check permission before copy over


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to check permission before copy over
# 1  
Old 04-22-2011
shell script to check permission before copy over

Hi,

I am writing some shell script to check the file owner permission whether the write bit is turn on. If it is turn on it will copy the file to the destination else it will just prompt user to change the file and skipping it.

However, I am starting getting loss here as I have couple of issues that I am facing:
1. first the file could be using regular expression in the file for example file* or file?? which is usually handling pretty well if you are using cp command to handle but in script I will have to warn user to put quote.
2. My current script also have limitation that it cannot directory.

Is there any easier way of how the script to structure. For example, let say the user can just like what they did in typical cp command just run
Code:
script -s source -t target

and the script will check through the file owner permission and if it is directory it will check those file permission inside (we do not have to check directory permission).

The more I write my current script the more I find its limitation.

I always thought there should an easier way to just pass the script -s source -t target to cp -pr source target in an easier way.
Code:
#!/bin/bash

#version 1.0
#Revised Date: 10-Feb-2011

#Set this as your target filesystem
TGT="/tmp/dir2"

#Function call to check file owner write bit
#set -x
func_checkperm()
{
        BIT=`ls -l $f | sed -e 's/..\(.\).*/\1/'`
        case $BIT in
                -)
                echo "NO_WRITE"
                ;;
                w)
                echo "WRITE"
                ;;
        esac
}

if [ $# -eq 1 ]
then
        SRC="$1"

        if [ -d "$SRC" ]; then
        echo "This is a directory not a file, please specify a file"

        else

                for f in $SRC
                do
                CON_VALUE=$(func_checkperm "$f")
                case $CON_VALUE in
                        NO_WRITE)
                                echo "$f write permission bit is not set.  Please correct the permisson.  Skipping.."
                        ;;
                        WRITE)
                                cp -pr $f $TGT
                        ;;
                esac
                done
        fi

elif [ $# -gt 1 ]
then
        echo "Please enclose multiple input files or regular expression in quotes.  For example: <script.sh> \"inputsource*\" target"

elif [ "$1" = "" ]
then
        echo "The command usage is not correct"
fi


Last edited by Scott; 04-23-2011 at 02:24 AM.. Reason: Please use code tags
# 2  
Old 04-23-2011
whats the issue you are facing?

regards,
Ahamed
# 3  
Old 04-25-2011
I would like to make it work for the directory but not sure how to start
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check and copy column entries

Hi I have a text file (INPUT.TXT) of three columns assuming they are separated by tab. The format is given as: Column1 Column2 Column3 aaa NO aaa bbb YES asdf ccc YES yyasdf ddd NO ddd eee ... (3 Replies)
Discussion started by: my_Perl
3 Replies

2. Shell Programming and Scripting

Permission denied running shell script in opensuse

Hello, I am having an odd problem in open suse 12. I have a shell script and when I try to run it, I get "permission denied" The permissions from ls -l are, -rw------- 1 user1 users 25904 Jan 10 16:26 script.sh I have tried to change the permissions in dolphin but this does not change the... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

4. Shell Programming and Scripting

Execute permission for shell script

Hi All, I am not able to figure out what is the problem with calling a shell script within a shell script. i have given all the permissions to both schell scripts. but when i am seeing the log file error is coming like weekly_us_push_rpts_tst.sh: ./vacation_quota_summary_detail.sh: Execute... (9 Replies)
Discussion started by: krupasindhu18
9 Replies

5. Shell Programming and Scripting

Automation, Copy, File Status Check --> Script(s)

All, I have to write a script to do the following requirement. There is a file called BUSINESS_DATE.TXT. This file get updated once the oracle partition created. In Oracle, Partition will be created every day. There is a seperate script scheduled to take care ORACLE partition creation. The... (3 Replies)
Discussion started by: karthi_mrkg
3 Replies

6. Shell Programming and Scripting

perl script to check read/write/execute permission for 'others'

I want to check access rights permissions not for 'user', not for 'group', but for 'others'. I want to do it by system command in which i want to use 'ls -l' and 'awk' command. I have written the following program : #!/usr/bin/local/perl #include <stdlib.h> system ("ls -l | awk... (1 Reply)
Discussion started by: shubhamsachdeva
1 Replies

7. Shell Programming and Scripting

shell script and Permission denied

Have the following in a .sh file. printf "Installing ... \ r" cd $ ORG_DIR / a_a . / configure> error.log Make 1> error.log 2> error.log make install> error.log But when I run I get the following. install.sh: line 270:. / configure: Permission denied make: *** No rule two make target... (3 Replies)
Discussion started by: Mumie
3 Replies

8. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

9. Shell Programming and Scripting

Need a script to copy files and check

Hi all, I need a script in ksh: 1: Copy files from directory (A) to directory (B) 2: Check if files that will be copied in directory (A) have never been yet copied to (B) 3: Never copy the last created file of directory (A) This script will run on crontab. Thanks in advance for your... (1 Reply)
Discussion started by: Camaro
1 Replies

10. UNIX for Dummies Questions & Answers

need shell script that checks your user id and your permission level

I just need the shell script that checks your user id and your permission level. I sort of have one to check the user id but I don't like it. Can anyone help with this? (3 Replies)
Discussion started by: yammer
3 Replies
Login or Register to Ask a Question