Problem with script - details in thread

Discussion in 'Programming/Scripts' started by Poliman, Apr 18, 2018.

  1. Poliman

    Poliman Member

    I have a little problem with this script. It is developed for removing not needed .gz files from specified path which is put to script as parameter.
    It works quite well except clearing .gz files exactly from specified path. For example when I put path /var/log to script all .gz files will be cleared in directiories which are inside /var/log but .gz from this path will be untouched. Currently I don't know how to refactor this script to works properly. Can anybody help with this?

    Code:
    #!/bin/bash
    #Pass path as an argument to script ./cleaner /path/to/cleaning
    #create 124MB filename.gz -> fallocate -l 124M filename.gz
    echo "Script for deleting not necessary archived log files."
    cd $1
    
    function clearFiles()
    {
        for directory in *
        do
            if [ -d "$directory" ]
            then
                cd $directory
                #pushd $directory
                x="*.gz"
    
                for plik in $x
                do
                    if [ -e $plik ]
                    then
                        echo "LogFile '$plik' exists and can be deleted"
                        echo "Deleting file $plik":
                        #rm $plik
                        echo -ne '###               (25%)\r'
                        sleep 0.3
                        echo -ne '######            (50%)\r'
                        sleep 0.3
                        echo -ne '#########         (75%)\r'
                        sleep 0.3
                        echo -ne '############      (100%)\r'
                        echo -ne '\n'
                        echo -e "$plik deleted \n"
                    else
                        echo "I think in $directory currently is nothing to delete."
                    fi
                done
                clearFiles
                cd ..
                #popd
            else
                echo "$directory does not contain any archived file or it's a file."
            fi
        done
        echo "Script is finishing his job."
    }
    
    #Call function
    clearFiles
     
  2. Taleman

    Taleman Active Member HowtoForge Supporter

    The script does cd $1, which makes /var/log the current working directory.
    Then the script does cd $directory for each file entry in /var/log, and does it's stuff, but the script never does anything in /var/log directory. I mean it goes to subdirectories of /var/log only.
    Maybe tuning logrotate would be a better solution? Home made maintenance scripts take work to keep working after changes in the system.
     
    Poliman likes this.
  3. Poliman

    Poliman Member

    I created this script and few more, because I want to learn bash. ;) In above case I should add removing .gz files before for loop declaration:
    Code:
    cd $1
    rm *.gz
    for directory in *
    do
         if [ -d "$directory" ]
         ...
    Am I right?
    Btw do you think this script is valid syntactically, logically? There is also recursion.
     
    Last edited: Apr 19, 2018
  4. Taleman

    Taleman Active Member HowtoForge Supporter

    If you want to make good bash scripts, use set -e and set -u. In addition, be wary with spaces in parameters and file names. There is this good short intro: https://www.davidpashley.com/articles/writing-robust-shell-scripts/ .
    As for the syntactically correct, who knows? The bash syntax is so curious you just have to run it and see what bash makes of it. Use set -x to make the script echo all commands it is going to execute.
    If your script becomes complex, maybe learn Python or some othet script language that has less strange syntax and semantics?
     
  5. Poliman

    Poliman Member

    Thank you for answer. I am going to check how use set -e/set -u/set -x.
     

Share This Page