Any improvement possible in this script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Any improvement possible in this script
# 1  
Old 03-06-2009
Bug Any improvement possible in this script

Hi!
Thank you for the help yesterday

This is the finished product
There is one more thing I would like to do to it but I’m not to certain
On how to proceed I would like to log all output to a log in order to
Be able to roll back

This script is meant to be used in repairing a disaster that was done
Some time ago to a /DIR structure that someone did chmod –R 777 /DIR
There are 39 different structure in this setup that is why the step by step
Aproche

And BTW I’m not a very good script writer so any hints
Is very welcome and appreciated but works fine has it is
This is the script
Code:
###############################################################################
#
# Script de test pour les ww
# 05/02/2008
###############################################################################
usage () {

NAME= echo "chgfile.sh"
VERSION= echo "Version-01"
PURPOSE= echo"Correction des permission usager et group, projet ww"

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
     -p, permission
     -d, path
     -u, user name
     -g, group name
     -n, file-name-type
     -v, verbose
     -h, usage and options (this help)
     -l, see this script"
    exit 1
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME  -h for help; exit 1; }

# var init
DIR=
MOD=
USR=
GID=
NAM=
# option and argument handling
while getopts vhlp:n:d:u:g: options; do

    case "$options" in
        p) MOD="$OPTARG" ;;
        d) DIR="$OPTARG" ;;
        u) USR="$OPTARG" ;;
        g) GID="$OPTARG" ;;
        n) NAM="$OPTARG" ;;
        v) verbose=on ;;
        h) usage ;;
        l) more $0; exit 1 ;;
       \?) echo invalid argument, type -h for help; exit 1 ;;
    esac

done
# Main execusion

# Setup find correctly.
export IFS=$'\n'

# Loop through the array.

for x in ${DIR[@]}
        do
        # Find all files & subdirectories
        for i in $(find $x -type d)
                do
                # Fix Permissions
                chmod -c $MOD $i
                chown -c $USR:$GID $i
                done

                # Find all Files
                for i in $(find $x -type f)
                do
                # Fix Permissions
                chmod -c $MOD $i
                chown -c $USR:$GID $i                

                done
        done
 ------------------------------------------------

THK
Dan
# 2  
Old 03-06-2009
Look like complicated, what about a simple loop ?
Code:
find ${DIR} | while read i
do
   chmod -c $MOD $i
   chown -c $USR:$GID $i 
done

# 3  
Old 03-07-2009
Improving my script

Hi! Danmero

I tried that bit of code you posted and I get
The same result. It more élégant
And compact. But the thing I'm really after
Is logging the out-put to file for a possible
Roll-back

Thanks again for the input
# 4  
Old 03-08-2009
man stat , save the original file/dir information on log.file
Code:
find ${DIR} | while read i
do
   stat $i >> log.file
   chmod -c $MOD $i
   chown -c $USR:$GID $i 
done

# 5  
Old 03-09-2009
Thank You to all

Smilie Thank You to all

The script Works fine and. And this thread can be closed
Best regards
Dan
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Performance improvement in grep

Below script is used to search numeric data from around 400 files in a folder. I have 300 such folders. Need help in performance improvement in the script. Below Script searches 20 such folders ( 300 files in each folder) simultaneously. This increases cpu utilization upto 90% What changes... (3 Replies)
Discussion started by: vegasluxor
3 Replies

2. UNIX for Dummies Questions & Answers

Improvement in shell script

Hi This is my Following code: #!/bin/sh echo "TOTAL_NO_OF_MAILS" read TOTAL_NO_OF_MAILS echo "TOTAL_NO_OF_TICKETS " read TOTAL_NO_OF_TICKETS echo "TICKETS_IN_QUEUE" read TICKETS_IN_QUEUE rm -rf `pwd`/Focus echo "Hi Team\nSTATS IN CLRS MAIL BOX\n\n==============================" >> Focus... (11 Replies)
Discussion started by: wasim999
11 Replies

3. Shell Programming and Scripting

I need the improvement for my script

Hi All, Here is my script #! /bin/sh var1=some email id var2=some email id grep -i "FAILED FILE FORMAT VALIDATION" /opt >tmp2 diff tmp1 tmp2 | grep ">" >tmp3 if then cat tmp3 | mailx -s " Error Monitoring" $var2 else echo "Pattern NOt Found" | mailx -s " Error Monitoring" $var1... (1 Reply)
Discussion started by: Gopalak
1 Replies

4. UNIX for Advanced & Expert Users

linux os improvement

can anyone help to share the knowledge on linux os improvement? 1) os account - use window AD authentication, such as ldap, but how to set /etc/passwd, where to put user home? 2) user account activity - how to log os user activity share the idea and what tools can do that...thx (5 Replies)
Discussion started by: goodbid
5 Replies

5. Shell Programming and Scripting

Looking for improvement to script that compresses a directory that is 2 months old

I write reports daily to a directory that is named with the month and year. I wanted to compress all but the last two months worth of these directories to save space. I am going to cron a job to run on the first of each month to do this clean up. I didn't have zip/unzip on my AIX environment so... (0 Replies)
Discussion started by: slatoms
0 Replies

6. Shell Programming and Scripting

Script ready but might need some improvement.

Hi All, I have written a script which does some editing in the files, based on user input.This might not be the most elegant way of doing it and there would be many improvements needed. Please go through it and let me know how it could be improved. Suggestions are welcome!! Thanks!... (2 Replies)
Discussion started by: nua7
2 Replies

7. Programming

File - reading - Performance improvement

Hi All I am reading a huge file of size 2GB atleast. I am reading each line and cutting certain columns and writing it to another file. Here is the logic. int main() { string u_line; string Char_List; string u_file; int line_pos; string temp_form_u_file; ... (10 Replies)
Discussion started by: dhanamurthy
10 Replies
Login or Register to Ask a Question