preventing wildcard expansion in bash script -
i've searched here, still can't find answer globbing problems.
we have files "file.1" through "file.5", , each 1 should contain string "completed" if our overnight processing went ok.
i figure it's thing first check there files, want grep them see if find 5 "completed" strings. following innocent approach doesn't work:
files="/mydir/file.*" if [ -f "$files" ]; count=`grep completed $files` if [ $count -eq 5 ]; echo "found 5" else echo "no files?" fi
thanks advice....lyle
per http://mywiki.wooledge.org/bashfaq/004, best approach counting files use array (with nullglob
option set):
shopt -s nullglob files=( /mydir/files.* ) count=${#files[@]}
if want collect names of files, can (assuming gnu grep):
completed_files=() while ifs='' read -r -d '' filename; completed_files+=( "$filename" ) done < <(grep -l -z completed /dev/null files.*) (( ${#completed_files[@]} == 5 )) && echo "exactly 5 files completed"
this approach verbose, guaranteed work highly unusual filenames.
Comments
Post a Comment