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?

  • What is the problem? Can you loop through files in a for loop? if read syntax? Then it remains only to use the basename $0 command to "catch" yourself (and you can read the man test to write an if that will only select executable regular (i.e., regular) files from all others) - avp
  • Yes, it's all clear, but how to write all this in the bash script? - Vadim Moroz
  • Well, for i in .* *; do for i in .* *; do loops through files, if [ "$i" == "$ME" ]; then continue fi if [ "$i" == "$ME" ]; then continue fi track itself if [ -f "$i" -a -x "$i" ] selects the executable regular ... Now try writing yourself (plenty of debug echo will help you) - avp

1 answer 1

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:

  • 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