Filenames with wildcards in variables

后端 未结 4 649
孤独总比滥情好
孤独总比滥情好 2021-01-13 02:22
#!/bin/bash
outbound=/home/user/outbound/
putfile=DATA_FILE_PUT_*.CSV
cd $outbound
filecnt=0
for file in $putfile; do let filecnt=filecnt+1; done
echo \"Filecount: \         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 02:59

    Put set -x just below the #! line to watch what your script is doing.

    If there is no matching file, then the wildcard is left unexpanded, and the loop runs once, with file having the value DATA_FILE_PUT_*.CSV.

    To change that, set the nullglob option. Note that this only works in bash, not in sh.

    shopt -s nullglob
    putfile=DATA_FILE_PUT_*.CSV
    for file in $putfile; do let filecnt=filecnt+1; done
    

    Note that the putfile variable contains the wildcard pattern, not the list of file names. It might make more sense to put the list of matches in a variable instead. This needs to be an array variable, and you need to change the current directory first. The number of matching files is then the length of the array.

    #!/bin/bash
    shopt -s nullglob
    outbound=/home/user/outbound/
    cd "$outbound"
    putfiles=(DATA_FILE_PUT_*.CSV)
    echo "Filecount: " ${#putfiles}
    

    If you need to iterate over the files, take care to protect the expansion of the array with double quotes, otherwise if a file name contains whitespace then it will be split over several words (and if a filename contains wildcard characters, they will be expanded).

    #!/bin/bash
    shopt -s nullglob
    outbound=/home/user/outbound/
    cd "$outbound"
    putfiles=(DATA_FILE_PUT_*.CSV)
    for file in "${putfiles[@]}"; do
      echo "Processing $file"
    done
    

提交回复
热议问题