变量中带有通配符的文件名 [英] Filenames with wildcards in variables

查看:98
本文介绍了变量中带有通配符的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #!/bin/bash出站=/家庭/用户/出站/putfile = DATA_FILE_PUT _ *.CSVcd $ outboundfilecnt = 0用于$ putfile中的文件;让filecnt = filecnt + 1;完毕echo"Filecount:" $ filecnt 

因此,当出站目录中有文件时,此代码可以很好地工作.我可以将文件放入出站路径,只要它们与putfile掩码匹配,然后文件就会按预期增加.

问题出在哪里,如果我在 $ outbound 中没有文件的情况下运行此文件.如果有零个文件,那么 $ filecnt 仍然返回 1 ,但是我想让它在没有文件的情况下返回 0 .

我想念一些简单的东西吗?

解决方案

set -x 放在#!行的正下方,以查看您的脚本在做什么./p>

如果没有匹配的文件,则通配符不展开,并且循环运行一次,其中 file 的值为 DATA_FILE_PUT _ *.CSV .

要更改此设置,请设置nullglob选项.请注意,这仅适用于bash,不适用于sh.

  shopt -s nullglobputfile = DATA_FILE_PUT _ *.CSV用于$ putfile中的文件;让filecnt = filecnt + 1;完毕 

请注意, putfile 变量包含通配符模式,而不是文件名列表.将匹配列表放在变量中可能更有意义.这需要是一个数组变量,并且您需要首先更改当前目录.然后,匹配文件的数量就是数组的长度.

 #!/bin/bashshopt -s nullglob出站=/家庭/用户/出站/cd"$ outbound"putfiles =(DATA_FILE_PUT _ *.CSV)回声"Filecount:" $ {#putfiles} 

如果需要遍历文件,请注意用双引号保护数组的扩展,否则,如果文件名包含空格,则它将被分成多个单词(如果文件名包含通配符,则它们将被扩展.)

 #!/bin/bashshopt -s nullglob出站=/家庭/用户/出站/cd"$ outbound"putfiles =(DATA_FILE_PUT _ *.CSV)用于"$ {putfiles [@]}"中的文件;做回显正在处理$ file"完毕 

#!/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: " $filecnt

So this code works well when there are files located in the outbound directory. I can place files into the outbound path and as long as they match the putfile mask then the files are incremented as expected.

Where the problem comes in is if I run this while there are no files located in $outbound. If there are zero files there $filecnt still returns a 1 but I'm looking to have it return a 0 if there are no files there.

Am I missing something simple?

解决方案

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

这篇关于变量中带有通配符的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆