开发者

bash echo problem

Here's a bash script that extracts some data from a html file.

price=`grep '        <td>\$' $1 | sed -e 's/<td>//g' -e 's:</td>::g' -e 's/\$ //g' -e 's/^  *//g'`
grep '        <td>\$' $1 | sed -e 's/<td>//g' -e 's:</td>::g' -e 's/\$ //g' -e 's/^  *//g'

echo "Price: $price"

The sed part could use some help, but that's not the issue here. The problem is that, when I run the script, it should print the found value twice, right? But it prints it only once, the first time (Without开发者_StackOverflow the 'Price:'). What's the problem here?


The problem is that the string you're returning has a \r in it, which returns the cursor to the first column before printing stuff out. Use od -c to verify. And use a proper tool such as xmlstarlet to make sure this doesn't happen.


The first grep reads everything on standard input. Then, the second grep blocks trying to read from stdin.


I'm guessing that unlike the code shown, the assignment actually happens in a subshell and therefore is not visible (lost on exit of subshell)

I'm afraid you ran into a subshell issue that you donot show here. Post more code that you actually use if you can.

--- Sample:

 unset price
 price=1
 echo $price   # works

 unset price
 echo -n 1 | price=$(cat)
 echo $price   # works _not_


A couple of comments about your use of sed:

-e 's/^ *//g' -- you don't need the "g": your pattern is anchored at the beginning so it can only match once. Might as well look for tabs too: -e 's/^[[:space:]]\{1,\}//'

-e 's/<td>//g' -e 's:</td>::g' can be collapsed into -e 's|</\{0,1\}td>||g'

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜