我有一个 bash 脚本,我想接受一些可选的输入参数,后跟文件名或包含通配符的文件规范。
如果我运行
getopts_problem.sh -td *.txt
脚本愉快地输出当前目录中的 .txt 文件列表:
文件是 readme.txt letter_to_editor.txt someotherfile.txt
如果我运行getopts_problem.sh -td *.ABC
脚本输出
文件是 *.ABC
当前目录中没有扩展名为“.ABC”的文件。然后,出于某种原因,脚本将“*.ABC”解释为文件名。如何让脚本将“*.ABC”识别为要扩展的文件名表达式,而不是实际的文件名?代码如下:
# !/bin/sh
doDry=0
doTimestamp=0
while getopts ":dt" OPT;
do
case $OPT in
d ) doDry=1 ;;
t ) doTimestamp=1 ;;
? ) echo 'Bad options used. '
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
fileList=$@
for file in "$fileList"
do
echo file is $file
done
exit 0