How to recursively search directories in a minimal shell (no grep, find, etc.)?

I work with an embedded system that works with QNX, which has a truncated shell (KSH).

I want to find everything to run all the executables in the file system that match the following:

*/shle/*-*_test 

The "shle" directory can be displayed up to 4 levels as root. My current approach is to execute the following commands:

 for shle in ./shle ./*/shle ./*/*/shle ./*/*/*/shle do for exe in $shle/*-*_test do echo running: $exe $exe done done 

Is there a cleaner or faster way to do this? Are there any commands other than grep and find that I should try?

+4
source share
2 answers

If you don't have find , you cannot do much better than what you did: list the levels. If you need to go down to arbitrary levels, you can do this with a recursive function (but look, recursion is complicated when all you have is global variables). Fortunately, with a known maximum depth, this is much easier.

There is little room for improvement: you'd better put double quotation marks around all variable substitutions, if there is a file name containing spaces or special characters. And you do not check if $exe and the executable file exist (it may be a template โ€ฆ/*-*_test if this template does not match anything, or perhaps it may be an unexecutable file).

 for shle in shle */shle */*/shle */*/*/shle; do for exe in "$shle"/*-*_test; do test -x "$exe" && "$exe" done done 

If you do not even have test (if you have ksh, it is built-in, but if it is a stripped-down shell, it may be absent), you can leave with a more complex test to find out if the template has been expanded:

 for shle in shle */shle */*/shle */*/*/shle; do for exe in "$shle"/*-*_test; do case "$exe" in */"*-*_test") :;; *) "$exe";; esac done done 

(I am surprised that you do not have find , I thought that QNX comes with the full POSIX package, but I am not familiar with the QNX ecosystem, it may be a stripped down version of the OS for a small device.)

+2
source

You can try using the globstar option and specify ** / shle to find directories with one or more levels.

From the ksh man page:

  โˆ— Matches any string, including the null string. When used for filename expansion, if the globstar option is on, two adjacent โˆ— by itself will match all files and zero or more directories and subdirectories. If followed by a / then only directories and subdirectories will match. 
+3
source

Source: https://habr.com/ru/post/1379660/


All Articles