for loop(f in * .txt)如果不存在* .txt文件则抛出错误


1

我有一个小的bash脚本,看起来像这样:

#!/bin/sh
for f in ./*.txt
do
 f=$(basename $f)
 echo "Processing $f"
 scp somewhere
 mv $f done/`basename $f`
done

这是完美的工作,但只要没有文件,它就会抛出这个错误:*.txt: No such file or directory 我想知道为什么它进入循环以及我怎么能这样做(如果可能没有新的?)

Answers:


1

如果没有*.txt文件,则字符串保持未展开状态并输入循环。为防止这种情况,请设置nullglob选项。

shopt -s nullglob
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.