如何在此内部执行命令?
#!/bin/bash
for i in {8..12}
do
printf "curl "http://myurl?hour=20140131%02s" -o file%02s.txtn" "$i" "$i"
done
我的脚本的当前版本打印出以下几行:
curl "http://myurl?hour=2014013108" -o file08.txt
curl "http://myurl?hour=2014013109" -o file09.txt
curl "http://myurl?hour=2014013110" -o file10.txt
curl "http://myurl?hour=2014013111" -o file11.txt
curl "http://myurl?hour=2014013112" -o file12.txt
我想更改脚本,以便执行每行并在每个卷曲请求下保存一个文件。
无需使用eval,您可以使其简单:
for i in {8..12}; do
printf -v n %02d $i
curl -s "http://myurl/?hour=20140131${n}" -o "file${n}.txt"
done
如果您使用的是bash
4,则不需要printf
:
for i in {08..12}
do
curl "http://myurl?hour=20140131$i" -o "file$i.txt"
done
尝试评估生成的命令:
#!/bin/bash
for i in {8..12}
do
eval curl $(printf "'http://myurl?hour=20140131%.2i' -o file%.2i.txt" "$i" "$i")
done
您也可以使用简化的代码:
for i in $(seq -f "%02g" 08 12)
do
curl "http://myurl?hour=20140131${i}" -o file${i}.txt
done
只需在没有printf
的情况下添加另一行#!/bin/bash
for i in {8..12}
do
printf "curl "http://myurl?hour=20140131%02s" -o file%02s.txtn"
x=$(printf "%02d" "$i")
curl "http://myurl?hour=20140131$x" -o file$x.txt
done