Here's my problem: I have to resolve various filenames/locations (data directory may have sub-directories) which are user-configurable. If I can resolve the filename completely prior to the loop the following script works:
[prompt] more test.sh #! /usr/bin/env bash newfile=actual-filename for directory in `find -L ${FILE_PATH}/data -type d`; do for filename in `ls -1 ${directory}/${newfile} 2>/dev/null`; do if [[ -r ${filename} ]]; then echo "Found ${filename}" fi done done [prompt] ./test.sh [prompt] Found ${SOME_PATH}/actual-filename
However, if newfile has any wildcarding in it the inner-loop will not run. Even if it only returns a single file.
I would use find with some regex, but to auto-generate the proper expressions and do the substitutions for somethings will be tricky (eg pgb.f0010{0930,1001}{00,06,12,18} would correlate to some files associate with Sep. 30 and Oct 1 of 2010 the first grouping is computed by my script for a provided date).
pgb.f0010093000 pgb.f0010093006 pgb.f0010093012 pgb.f0010093018 pgb.f0010100100
pgb.f0010100106 pgb.f0010100112 pgb.f0010100118
I'm running Fedora 15 64-bit.
newfile="*" find -L ${FILE_PATH}/data -name "${newfile}" \ | while read filename do if [[ -r ${filename} ]]; then echo "Found ${filename}" fi done
-or-
newfile="*" find -L ${FILE_PATH}/data -name "${newfile}" -readable -exec echo "Found {}" \;
-or with regular expressions-
newfile='.*/pgb.f0010(0930|1001)(00|06|12|18)' FILE_PATH=. find -L ${FILE_PATH}/. -regextype posix-extended \ -regex "${newfile}" -readable -exec echo "Found {}" \;
The root problem is that there is a dependence on shell expansion in the broken script. Use eval:
#! /usr/bin/env bash FILE_PATH="." newfile=pgb.f0010{0930,1001}{00,06,12,18} for directory in `find -L ${FILE_PATH}/data -type d`; do for filename in `eval ls -1 ${directory}/${newfile} 2>/dev/null`; do if [[ -r ${filename} ]]; then echo "Found ${filename}" fi done done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.