vendredi 27 février 2015

Search for matching files with wildcards without using find in a bash script


For an assignment, I'm supposed to write a bash script that can act as a very basic find, but without actually using the find command. For example, if I put in this input:


my_find ~/homedir/ -name 'test*' -name '*.o' -type f


I need it to search ~/homedir/ for anything matching the name test* with the wildcard character, then AND that and search within those results for anything matching a *.o name, and finally (although possibly redundant), search for only files. Finally, it will list all the results.


As of now, I've made a variable that stores the directory path (dirpath=$1). I then use shift to get rid of the first argument, and then do this (note that -type isn't implemented yet):



while (($#)); do
if [[ $1 == "-name" ]]; then
# process the name here?
shift 2 # get rid of -name "name"
fi
done


I'm kind of wondering how I can structure or even do this. I have a partially done function that will recurse through the directory path, but I'm not sure how / where I can process the argument passed in. Here is the recursive function to look through each directory.



recurse_path () {
for i in "$@"; do
# process and store matching file names in the array?
if [[ -d "${i}" ]]; then
cd "${i}"
recurse_path *
fi
done
}


So how can I search for my matching file names and store the results in an array while using this function? Also, is this the right way to do this, or is there a potentially easier way to update the results I get as each command is looked at? I'm brand new to bash scripting so I would appreciate any advice.



Aucun commentaire:

Enregistrer un commentaire