Sponsored Content
Top Forums Shell Programming and Scripting Shell script code not really understandable for me Post 302995390 by digioleg54 on Wednesday 5th of April 2017 09:16:31 AM
Old 04-05-2017
Shell script code not really understandable for me

I have a script:

Code:
 DAYS=10
 print_usage() {
        echo "\n"
        echo "Usage: $PROG [-days] [-file|-dir] dir1 [ dir2 ... dirN]"
        echo "       This program will delete all file/directories that are beyond "
        echo "       'x days' old."
        echo "           -days      - This flag allows you to define x days [default:10]"
        echo "           -file|-dir - This flag allows one to remove files [default: directories]"
        echo "\n"
}
  
  
         while [ ! -z "$1" ]
        do
                case $1 in
                        -file* | -f*)
                                TYPE="f"
                                ;;
                        -link* | -l*)
                                TYPE="l"
                                ;;
                        -dir* | -d*)
                                TYPE="d"
                                ;;
                        -1* | -2* | -3* | -4* | -5* | -6* | -7* | -8* | -9* | -0* )
                                DAYS=`echo $1 | awk -F- '{printf("%d",$2)}'`
                                ;;
                        -* )
                                print_usage
                                exit -3
                                ;;
                        * )
                                if [ -d $1 ]
                                then
                                        if [ "$TYPE" = "f" or "$TYPE" = "l" ]
                                        then
                                                echo "Searching $1 for files that are $DAYS days old"
                                                find $1 -type $TYPE -mtime +$DAYS -print -exec /usr/bin/rm -f {} \;
                                        else
                                                echo "Searching $1 for directories that are $DAYS days old"
                                                TMP_LOG=$LOG_HOME/delete.$$
                                                find $1/* -type $TYPE -mtime +$DAYS -print > ${TMP_LOG}
                                                cat $TMP_LOG
exit;
                                                for rm_file in `cat $TMP_LOG | grep -v 'log$'`
                                                do
                                                        if [ -d ${rm_file} ]
                                                        then
                                                                /usr/bin/rm -rf ${rm_file}
                                                        else
                                                                echo "Skipping ${rm_file} because, either root dir is removed or it is not a directories"
                                                        fi
                                                done
                                                /usr/bin/rm -f $TMP_LOG
                                        fi
                                 else
                                        echo "Following is NOT a DIRECTORY: $1"
                                fi
                 esac

We call this script from another like this:

Code:
 /export/applications/dte/sh/clean_disk.sh -60        /export/applications/dte/web/release 
  
 /export/applications/dte/sh/clean_disk.sh -14 -file  ${DTE_WDATA_DIR}/log
  
  /export/applications/dte/sh/clean_disk.sh -30 -link  /export/applications/dte/web/data/outfiles

I don't understand, why case we check $1 and try to set up type as file, link, dir, if $1 is always DAYS
And why script is checking $1 as directory, when we definitely know that it is a number?

Thanks for contribution

Last edited by vbe; 04-05-2017 at 01:25 PM.. Reason: little correction
 

10 More Discussions You Might Find Interesting

1. Solaris

Pl explain the shell script code

if then ROLLBACK=1 ; elif then echo "Nothing to install!" ; echo "Exiting." ; exit 0; Plz explaing what is the ${1:-0} in if loop?:) (3 Replies)
Discussion started by: ysrikanth
3 Replies

2. Shell Programming and Scripting

code formatter for shell script

hello, do anybody know a program to format a shell script code ? i tried "editrocket.com" but this product doesn't format a shell script code. i searched for programs but can't find a shell script code formatter. i have to change a shell script and the style of code is ..... regards (5 Replies)
Discussion started by: bora99
5 Replies

3. Shell Programming and Scripting

Insert C code in shell script

Hi, Anybody know on how to insert C code in shell script. I am writing BLOB data to a database table in C but I don't know on how to insert the C code in shell script. Thanks in advance. (1 Reply)
Discussion started by: badbunny9316
1 Replies

4. Homework & Coursework Questions

i get stuck with this shell script code

i get stuck here . Anyone could check my work? the user type a group of upper case letters at a time with 0 at the end. Find and display the first letter in alphabetic order. For example, input of F, G, K, S, U, G, D, Q, P , the result should be D Any invalid input character (eg. #, $, 3, a,... (5 Replies)
Discussion started by: sbcvn
5 Replies

5. UNIX for Dummies Questions & Answers

XEmacs compilation errors not understandable

Hi all! I am new to this forum. I have recently installed Cygwin and XEmacs on my laptop running Windows Vista. I am studing at the moment and the code I am creating is mainly for that purpose. I am trying to create the algorithm of Insertion sort. When I compile my code in XEmacs i get some... (1 Reply)
Discussion started by: BlueTower
1 Replies

6. Programming

Script Shell in java code

Hello, This is my script shell: echo Mon premier script echo Liste des fichiers : ls -la exit 0 This is my code java: public class test { public static void main(String args) { try { Process process = Runtime.getRuntime().exec("sh script1.sh"); } catch... (2 Replies)
Discussion started by: chercheur857
2 Replies

7. UNIX for Dummies Questions & Answers

Script Shell in java code

Hello, I try to run a script shell from a java program: but it runs only if i do :chmod 777 myShellScript in the terminal Please how can i insert chmod 777 in my java code without going through the terminal? Thank you (1 Reply)
Discussion started by: chercheur857
1 Replies

8. Shell Programming and Scripting

Help with Shell script code

Hello all, I am in a middle of an assignment and i would appreciate any help. How can i write a bash shell script code that checks if all elements in an array are the same numbers. I mean -->array = ( 0,0,0,0,0 ) ( e.g., if then return "OK' fi ) Thank you in advance, (9 Replies)
Discussion started by: Geekie
9 Replies

9. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

10. UNIX for Beginners Questions & Answers

Shell Script - Alphabet in code

Hi e Hi everyone, I can't make this script work, #! /bin/bash declare -A crypt=( ="A" ="a" ="B" ="b" ="C" ="c" =' ' ='!' ) encode () { local word=$1 for ((i=0; i<${#word}; ++i)) ; do local char=${word:$i:1} printf %s' ' ${crypt} done ... (5 Replies)
Discussion started by: Pinguino
5 Replies
All times are GMT -4. The time now is 06:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy