Forum Discussion

Michaelyang's avatar
Michaelyang
Icon for Cirrostratus rankCirrostratus
Dec 16, 2022

loop for iCall

Hello,

I created an icall script to automatically delete files
ex. When the number of files > 5, the oldest file can be deleted until 5 files are left
Here is my code:

modify script backup_script {
    app-service none
    definition {
        set total [exec ls -l /var/local/ucs | grep "ucs$" | wc -l]
        set old "[exec ls -tr /var/local/ucs | grep "ucs$" | head -1]"

        if { $total > 5 } then {
            exec rm -f /var/local/ucs/$old
        } else {
        }
    }
    description none
    events none
}

I thought it would work, but when I tested adding 10 files, I found that running the script once only deleted one file ...... Couldn't delete 5 files, couldn't reach my requirement...

Please tell me how I can improve it

Any help is appreciate.

 

  • Hi Michaelyang,

    check the iCall script below... 

     

    sys icall script UCS_CleanUp {
        app-service none
        definition {
                    eval { exec ls -t } [glob /var/local/ucs/*.ucs] { | awk "NR>5" | xargs -d "\n" rm -f }
            }
        description none
        events none
    }
    sys icall handler periodic UCS_CleanUp {
        interval 3600
        script UCS_CleanUp
    }
    

     

    The [glob] command is used as a workaround for [exec]'s missing ability to deal with "*" matches. It passes the output of [glob] to "ls -t", to sort the results based on their creation time (newest first).  "awk NR>5" will then be used to skip the first 5 results. The remaining results will then become passed to the "xargs -d \n"command to filter any new lines and then pass the remaining string to "rm -f" to delete the obsolete files. 

     

    [itacs@kw-f5-dev:Active:Standalone] ucs # ls -t /var/local/ucs/*.ucs
    /var/local/ucs/j_0_days_old.ucs  /var/local/ucs/r_2_days_old.ucs  /var/local/ucs/d_4_days_old.ucs  /var/local/ucs/x_6_days_old.ucs
    /var/local/ucs/g_1_days_old.ucs  /var/local/ucs/e_3_days_old.ucs  /var/local/ucs/x_5_days_old.ucs
    [itacs@kw-f5-dev:Active:Standalone] ucs # tclsh
    % 
    % exec ls -t /var/local/ucs/*.ucs
    ls: cannot access /var/local/ucs/*.ucs: No such file or directory
    % 
    % glob "/var/local/ucs/*.ucs"
    /var/local/ucs/g_1_days_old.ucs /var/local/ucs/x_7_days_old.ucs /var/local/ucs/x_6_days_old.ucs /var/local/ucs/j_0_days_old.ucs /var/local/ucs/e_3_days_old.ucs /var/local/ucs/r_2_days_old.ucs /var/local/ucs/x_5_days_old.ucs /var/local/ucs/d_4_days_old.ucs
    % 
    % eval { exec ls -t } [glob "/var/local/ucs/*.ucs"] 
    /var/local/ucs/j_0_days_old.ucs
    /var/local/ucs/g_1_days_old.ucs
    /var/local/ucs/r_2_days_old.ucs
    /var/local/ucs/e_3_days_old.ucs
    /var/local/ucs/d_4_days_old.ucs
    /var/local/ucs/x_5_days_old.ucs
    /var/local/ucs/x_6_days_old.ucs
    /var/local/ucs/x_7_days_old.ucs
    % 
    % eval { exec ls -t } [glob "/var/local/ucs/*.ucs"] { | awk "NR>5" }
    /var/local/ucs/x_5_days_old.ucs
    /var/local/ucs/x_6_days_old.ucs
    /var/local/ucs/x_7_days_old.ucs
    % 
    % eval { exec ls -t } [glob "/var/local/ucs/*.ucs"] { | awk "NR>5" | xargs -d "\n" rm -f }
    % ^C
    [itacs@kw-f5-dev:Active:Standalone] ucs # ls -t /var/local/ucs/*.ucs
    /var/local/ucs/j_0_days_old.ucs  /var/local/ucs/r_2_days_old.ucs  /var/local/ucs/d_4_days_old.ucs
    /var/local/ucs/g_1_days_old.ucs  /var/local/ucs/e_3_days_old.ucs
    [itacs@kw-f5-dev:Active:Standalone] ucs #

     

    No need for a loop... 😉

    Cheers, Kai

  • Hi Michaelyang,

    check the iCall script below... 

     

    sys icall script UCS_CleanUp {
        app-service none
        definition {
                    eval { exec ls -t } [glob /var/local/ucs/*.ucs] { | awk "NR>5" | xargs -d "\n" rm -f }
            }
        description none
        events none
    }
    sys icall handler periodic UCS_CleanUp {
        interval 3600
        script UCS_CleanUp
    }
    

     

    The [glob] command is used as a workaround for [exec]'s missing ability to deal with "*" matches. It passes the output of [glob] to "ls -t", to sort the results based on their creation time (newest first).  "awk NR>5" will then be used to skip the first 5 results. The remaining results will then become passed to the "xargs -d \n"command to filter any new lines and then pass the remaining string to "rm -f" to delete the obsolete files. 

     

    [itacs@kw-f5-dev:Active:Standalone] ucs # ls -t /var/local/ucs/*.ucs
    /var/local/ucs/j_0_days_old.ucs  /var/local/ucs/r_2_days_old.ucs  /var/local/ucs/d_4_days_old.ucs  /var/local/ucs/x_6_days_old.ucs
    /var/local/ucs/g_1_days_old.ucs  /var/local/ucs/e_3_days_old.ucs  /var/local/ucs/x_5_days_old.ucs
    [itacs@kw-f5-dev:Active:Standalone] ucs # tclsh
    % 
    % exec ls -t /var/local/ucs/*.ucs
    ls: cannot access /var/local/ucs/*.ucs: No such file or directory
    % 
    % glob "/var/local/ucs/*.ucs"
    /var/local/ucs/g_1_days_old.ucs /var/local/ucs/x_7_days_old.ucs /var/local/ucs/x_6_days_old.ucs /var/local/ucs/j_0_days_old.ucs /var/local/ucs/e_3_days_old.ucs /var/local/ucs/r_2_days_old.ucs /var/local/ucs/x_5_days_old.ucs /var/local/ucs/d_4_days_old.ucs
    % 
    % eval { exec ls -t } [glob "/var/local/ucs/*.ucs"] 
    /var/local/ucs/j_0_days_old.ucs
    /var/local/ucs/g_1_days_old.ucs
    /var/local/ucs/r_2_days_old.ucs
    /var/local/ucs/e_3_days_old.ucs
    /var/local/ucs/d_4_days_old.ucs
    /var/local/ucs/x_5_days_old.ucs
    /var/local/ucs/x_6_days_old.ucs
    /var/local/ucs/x_7_days_old.ucs
    % 
    % eval { exec ls -t } [glob "/var/local/ucs/*.ucs"] { | awk "NR>5" }
    /var/local/ucs/x_5_days_old.ucs
    /var/local/ucs/x_6_days_old.ucs
    /var/local/ucs/x_7_days_old.ucs
    % 
    % eval { exec ls -t } [glob "/var/local/ucs/*.ucs"] { | awk "NR>5" | xargs -d "\n" rm -f }
    % ^C
    [itacs@kw-f5-dev:Active:Standalone] ucs # ls -t /var/local/ucs/*.ucs
    /var/local/ucs/j_0_days_old.ucs  /var/local/ucs/r_2_days_old.ucs  /var/local/ucs/d_4_days_old.ucs
    /var/local/ucs/g_1_days_old.ucs  /var/local/ucs/e_3_days_old.ucs
    [itacs@kw-f5-dev:Active:Standalone] ucs #

     

    No need for a loop... 😉

    Cheers, Kai