我在linux中编写了以下脚本以获取USB详细信息:当附加USB时,此脚本从udev执行。
fgrep -A 15 'New USB device found' /var/log/messages | tail -n 16 > usb_detail
SERIAL=$(sed -n '5s/A.*: //p' usb_detail)
SIZE=$(sed -n '10s/A.*: //p' usb_detail)
MOUNT=$(sed -n '14s/A.*: //p' usb_detail)
blkid > Label
LABEL=$(grep $MOUNT Label | awk '{print $2}')
现在的问题是我得到了所有的值(序列号,大小,挂载),但是当它从BLKID检查usb的标签时,我什么也得不到。这个命令在输出中没有给我任何东西:
blkid > Label
此命令在命令提示符下正常运行。但是当我在这个脚本中运行它(当连接usb时从UDEV调用它)时它不会给出任何输出。
那我怎么能弄清楚什么是主要问题以及为什么我在LABEL文件中没有得到任何东西?
这是我的/ var / log / messages:
Apr 28 08:25:58 box2 kernel: usb 1-3: New USB device found, idVendor=0411, idProduct=0105
Apr 28 08:25:58 box2 kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=5
Apr 28 08:25:58 box2 kernel: usb 1-3: Product: USB-SATA Bridge
Apr 28 08:25:58 box2 kernel: usb 1-3: Manufacturer: BUFFALO
Apr 28 08:25:58 box2 kernel: usb 1-3: SerialNumber: 00001412AA38
Apr 28 08:25:58 box2 kernel: usb 1-3: configuration #1 chosen from 1 choice
Apr 28 08:25:58 box2 kernel: scsi25 : SCSI emulation for USB Mass Storage devices
Apr 28 08:26:05 box2 kernel: scsi 25:0:0:0: Direct-Access BUFFALO External HDD PQ: 0 ANSI: 2 CCS
Apr 28 08:26:05 box2 kernel: sd 25:0:0:0: Attached scsi generic sg0 type 0
Apr 28 08:26:05 box2 kernel: sd 25:0:0:0: [sda] 976773168 512-byte logical blocks: (500 GB/465 GiB)
Apr 28 08:26:05 box2 kernel: sd 25:0:0:0: [sda] Write Protect is off
Apr 28 08:26:05 box2 kernel: sd 25:0:0:0: [sda] Assuming drive cache: write through
Apr 28 08:26:05 box2 kernel: sd 25:0:0:0: [sda] Assuming drive cache: write through
Apr 28 08:26:05 box2 kernel: sda: sda1
Apr 28 08:26:05 box2 kernel: sd 25:0:0:0: [sda] Assuming drive cache: write through
Apr 28 08:26:05 box2 kernel: sd 25:0:0:0: [sda] Attached SCSI disk
这是命令提示符下的blkid输出:
[root@box2 usbmon]# blkid
/dev/sdf1: UUID="2d1bd58e-86ea-4ab2-99e3-ec2d1fb24021" TYPE="ext4"
/dev/sdf2: UUID="Je95wE-NW7f-Mqlv-UBgi-qNOL-8i35-8cUUTj" TYPE="LVM2_member"
/dev/mapper/vg_box2-lv_root: UUID="27cf24f7-4e7d-42bd-97be-09ddc6eb614b" TYPE="ext4"
/dev/mapper/vg_box2-lv_swap: UUID="36c54ea3-f9f3-455a-9c84-58e7317d1e60" TYPE="swap"
/dev/mapper/vg_box2-lv_home: UUID="5cac0192-2757-4cf6-b70f-d9ff8fbe6f36" TYPE="ext4"
/dev/sda1: LABEL="BU5" UUID="242DB177461FD69A" TYPE="ntfs"
答案 0 :(得分:2)
使用sudo运行会让你正确。
sudo blkid
答案 1 :(得分:0)
我通过更改语法
解决了这个问题blkid > Label
到
/sbin/blkid > Label
不知道为什么,但有时它有效。