如何在bash-in循环中使用dd命令
我对bash不是很精通,但我知道一些命令,并且可以四处走动。我在编写脚本填充运行Ubuntu(嵌入式Linux)的外部设备上的闪存驱动器时遇到问题 我想知道闪存驱动器何时充满(停止向其中写入随机数据),以便继续执行其他操作 在Python中,我将执行以下操作:如何在bash-in循环中使用dd命令,bash,Bash,我对bash不是很精通,但我知道一些命令,并且可以四处走动。我在编写脚本填充运行Ubuntu(嵌入式Linux)的外部设备上的闪存驱动器时遇到问题 我想知道闪存驱动器何时充满(停止向其中写入随机数据),以便继续执行其他操作 在Python中,我将执行以下操作: while ...condition: if ....condition: print "Writing data to NAND flash failed ...." break else:
while ...condition:
if ....condition:
print "Writing data to NAND flash failed ...."
break
else:
continue
但我不知道如何在bash中做到这一点。提前感谢您的帮助 根据
手册dd
:
DIAGNOSTICS
The dd utility exits 0 on success, and >0 if an error occurs.
这就是您在脚本中应该做的,只需在dd命令后检查返回值:
dd if=/dev/urandom of=/storage/testfile.txt
ret=$?
if [ $ret gt 0 ]; then
echo "Writing data to NAND flash failed ...."
fi
试试这个
#!/bin/bash
filler="$1" #save the filename
path="$filler"
#find an existing path component
while [ ! -e "$path" ]
do
path=$(dirname "$path")
done
#stop if the file points to any symlink (e.g. don't fill your main HDD)
if [ -L "$path" ]
then
echo "Your output file ($path) is an symlink - exiting..."
exit 1
fi
# use "portable" (df -P) - to get all informations about the device
read s512 used avail capa mounted <<< $(df -P "$path" | awk '{if(NR==2){ print $2, $3, $4, $5, $6}}')
#fill the all available space
dd if=/dev/urandom of="$filler" bs=512 count=$avail 2>/dev/null
case "$?" in
0) echo "The storage mounted to $mounted is full now" ;;
*) echo "dd errror" ;;
esac
ls -l "$filler"
df -P "$mounted"
代码是在的帮助下完成的,上面的
dd
将运行,直到驱动器被填满,并且每次都将退出>0
。因此,不需要进行任何测试-当脚本在dd之后继续时-驱动器已满..;)@jm666-除非dd
因其他错误而提前退出。我认为要正确执行此操作,您需要知道闪存驱动器可以容纳多少字节,并明确告诉dd
写入那么多字节。
#!/bin/bash
filler="$1" #save the filename
path="$filler"
#find an existing path component
while [ ! -e "$path" ]
do
path=$(dirname "$path")
done
#stop if the file points to any symlink (e.g. don't fill your main HDD)
if [ -L "$path" ]
then
echo "Your output file ($path) is an symlink - exiting..."
exit 1
fi
# use "portable" (df -P) - to get all informations about the device
read s512 used avail capa mounted <<< $(df -P "$path" | awk '{if(NR==2){ print $2, $3, $4, $5, $6}}')
#fill the all available space
dd if=/dev/urandom of="$filler" bs=512 count=$avail 2>/dev/null
case "$?" in
0) echo "The storage mounted to $mounted is full now" ;;
*) echo "dd errror" ;;
esac
ls -l "$filler"
df -P "$mounted"
bash ddd.sh /path/to/the/filler/filename