我对dd命令有疑问。我像这样使用它了一段时间:
while c=$(dd count=1000 bs=1 2>/dev/null) ;do
echo -n "$c" > /dev/ttyO5
done < $FILE
它就像魅力一样,唯一的问题是,一旦文件被完全读取,它就不会停止,而它会停留在那里。一旦dd读完所有文件,我怎么能停下来停止?
BTW:注意我的机器嵌入了linux,它没有像普通的ubuntu机器那样拥有所有的命令和工具。由于
输出dd --version
BusyBox v1.20.2 (MUSE) multi-call binary.
Usage: dd [if=FILE] [of=FILE] [bs=N] [count=N] [skip=N]
[seek=N]
Copy a file with converting and formatting
if=FILE Read from FILE instead of stdin
of=FILE Write to FILE instead of stdout
bs=N Read and write N bytes at a time
count=N Copy only N input blocks
skip=N Skip N input blocks
seek=N Skip N output blocks
Numbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),
MD (x1000000), M (x1048576), GD (x1000000000) or G (x1073741824)
答案 0 :(得分:0)
由于您有if
和of
,为什么不使用它们,
test.sh:
dd if=$1 of=/dev/ttyO5 count=1000 bs=1 2>/dev/null
输出:
root@localhost~# ./test.sh file.txt
The quick brown fox jumps over the bashy dog.
root@localhost~#
答案 1 :(得分:0)
请注意
echo '\n'
可以从stderr
中读取状态dd count=1000 bs=1 </dev/null
0+0 records in
0+0 records out
所以
while
c=$(dd ... 2>errorfile);
read line <errorfile; # read the first line of error
[[ $line != 0+0* ]]; # break while if line begins with 0+0
do
...
done
与检查c
的长度略有不同c=$(dd count=1 bs=1 </dev/null)
echo "${#c}"
c=$(dd count=1 bs=1 <<<$'\0')
echo "${#c}"
因为在最后一种情况下读取了一个NUL字符,而在之前没有读过任何字符。
编辑:以下演示它有效但速度很慢:
for x in {{0..9},{a..f}}{{0..9},{a..f}}; do printf "\x$x"; done |
while
c=$(dd count=1 bs=1 2>errfile;echo .); c=${c%.}
read line <errfile
[[ $line != 0+0* ]]
do
if [[ $c = '' ]]; then printf "\0"; else echo -n -E "$c"; fi
done | od -c