通过URL迭代

时间:2016-02-16 03:14:13

标签: bash loops ffmpeg

如果我想使用ffmpeg从网站下载一堆.ts文件,而网址格式为

library(ggplot2); library(grid); library(gridBase)

pdf("test.pdf")

grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, ncol=2,
    widths = grid::unit(c(.6, .4), "npc"))))

#Draw base plot
grid::pushViewport(grid::viewport(layout.pos.col = 1, width = grid::unit(.8, "npc")))

graphics::plot.new()
graphics::par(fig = gridBase::gridFIG(), mar=c(1, 1, 1, 1), new = TRUE)


plot(1:10)

grid::popViewport()

#Draw ggplot
grid::pushViewport(grid::viewport(layout.pos.col = 2, width = grid::unit(.2, "npc")))
print( ggplot(mtcars, aes(mpg, hp)) + geom_point(), newpage = FALSE)
grid::popViewport()

dev.off()

如果http://example.com/video-1080Pxxxxx.ts 是一个从00000到99999的数字(需要零填充),我将如何在bash中迭代它,以便它尝试从00000,00001,00002等开始的每个整数。 p>

4 个答案:

答案 0 :(得分:2)

循环遍历从099999的整数值,并使用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

答案 1 :(得分:0)

纯粹的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

答案 2 :(得分:0)

为什么不用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

(我确信有更好的方法可以做到这一点,但目前还没有向我展示)

答案 3 :(得分:0)

My 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