我有一个简单的脚本,它可以从文件中获取一些值。我正在使用 sed 来获取这些值(语法如下)。这些命令直到昨天都运行良好。但现在当我运行这些命令时,我没有得到任何值。我没有改变任何东西,所以我很惊讶是什么原因。有人能告诉我如何调试我的问题吗?以下是文件文本:
May 1 11:59:31 box2 kernel: usb 1-3: new high speed USB device using ehci_hcd and address 24
May 1 11:59:31 box2 kernel: usb 1-3: New USB device found, idVendor=0411, idProduct=0105
May 1 11:59:31 box2 kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=5
May 1 11:59:31 box2 kernel: usb 1-3: Product: USB-SATA Bridge
May 1 11:59:31 box2 kernel: usb 1-3: Manufacturer: BUFFALO
May 1 11:59:31 box2 kernel: usb 1-3: SerialNumber: 00001412AA38
May 1 11:59:31 box2 kernel: usb 1-3: configuration #1 chosen from 1 choice
May 1 11:59:31 box2 kernel: scsi27 : SCSI emulation for USB Mass Storage devices
May 1 11:59:38 box2 kernel: scsi 27:0:0:0: Direct-Access BUFFALO External HDD PQ: 0 ANSI: 2 CCS
May 1 11:59:38 box2 kernel: sd 27:0:0:0: Attached scsi generic sg6 type 0
May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] 976773168 512-byte logical blocks: (500 GB/465 GiB)
May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Write Protect is off
May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Assuming drive cache: write through
May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Assuming drive cache: write through
May 1 11:59:38 box2 kernel: sdf: sdf1
May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Assuming drive cache: write through
May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Attached SCSI disk
脚本是:
SERIAL=$(sed -n '5s/A.*: //p' filename)
SIZE=$(sed -n '10s/A.*: //p' filename)
MOUNT=$(sed -n '14s/A.*: //p' filename)
答案1
我只能假设你在四月份匹配大写字母 A,但现在是五月份了,所以它不再起作用。
也许您应该使用“^”而不是“A”。
答案2
正如@wfaulk 所说的,如果提供的文本是您使用的,那么数字应该是 6,11,15。
SERIAL=$(sed -n 's/.*SerialNumber: \(.*\)/\1/p' filename)
SIZE=$(sed -n 's/.*logical blocks: \(.*\)/\1/p' filename)
可能更可靠。不过,根据给出的数据,我找不到更好的方法来获取挂载点。
来自评论
MOUNT=$(sed -n 's/.* sd[a-z]: \(.*\)/\1/p' filenaem)
答案3
由于日志轮换,日志文件中的内容可能会消失。上次我不得不这样做时,我发现 lshw 实用程序非常有用。例如,lshw -class disk -quiet
将满足您的需求。lshw 在 ubuntu 上默认可用,也可用于 centos/redhat(http://www.ducea.com/2006/06/03/install-lshw-on-rhel-fedora-centos/)。
我知道这与主题无关,但我希望它对原帖者有用。