Shell script code not really understandable for me


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script code not really understandable for me
# 1  
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
# 2  
Old 04-05-2017
Quote:
Originally Posted by digioleg54
...
...
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"
}
  
  
...
 ...

...
I don't understand, why case we check
Code:
$1

and try to set up type as file, link, dir, if
Code:
$1

is always DAYS
And why script is checking CODE]$1 [/CODE] as directory, when we definitely know that it is a number?
...
I think it is a generally accepted convention that square brackets ("[]") means the argument is optional.
So, in your script, only "dir1" is mandatory - the rest of the arguments are optional.
In other words, $1 will be a number if it is specified.

You could, alternatively, execute your script as such:
Code:
 /export/applications/dte/sh/clean_disk.sh /export/applications/dte/web/release

For such an invocation, dir1 is "/export/applications/dte/web/release"
and DAYS takes the default value of 10.
Which is the same as:
Code:
 /export/applications/dte/sh/clean_disk.sh -10 /export/applications/dte/web/release

(At least that is what the "print_usage" function tries to convey. I haven't really reviewed or tested your script to see if it actually does what it says it does.)
# 3  
Old 04-05-2017
I think you left out an important part of the script. There must be a "shift" somewhere below that "esac", which will change the value of $1.

When $1="A", $2="B", $3="C", shift tosses the value of $1 then sets $1="B", $2="C".

If you give shift a value, like shift 2, it deletes n leading arguments, not just one.

In this manner you can loop through all arguments while just checking $1.

I don't like the way they do the loop, though. Just because an argument is blank doesn't mean its the last argument, what if it's supposed to be blank for some reason?
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 04-05-2017
Sorry, you are right. There is a shift and I already understand, what is going on

Thanks for contribution
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question