【问题标题】:How to create a loop before downloading a file checking your current file?如何在下载文件检查当前文件之前创建循环?
【发布时间】:2019-12-24 03:27:18
【问题描述】:

我就是这样开始的。很简单的事情。

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"

我也试过这个。我知道这是错误的,因为我没有任何东西可以让 y 匹配 x。我试图在 wget 中使用 y 作为变量来匹配我的文件 x 中的某些内容。

for x in $(cat file); do
for y in ???; do

if [ "$x" == "$y" ]; then
     echo "${x##*/} & ${y##*/} match skipping download"
else
     echo "${x##*/} don't match ${y##*/} downloading"
  sleep 1
     wget -q -nc --show-progress "$y"
fi
done
done

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

【问题讨论】:

  • 为什么不只是wget -N
  • 我对此很陌生,正在寻找任何可能性。如果这可行并获得学习编码的经验,希望为我的 bash 添加更多内容。谢谢。
  • "$input" 文件中有什么内容?请发布示例输入。您的脚本中发生了什么?你想达到什么目标?为什么不直接使用 wget -N 呢? [ $line = $file ] 只是比较字符串,它不检查文件是否存在。你想用你的脚本做什么?

标签: bash loops if-statement


【解决方案1】:

你不需要第二个循环

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

#!/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"

记住 linux 是区分大小写的。 当然,你可以在一行中有多个数据并将其拆分以获得文件名和路径。

脚本的结果将是。

$>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
$>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 2013-09-13
    • 2018-06-21
    • 2012-06-10
    • 2016-04-15
    • 1970-01-01
    相关资源
    最近更新 更多