Bash:检查目录中是否存在txt文件,如果是,则使用aria2c处理它们

时间:2014-09-07 14:20:44

标签: bash file

好的,我正在尝试创建一个......

的脚本

一个。将在我删除txt笔记的文件夹中查找,如果其中任何一个以" a2c _"它会将其识别为" aria2c下载列表"。这带来了......

湾将第一个匹配文件与一个类似于txt文件的目录名一起传递给aria2c,以便它将txt中找到的每个URL下载到同名目录。

我结束了这个......:

#!/bin/bash
#PARAMETERS: _______________________________
workingdir="/home/username/downloads/"
#___________________________________________

echo Working dir is: $workingdir
mkdir -p $workingdir
echo "Making dir"
cd $workingdir

if [ -f a2c_* ]
then
        echo "Found files"
        mkdir -p !a2c_downloaded
        echo "Making !a2c_downloaded dir"
        counter=0
        echo "Set counter to 0"
        for f in a2c_*.txt
        do
                counter=$((counter+1))
                echo Download List File is: $f
                echo $counter file processing.
                tempfile=${f%%.*}
                tempfile="`echo "$tempfile" | sed ' s/a2c_//' `"
                downdir=$tempfile
                echo Download Dir is: $downdir
                mkdir -p $downdir
                echo ___________________________________________
                echo $endfilename
                aria2c --auto-file-renaming -i $f -d $downdir --force-sequential
                echo "Will download $f to $downdir"
                sleep 5
                mv $f !a2c_downloaded/
        done
else
        echo "No files found"
fi

......当我测试它时起作用了。今天,一天之后,我在dir中抛出一些a2c _ * .txt文件,我遇到了意外的操作员"错误。有任何想法吗?是否有更简单的方法来完成我想要做的事情?

感谢。

_UPDATED: _________________________________

#!/bin/bash
#PARAMETERS: _______________________________
workingdir="/home/username/downloads/"
MatchPattern="a2c_*"
#___________________________________________

echo Working dir is: $workingdir
mkdir -p $workingdir
echo "Making dir"
cd $workingdir

#if [ -f a2c_* ];
#if [ find . -maxdepth 1 -type f -name "a2c_*.txt" 2>/dev/null | grep "a2c_*" ]
#if [ -f a2c_* ]
#if [ "$?" = "0" ];
echo "Match Pattern set as $MatchPattern"
echo "Now looking in $workingdir for $MatchPattern"
echo "Manual list:"
echo "_________________________________________"
MatchList=$(ls -1 "$MatchPattern")
echo "$MatchList"
echo "_________________________________________"
if ls -1 $MatchPattern >/dev/null 2>&1
then
        echo "Found files"
        mkdir -p !a2c_downloaded
        echo "Making !a2c_downloaded dir"
        counter=0
        echo "Set counter to 0"
        for f in a2c_*.txt
        do
                counter=$((counter+1))
                echo "Download List File is: $f"
                echo $counter file processing.
                tempfile=${f%%.*}
                tempfile="`echo "$tempfile" | sed ' s/a2c_//' `"
                downdir=$tempfile
                echo "Download Dir is: $downdir"
                mkdir -p "$downdir"
                echo ___________________________________________
                echo "$endfilename"
                aria2c --auto-file-renaming -i "$f" -d "$downdir" --force-sequential
                echo "Will download $f to $downdir"
                sleep 5
                mv "$f" !a2c_downloaded/
        done
else
        echo "No files found"
fi

......上述工作经过一些修复http://www.shellcheck.net/告诉我要做。问题是,它建议我在行中使用双引号:

if ls -1 $MatchPattern >/dev/null 2>&1

if ls -1 "$MatchPattern" >/dev/null 2>&1

..."双引号以防止全局和单词分裂。",但是当我这样做时,脚本停止为我工作。我应该保留原样吗?它似乎工作正常 - 现在。

1 个答案:

答案 0 :(得分:1)

而不是

if [ -f a2c_* ]
then

你可以试试这个:

file_exists() {
  for _i do
    [ -f "$_i" ] && break
  done
}

然后

if file_exists a2c_*
then