【发布时间】:2016-05-27 04:03:25
【问题描述】:
如果我想用ffmpeg从一个网站下载一堆.ts文件,url格式是
http://example.com/video-1080Pxxxxx.ts
xxxxx 是一个从 00000 到 99999 的数字(需要零填充),我将如何在 bash 中遍历它,以便它尝试从 00000、00001、00002 等开始的每个整数?
【问题讨论】:
如果我想用ffmpeg从一个网站下载一堆.ts文件,url格式是
http://example.com/video-1080Pxxxxx.ts
xxxxx 是一个从 00000 到 99999 的数字(需要零填充),我将如何在 bash 中遍历它,以便它尝试从 00000、00001、00002 等开始的每个整数?
【问题讨论】:
我的 Bash (4.3) 可以做到这一点:
$ echo {001..010}
001 002 003 004 005 006 007 008 009 010
所以你可以这样做
for i in {00000..99999}; do
url="http://example.com/video-1080P${i}.ts"
# Use url
done
【讨论】:
为什么不用 for 循环做点什么:
for i in 0000{0..9} 000{10..99} 00{100..999} 0{1000..9999} {10000..99999}
do
# Curl was used since some minimal installs of linux do not have wget
curl -O http://example.com/video-1080P"$i".ts
sleep 1
done
(我确信有更好的方法可以做到这一点,但目前还没有呈现给我)
【讨论】:
在纯 bash 中:
$ n=99999 ; for ((i=0; i<=n; i++)) { s=$(printf "%05d" $i); echo $s ; }
或使用实用程序:
$ seq -w 0 99999
$ seq --help
Usage: seq [OPTION]... LAST
or: seq [OPTION]... FIRST LAST
or: seq [OPTION]... FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.
Mandatory arguments to long options are mandatory for short options too.
-f, --format=FORMAT use printf style floating-point FORMAT
-s, --separator=STRING use STRING to separate numbers (default: \n)
-w, --equal-width equalize width by padding with leading zeroes
【讨论】:
循环从0 到99999 的整数值,并使用printf 填充到5 位。
for x in {0..99999}; do
zx=$(printf '%05d' $x) # zero-pad to 5 digits
url="http://example.com/video-1080P${zx}.ts"
... # Do something with url
done
【讨论】: