简体   繁体   中英

simple 'printf' in bash gets screwed up output

I'm a newbie here. I've really tried to google it but failed.

I've got a simple script that reads from file line by line and then prints it. "b.txt" has the first line of "02083192846".

#!/bin/bash
while IFS= read -r line; do
    printf 'downloading %s .html\n' $line
done < "$1"

however, the output is screwed up:

User@User-pk ~/test
$ ./test.sh b.txt
 .htmlading 02083192846

Once this is fixed, my next question is how to use this line as part of the file name that some command will store into. ie the filename should be "02083192846.html". I've tried setting using it as ${line}.html but it doesn't work. For example grep "foo" -f ${line}.html doesn't work. But curl http://foo -o $line.html does work still!

What I would do :

#!/bin/bash
while IFS= read -r line; do
    printf 'downloading %s .html\n' "${line//$'\r'/}"
    #                                      ________  
done < "$1"
    #                                          ^ 
    #                          remove \r with bash parameter expansion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM