0
\$\begingroup\$

The idea is to perform indexing of filetrees while excluding certain directories. I want to give the user a simple conf file instead of having to edit the script itself. This is what I came up with:

conf.file:

cache
home/files

program:

#!/usr/bin/env bash

function get_opts {
        while read opt
        do
        echo "-path /data/data/com.termux/$opt -prune -o \\" >> fn_temp
        done < conf.file
}

function build_search_fn {
        touch fn_temp
        echo "function do_search {" > fn_temp
        echo "find /data/data/com.termux \\" >> fn_temp
        get_opts
        echo "-print" >> fn_temp
        echo "}" >> fn_temp
}

build_search_fn
source fn_temp
do_search >> output

It does what I want, but I have a strong feeling that it's not the 'proper' way of doing it. Besides the obvious lack of putting the base path into variables and some errorhandling I'm eager to learn about other approaches to do this.

\$\endgroup\$
1
\$\begingroup\$

You can use subcommands in a command:

echo "This $(date) is a date calculated in the echo command"

When you don't need additional control statements, you can use combined code like:

find /data/data/com.termux \
   $(awk '{printf("-path %s%s -prune -o ", "/data/data/com.termux/", $0)}' conf.file) \
   -print >> output

The awk command might be new for you, it is a replacement for your loop over the conf file. The same result can be found with sed:

sed 's#.*#/data/data/com.termux/& -prune -o #' conf.file

I think awk is the best here.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I left awk because I wanted a pure bash version. I don't know if it adds any value to throw a regex in just for the sake of it. Certainly for more eloberate things.I'll except your's as answer anyway since it's for sure the more common way.Edit: Stroke that regex since awk is used for formatting the string not matching. \$\endgroup\$ – chalybeum Mar 21 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.