Forum Discussion
loop for iCall
- 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
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 Kai_Wilke,
Thanks for your help.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com