2017-09-29 43 views
0

似乎每当我在shell上测试它的工作,但是当我从脚本运行它时,它不会。不能在curl url中插入变量

壳牌:

fips=55555 
echo https://www2.census.gov/geo/tiger/TIGER2016/ROADS/tl_2016_$fips'_'roads.zip 
>>https://www2.census.gov/geo/tiger/TIGER2016/ROADS/tl_2016_55555_roads.zip 

我通过一些数据试图循环在文本文件中是这样的:

enter image description here

并解析它/输入到卷曲网址:

但是输出的字符串的开头像这样被覆盖:

./readFipsData.txt 
>>_roads.zipw2.census.gov/geo/tiger/TIGER2016/ROADS/tl_2016_48001 

注意:如果这有什么区别,我使用Git Bash for Windows。

cat ./testFipsData.txt | 
while read line 
do 
    counter=0 
    for col in $line 
    do 
    if [ $counter == 0 ] 
    then 
     state=$col 
    elif [ $counter == 1 ] 
    then 
     county=$col 
    elif [ $counter == 2 ] 
    then 
     fips=$col 
    fi 
     ((counter++)) 
    done 
#curl -k -o ./$county.zip "https://www2.census.gov/geo/tiger/TIGER2016/ROADS/tl_2016_$fips_roads.zip" 
echo https://www2.census.gov/geo/tiger/TIGER2016/ROADS/tl_2016_$fips'_'roads.zip 
done 
+1

输入文件可能包含MSWin行尾。从输入中删除'$'\ r''。 – choroba

+2

不要使用管道,使用输入重定向'done Barmar

+0

@choroba不,问题在于他将变量设置在子shell中,因为管道。 – Barmar

回答

0

感谢您的所有建议这里是什么工作:

cat ./testFipsData.txt | 
while read state county fips 
do 
fips=${fips%$'\r'} 
curl -k -o ./$county.zip https://www2.census.gov/geo/tiger/TIGER2016/ROADS/tl_2016_$fips'_'roads.zip 
done