bash OSX 上 udev 功能的替代方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2600514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:55:26  来源:igfitidea点击:

Alternative to udev functionality on OSX

bashmacosudev

提问by S1syphus

I'm trying to create a custom file/check in check out script for external hardrives, however part of the script is from a Linux machine, which I have tested works fine, but uses udevinfo, OS X doesn't have udev, so is there anything that offers the same functionality?

我正在尝试为外部硬盘创建自定义文件/签入签出脚本,但是脚本的一部分来自 Linux 机器,我测试过它工作正常,但使用udevinfo,OS X 没有 udev,所以有什么可以提供相同功能的吗?

#!/bin/bash
declare -a EXTERNAL_DISKS
declare -a INTERNAL_DISKS

for disk in /dev/[sh]d[a-z]; do
     eval `udevinfo -q env -n $disk`
     [ "$ID_BUS" = "usb" ] && EXTERNAL_DISKS=( ${EXTERNAL_DISKS[@]} $disk )
     [ "$ID_BUS" = "scsi" ] && INTERNAL_DISKS=( ${INTERNAL_DISKS[@]} $disk )
 done

 echo "Internal disks: ${INTERNAL_DISKS[@]}"
 echo "External disks: ${EXTERNAL_DISKS[@]}"

Anybody know any alternatives? Or a way this could be accomplished on OSX using bash?

有人知道任何替代方案吗?或者可以使用 bash 在 OSX 上完成的方法?

回答by Ned Deily

#!/usr/bin/env python
from plistlib import readPlistFromString as rPFS
from subprocess import *

def shell(cmd):
    return Popen(cmd.split(), stdout=PIPE).communicate()[0]

disks = {False: [], True: []}   
for disk in rPFS(shell('diskutil list -plist'))['WholeDisks']:
    disks[rPFS(shell('diskutil info -plist ' + disk))['Internal']].append(disk)

print "Internal disks: " + ' '.join(disks[True])    
print "External disks: " + ' '.join(disks[False])    

回答by Azeem.Butt

IOKit manages devices, DiskArbitration manages mass storage devices on top of that. Neither has much in the way of a scripting interface.

IOKit 管理设备,DiskArbitration 在此之上管理大容量存储设备。两者都没有太多的脚本界面。