• # Mon script, tadaaa

    Posté par . En réponse au message Comment faire ?. Évalué à 1.

    #!/bin/bash
    #sizescan : this scripts scans the current directory/directory tree in order to give the smallest and
    #largest file.
    #usage : sizescan [-r] [directory]
    #if no directory is given the script will scan the current one. If the -r option is given the script will scan
    #directories recursively
    #variables initializations
    directoryDoesNotExist=1
    recursive=1 			#starts with false
    syntaxError=1
    #check the options for incorrect combinations
    #if we have two arguments the first one should be -r and the second one should be a valid directory
    if [ $# = 2 ]
     then
     #is the first arg -r ?
     if [ 1ドル = "-r" ]
    	then
    	recursive=0
    	else
    	syntaxError=0
     fi
     #is the second argument a valid path ?
     if [ -d 2ドル ]
    	then
    	toScan=2ドル
    	else
    	directoryDoesNotExist=0
     fi
    fi
    #if we face a single argument it should be -r or a valid path
    if [ $# = 1 ]
     then
     if [ -d 1ドル ]
     	then
    	toScan=1ドル
    	else
    	if [ 1ドル = "-r" ]
    	 then
    	 recursive=0
    	 toScan=`pwd`
    	 else
    	 directoryDoesNotExist=0
    	fi
     fi
    fi
    #otherwise we just set the scanned directory to current one
    if [ $# = 0 ]
     then
     toScan=`pwd`
    fi
    #if we have a syntax error we print the appropriate message
    if [ $syntaxError = 0 ]
     then
     echo Usage sizescan [-r] [directory]
     echo -- -r option scans directories recursively
     exit 1
    fi
    #if the provided directory doesn t exist we print the appropriate errror message
    if [ $directoryDoesNotExist = 0 ]
     then
     echo Provided directory doesn\'t exist!
     exit 1
    fi
    #Okay, at this stage our options are correct, we know what we want to do and we are sure the directory actually exists
    #if we don t want a recursive search we set the parameter maxdepth to 1 in order not to scan subdirs
    if [ $recursive = 0 ]
     then
     recursive=
     else
     recursive="-maxdepth 1"
    fi
    #finally we build the commands that will give us the results and execute them
    command_min="ls -la \`find "${toScan}" -type f "${recursive}"\` | awk '{print \5,ドル\9ドル}' | sort -n | head -n 1 | awk '{print \"Smallest - Size: \", \1,ドル \" File:\", \2ドル}'"
    command_max="ls -la \`find "${toScan}" -type f "${recursive}"\` | awk '{print \5,ドル\9ドル}' | sort -nr | head -n 1 | awk '{print \"Largest - Size: \", \1,ドル \" File:\", \2ドル}'"
    eval $command_min 
    eval $command_max