The task is this, you need to run all executable files in the directory. The directory is entered as a parameter. Consideration should be given to the possibility of recursion when the script being run is in the same directory. How can I do that?
1 answer
To do this, use the find utility. A dollar sign followed by a unit is used to indicate in the parameters of the search catalog. Search recursive in all directories. If this is undesirable, add the option -maxdepth 1 before the list of conditions.
find "$1" -executable -not -type d -not -samefile "$0" -exec {} \; Useful reading:
- find (1)
- bash (1) {section "Positional Parameters"}
- An article on Wikipedia about find
- And where is the accounting for possible recursion? - avp
- And can you explain what the -not -type d -exec {} \ \ \ do options do; ? - Vadim Moroz
- one@avp, it is taken into account - the file will be launched - mymedia
- @VadimMoroz,
-not -type d- exclude directories, they cannot be executed;-exec {} \;- run the file for execution - mymedia - So you don’t need to run the script recursively (it’s obvious if you don’t have a goal to suspend the system) - avp
|
forloop?ifread syntax? Then it remains only to use thebasename $0command to "catch" yourself (and you can read the man test to write anifthat will only select executable regular (i.e., regular) files from all others) - avpfor i in .* *; dofor i in .* *; doloops through files,if [ "$i" == "$ME" ]; then continue fiif [ "$i" == "$ME" ]; then continue fitrack itselfif [ -f "$i" -a -x "$i" ]selects the executable regular ... Now try writing yourself (plenty of debug echo will help you) - avp