Question:
I want to remove files(whose name contains “extract”) older than 7 days from various directories in a particular path. If I run the script without any argument the script should be able to delete the files from all the directories inside that particular path. If I run the script using any arguments , then the script should not delete files from those specific directories.Example :-
Path – /languages/analytics
Let’s assume directory “analytics” contains 10 dirctories like c,java,javascript,python,shell,golang,scala,ruby,react,angular.This directory also contains few files along with these 10 directories.
Scenario1 – I want to remove the files older than 7 days from all the 10 directories.
Scenario2 – I want to remove the files older than 7 days from all directories except angular.
Scenario3 – I want to remove the files older than 7 days from all directories except ruby,react,shell.
So basically whatever arguments I pass , the script should not delete files from those particular directories.The number of arguments may vary as per requirement.And if I run without arguments, then the script should delete older files from all the directories in that path.The number of directories may vary as well.
Here is what I have written so far …
sh scriptname -> successfully deletes files from all directories in /languages/analytics path.
sh scriptname ruby -> successfully delete files from all directories except ruby.
Can you please advise how to enhance this script to take as many arguments as possible and the script should not delete files from those particular directories which are passed in the argument ?
Answer:
Try this Shellcheck-clean code:for arg; do
loops over the positional arguments ($1
,$2
, …)continue 2
skips to the next iteration of the outer (for dir ...
) loop.
If you have better answer, please add a comment about this, thank you!