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_scrip...
  • Kai_Wilke's avatar
    Dec 17, 2022

    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