rm -f不会处理bash脚本中的所有参数

时间:2015-09-09 10:18:07

标签: bash expansion rm

在bash中有一个特殊的问题。我想创建一个脚本,该脚本采用文件名的前缀,并删除以该前缀开头并以某些给定后缀结尾的所有文件。为此,我有以下代码行:

#!/bin/bash

if [ $# -lt 1 ]; then
        echo "Please provide an argument";
        exit 1
fi

# Ending dot already included in $1
rm -f $1"aux" $1"bbl" $1"blg" $1"synctex.gz" $1"log"

# But even if you call it wrong...
rm -f $1".aux" $1".bbl" $1".blg" $1".synctex.gz" $1 ".log"

exit 0

不幸的是,当我按照预期调用我的脚本(名为cleanlatex)时:

cmd$ cleanlatex lpa_aaai.

仅删除.aux文件。很明显rm -f并没有将其应用扩展到所有参数,如果我明确地运行rm -f lpa_aaai.aux lpa_aaai.bbl ...,这就是我要做的事情。我在这里做错了什么?

编辑:要回答@ Etan的问题,这是我在添加这些命令时看到的内容:

+ '[' 1 -lt 1 ']'
+ rm -v lpa_aaai.aux lpa_aaai.bbl lpa_aaai.blg lpa_aaai.synctex.gz lpa_aaai.log
removed ‘lpa_aaai.aux’
removed ‘lpa_aaai.bbl’
removed ‘lpa_aaai.blg’
removed ‘lpa_aaai.synctex.gz’
removed ‘lpa_aaai.log’
+ rm -v lpa_aaai..aux lpa_aaai..bbl lpa_aaai..blg lpa_aaai..synctex.gz lpa_aaai. .log
rm: cannot remove ‘lpa_aaai..aux’: No such file or directory
rm: cannot remove ‘lpa_aaai..bbl’: No such file or directory
rm: cannot remove ‘lpa_aaai..blg’: No such file or directory
rm: cannot remove ‘lpa_aaai..synctex.gz’: No such file or directory
rm: cannot remove ‘lpa_aaai.’: No such file or directory
rm: cannot remove ‘.log’: No such file or directory

第二组由cannot remove消息组成,与我无关:我只将这些删除视为故障保护。这完全符合我的需要。谢谢。

0 个答案:

没有答案