Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在下载文件之前创建循环检查当前文件?_Bash_Loops_If Statement - Fatal编程技术网

Bash 如何在下载文件之前创建循环检查当前文件?

Bash 如何在下载文件之前创建循环检查当前文件?,bash,loops,if-statement,Bash,Loops,If Statement,我就是这样开始的。简单的 a="abc" b="ABC" if [ "$a" == "$b" ]; then echo "Strings $a & $b match" else echo "Strings $a don't match $b" fi 然后我试了这个。它总是声明跳过,即使我还没有下载它 input="file" while IFS= read -r line; do path="${line%/*}" file="${line##*/}" if [ $

我就是这样开始的。简单的

a="abc"
b="ABC"

if [ "$a" == "$b" ]; then
    echo "Strings $a & $b match"
else
    echo "Strings $a don't match $b"
fi
然后我试了这个。它总是声明跳过,即使我还没有下载它

input="file"
while IFS= read -r line; do
path="${line%/*}"
file="${line##*/}"

if [ $line = $file ]; then
     echo "skipping ${file##*/}"
else
     wget -nc -q --show-progress "${path}/${file}"
  sleep 1
fi
done < "$input"

我真的只想运行一个基本的循环,以便在下载时检查我的文件。如果文件已经存在,则跳过该文件,如果没有,则下载该文件

您不需要第二个循环

只需检查文件是否可读,如果不可读,请下载

#!/bin/bash

CHECKFILE="$1"

while read line; do

    if [ ! -r $line ]; then
        echo "File: $line not found"
        #change touch line to your wget
        #wget ....
        touch $line
    else
        echo "Found: $line"
    fi

done < "$CHECKFILE"

为什么不干脆
wget-N
?我对这个很陌生,正在寻找任何可能性。希望在我的bash中添加更多的东西,如果这行得通的话,并获得学习代码的经验。谢谢。
“$input”
文件中有什么?请张贴样本输入。你的剧本里发生了什么?你想要实现什么?为什么不直接使用
wget-N
[$line=$file]
只比较字符串,不检查文件是否存在。您想对脚本执行什么操作?
#!/bin/bash

CHECKFILE="$1"

while read line; do

    if [ ! -r $line ]; then
        echo "File: $line not found"
        #change touch line to your wget
        #wget ....
        touch $line
    else
        echo "Found: $line"
    fi

done < "$CHECKFILE"
$>ls -1
a.txt
b.txt
d.txt
files.txt
test.sh
$>cat files.txt
a.txt
b.txt
c.txt
d.txt
e.txt
A.txt
a.txt
$>test.sh files.txt
Found: a.txt
Found: b.txt
File: c.txt not found
Found: d.txt
File: e.txt not found
File: A.txt not found
Found: a.txt
$>ls -1tr
d.txt
b.txt
a.txt
files.txt
test.sh
c.txt
e.txt
A.txt
$>