Bash 脚本不会等到命令正确执行

发布于 2024-12-14 08:54:00 字数 768 浏览 0 评论 0原文

我正在编写一个非常简单的脚本,但由于某种原因,它的某些部分似乎是异步运行的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-12-21 08:54:00

(由于问题中的链接,我假设您正在使用 Linux)

我对 parted 不太熟悉,但我相信分区设备节点不是由它直接创建的 - 它们是由 udev 创建,本质上是一个异步过程:

  • parted 创建一个分区
  • 内核更新其内部状态
  • 内核通知udev 守护进程 (udevd)
  • udevd 检查其规则文件(通常位于 /etc/udev/ 下)并创建适当的设备节点

此过程允许将设备节点处理策略与内核明确分离,这是一件好事(TM)。不幸的是,它也会带来相对不可预测的延迟。

处理此问题的一种可能方法是让脚本等待设备节点出现:

while [ ! -e "/dev/sdd1" ]; do sleep 1; done

(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 by udev, which is by nature an asynchronous procedure:

  • parted creates a partition
  • the kernel updates its internal state
  • the kernel notifies the udev daemon (udevd)
  • udevd checks its rule files (usually under /etc/udev/) and creates the appropriate device nodes

This 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:

while [ ! -e "/dev/sdd1" ]; do sleep 1; done
与酒说心事 2024-12-21 08:54:00

假设您要做的就是确保在继续之前创建分区,有几种不同的方法

  1. 在移动到下一步之前检查进程parted是否已完成

  2. 在进入下一步之前检查设备是否已准备好(您将需要检查语法)。例如
    直到[ -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

  1. Check whether process parted has completed before moving to the next step

  2. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文