开发者

Shell Printf: Invalid Number error

I have a file named exfile(4 numbe开发者_如何学JAVArs per line):

200807 0 96 200807

I want to read this file and use all the 4 numbers.

My Shell script is:

while read line
do
    echo ${line}
    set ${line}
    echo "${1} ${2} ${3} ${4}"
    declare -i start
    declare -i end
    start=`expr ${2} / 1`
    end=`expr ${3} / 1`
    for i in {${start}..${end}}
    do
        picnum=`printf "%03d" $i`
        echo ${picnum}
    done
done < exfile

The error is printf: {0..96}: invalid number

if you delete the lines:

declare -i start
declare -i end
start=`expr ${2} / 1`
end=`expr ${3} / 1`

The error is the same. I add these lines to turn strings into integers.

Any idea why? Thanks a lot.

=============================================

Updated:The following code works:

while read line
do
    echo ${line}
    set ${line}
    echo "${1} ${2} ${3} ${4}"
    for i in $(seq ${2} ${3})
    do
        picnum=`printf "%03d" $i`
        echo ${picnum}
    done
done < exfile


Brace expansion happens before parameter expansion. "{${start}..${end}}" is not a valid brace expansion expression and so is left unexpanded. Use seq instead.


Your for loop should be like this:

for((i=$2;i<=$3;i++))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜