Bash 脚本不会等到命令正确执行
我正在编写一个非常简单的脚本,但由于某种原因,它的某些部分似乎是异步运行的。
singlePartDevice() {
# http://www.linuxquestions.org/questions/linux-software-2/removing-all-partition-from-disk-690256/
# http://serverfault.com/questions/257356/mdadm-on-ubuntu-10-04-raid5-of-4-disks-one-disk-missing-after-reboot
# Create single partition
parted -s "$1" mklabel msdos
# Find size of disk
v_disk=$(parted -s "$1" print|awk '/^Disk/ {print $3}'|sed 's/[Mm][Bb]//')
parted -s "$1" mkpart primary ext3 4096 ${v_disk}
parted -s "$1" set 1 raid on
return 0
}
singlePartDevice "/dev/sdc"
singlePartDevice "/dev/sdd"
#/dev/sdc1 exists but /dev/sdd1 does not exist
sleep 5s
#/dev/sdc1 exists AND /dev/sdd1 does also exist
正如您在调用 sleep 之前看到的,脚本只完成了部分工作。如何让我的脚本等到parted 成功完成其工作?
I am working on a very simple script but for some reason parts of it seem to run asynchronously.
singlePartDevice() {
# http://www.linuxquestions.org/questions/linux-software-2/removing-all-partition-from-disk-690256/
# http://serverfault.com/questions/257356/mdadm-on-ubuntu-10-04-raid5-of-4-disks-one-disk-missing-after-reboot
# Create single partition
parted -s "$1" mklabel msdos
# Find size of disk
v_disk=$(parted -s "$1" print|awk '/^Disk/ {print $3}'|sed 's/[Mm][Bb]//')
parted -s "$1" mkpart primary ext3 4096 ${v_disk}
parted -s "$1" set 1 raid on
return 0
}
singlePartDevice "/dev/sdc"
singlePartDevice "/dev/sdd"
#/dev/sdc1 exists but /dev/sdd1 does not exist
sleep 5s
#/dev/sdc1 exists AND /dev/sdd1 does also exist
As you see before the call of sleep the script has only partially finished its job. How do I make my script to wait until parted has done its job sucessfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(由于问题中的链接,我假设您正在使用 Linux)
我对
parted
不太熟悉,但我相信分区设备节点不是由它直接创建的 - 它们是由udev
创建,本质上是一个异步过程:parted
创建一个分区udev
守护进程 (udevd
)udevd
检查其规则文件(通常位于/etc/udev/
下)并创建适当的设备节点此过程允许将设备节点处理策略与内核明确分离,这是一件好事(TM)。不幸的是,它也会带来相对不可预测的延迟。
处理此问题的一种可能方法是让脚本等待设备节点出现:
(I am assuming that you are working on Linux due to the links in your question)
I am not very familiar with
parted
, but I believe that the partition device nodes are not created directly by it - they are created byudev
, which is by nature an asynchronous procedure:parted
creates a partitionudev
daemon (udevd
)udevd
checks its rule files (usually under/etc/udev/
) and creates the appropriate device nodesThis procedure allows for clear separation of the device node handling policy from the kernel, which is a Good Thing (TM). Unfortunately, it also introduces relatively unpredictable delays.
A possible way to handle this is to have your script wait for the device nodes to appear:
假设您要做的就是确保在继续之前创建分区,有几种不同的方法
在移动到下一步之前检查进程parted是否已完成
在进入下一步之前检查设备是否已准备好(您将需要检查语法)。例如
直到[ -f /dev/sdc && -f /dev/sdd ]
sleep 5
Assuming all you want to do is ensure that the partitions are created before proceeding, there are a couple of different approaches
Check whether process parted has completed before moving to the next step
Check if the devices are ready before moving to the next step (you will need to check the syntax). Eg
until [ -f /dev/sdc && -f /dev/sdd ]
sleep 5