Sponsored Content
Top Forums Shell Programming and Scripting shell script to check permission before copy over Post 302516390 by wanaka on Friday 22nd of April 2011 10:01:58 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SCRIPT(1)						    BSD General Commands Manual 						 SCRIPT(1)

NAME
script -- make typescript of terminal session SYNOPSIS
script [-akq] [-t time] [file [command ...]] DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript. If the argument command is given, script will run the specified command with an optional argument vector instead of an interactive shell. The following options are available: -a Append the output to file or typescript, retaining the prior contents. -k Log keys sent to program as well as output. -q Run in quiet mode, omit the start and stop status messages. -t time Specify time interval between flushing script output file. A value of 0 causes script to flush for every character I/O event. The default interval is 30 seconds. The script ends when the forked shell (or command) exits (a control-D to exit the Bourne shell (sh(1)), and exit, logout or control-D (if ignoreeof is not set) for the C-shell, csh(1)). Certain interactive commands, such as vi(1), create garbage in the typescript file. The script utility works best with commands that do not manipulate the screen. The results are meant to emulate a hardcopy terminal, not an addressable one. ENVIRONMENT
The following environment variable is utilized by script: SHELL If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically). SEE ALSO
csh(1) (for the history mechanism). HISTORY
The script command appeared in 3.0BSD. BUGS
The script utility places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects. It is not possible to specify a command without also naming the script file because of argument parsing compatibility issues. When running in -k mode, echo cancelling is far from ideal. The slave terminal mode is checked for ECHO mode to check when to avoid manual echo logging. This does not work when in a raw mode where the program being run is doing manual echo. BSD
January 22, 2004 BSD
All times are GMT -4. The time now is 07:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy