Python代码
1. #!/bin/sh
2. #randRead.sh
3. echo 1234567890 > sFile
4. exec 3<> sFile
5. #read -n 4 <&3
6. dd bs=1 count=4 of=/dev/null <&3 2>/dev/null
7. echo . | dd bs=1 count=1 >&3 2>/dev/null
8. exec 3>&-
9. cat sFile
#!/bin/sh
#randRead.sh
echo 1234567890 > sFile
exec 3<> sFile
#read -n 4 <&3
dd bs=1 count=4 of=/dev/null <&3 2>/dev/null
echo . | dd bs=1 count=1 >&3 2>/dev/null
exec 3>&-
cat sFile
运行结果如下:
Python代码
1. root@SMP2> cat -n randRead.sh
2. 1 #!/bin/sh
3. 2 echo 1234567890 > sFile
4. 3 exec 3<> sFile
5. 4 #read -n 4 <&3
6. 5 dd bs=1 count=4 of=/dev/null <&3 2>/dev/null
7. 6 echo . | dd bs=1 count=1 >&3 2>/dev/null
8. 7 exec 3>&-
9. 8 cat sFile
10. root@SMP2>
11. root@SMP2> ./randRead.sh
12. 1234.67890
13. root@SMP2>
root@SMP2> cat -n randRead.sh
1 #!/bin/sh
2 echo 1234567890 > sFile
3 exec 3<> sFile
4 #read -n 4 <&3
5 dd bs=1 count=4 of=/dev/null <&3 2>/dev/null
6 echo . | dd bs=1 count=1 >&3 2>/dev/null
7 exec 3>&-
8 cat sFile
root@SMP2>
root@SMP2> ./randRead.sh
1234.67890
root@SMP2>
附ABS上的例子:
Python代码
1. 87 echo 1234567890 > File # 写字符串到"File".
2. 88 exec 3<> File # 打开"File"并且给它分配fd 3.
3. 89 read -n 4 <&3 # 只读4个字符.
4. 90 echo -n . >&3 # 写一个小数点.
5. 91 exec 3>&- # 关闭fd 3.
6. 92 cat File # ==> 1234.67890
7. 93 # 随机存储.
本文转自glying 51CTO博客,原文链接:http://blog.51cto.com/liying/967765,如需转载请自行联系原作者