如何知道dd何时完成
How to know when dd has finished
我对 dd 命令有疑问。我用了一段时间是这样的:
while c=$(dd count=1000 bs=1 2>/dev/null) ;do
echo -n "$c" > /dev/ttyO5
done < $FILE
它工作起来很有魅力,唯一的问题是一旦文件被完全读取,它就不会停下来,而是停滞不前。一旦 dd 读取了所有文件,我怎样才能让 while 停止?
顺便说一句:请注意我的机器是嵌入式 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)
既然你有if
和of
,为什么不用它们呢,
test.sh:
dd if= 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~#
备注
- '\0'赋值给变量后会丢失
- '\\' 可能会根据 echo 的版本保留其特殊含义:尝试
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 <<<$'[=12=]')
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 "[=13=]"; else echo -n -E "$c"; fi
done | od -c
我对 dd 命令有疑问。我用了一段时间是这样的:
while c=$(dd count=1000 bs=1 2>/dev/null) ;do
echo -n "$c" > /dev/ttyO5
done < $FILE
它工作起来很有魅力,唯一的问题是一旦文件被完全读取,它就不会停下来,而是停滞不前。一旦 dd 读取了所有文件,我怎样才能让 while 停止?
顺便说一句:请注意我的机器是嵌入式 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)
既然你有if
和of
,为什么不用它们呢,
test.sh:
dd if= 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~#
备注
- '\0'赋值给变量后会丢失
- '\\' 可能会根据 echo 的版本保留其特殊含义:尝试
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 <<<$'[=12=]')
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 "[=13=]"; else echo -n -E "$c"; fi
done | od -c