input_dir="~/path/to/dir/" file_ext=".fastq.gz" input_path="${input_dir}*${file_ext}" echo "$input_path" # returns: ~/path/to/dir/*.fastq.gz f in "$input_path" ; echo "$f" ; done # returns: ~/path/to/dir/*.fastq.gz
i expecting list of files extension .fastq.gz.
unfortunately, can't store glob pattern without losing information elements , aren't intended quoted. thus, 2 separate variables need used prefix , suffix at glob expansion time, glob expanded unquoted between them:
input_dir=~/path/to/dir/ # ~ must not in quotes; use $home if want quote file_ext=".fastq.gz" f in "${input_dir}"/*"${file_ext}"; echo "found $f" done
note pattern -- "$prefix"/*"$suffix"
-- both expansions within quotes; prevents expanded portions being subject string-splitting or glob expansion.
if want store directive controlling how files identified in shell variable, consider array storing find
arguments:
# array variable, 1 entry per argument find_args=( ~/path/to/dir -maxdepth 1 -name '*.fastq.gz' ) while ifs= read -r -d '' filename; printf 'found %q\n' "$filename" done < <(find "${find_args[@]}" -print0)
another option perform expansion on, , store result in array variable, iterating on contents of variable in for
loop:
input_dir=~/path/to/dir file_ext=.fastq.gz input_files=( "$input_dir"/*"$file_ext" ) f in "${input_files[@]}"; printf 'found %q\n' "$f" done
by way -- aware if glob expression matches no files, glob default evaluate expression itself. may wise check whether $f
actually exists, handle case, or enable nullglob
shell option (which disables behavior) @ top of script.
thus:
shopt -s nullglob # prevents non-matching glob evaluating
...or...
for f in "$input_dir"/*"$file_ext"; # lack of quotes acceptable here because [[ ]] suppresses string-splitting , globbing [[ -e $f || -l $f ]] || { printf '%s\n' "$f not found" >&2; continue; } printf 'found %q\n' "$f" done
Comments
Post a Comment