简体   繁体   English

在awreg中使用ioreg -l在Mac上显示序列号的最后6个字符

[英]Using ioreg -l with awk to display the last 6 characters of the serialnumber on a mac

I wish to use a script to grab a certain part of the serial number on a macbook air and use it in combination with another variable to create a Computer name based on the last 6 characters of the computers serial number. 我希望使用脚本在Macbook air上获取序列号的特定部分,并将其与另一个变量结合使用,以基于计算机序列号的后6个字符创建计算机名称。

system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print substr( $0, length($0) - 5 )}'

This code did just that but it doesn't work in single user mode. 这段代码只是这样做,但在单用户模式下不起作用。 system_profiler returns a x86PlatformPlugin error and so i tried: system_profiler返回x86PlatformPlugin错误 ,所以我尝试了:

ioreg -l | grep IOPlatformSerialNumber | awk '{print substr( $0, length($0) - 6 )}'

with good result except for the serial ending in a double quote ". 除序列号以“双引号”结尾外,效果良好。

How can i modify the output to get rid of that last character and leave me with the last 6 characters of the Serial Number? 我如何修改输出以除去最后一个字符,而让我留下序列号的最后6个字符?

You're very close; 你很亲近 just tell awk to take the string of the right length: 只要告诉awk取正确长度的字符串即可:

ioreg -l | grep IOPlatformSerialNumber | awk '{print substr( $0, length($0) - 6, 6 )}'

From the documentation: 从文档中:

substr(string, start, length) substr(字符串,起始,长度)

This returns a length-character-long substring of string, starting at character number start. 这将返回从字符编号start开始的字符串的长字符长子字符串。 The first character of a string is character number one. 字符串的第一个字符是字符号一。

For example, substr("washington", 5, 3) returns "ing". 例如,substr(“ washington”,5,3)返回“ ing”。 If length is not present, this function returns the whole suffix of string that begins at character number start. 如果不存在长度,则此函数返回以字符号开始的整个字符串的后缀。 For example, substr("washington", 5) returns "ington". 例如,substr(“ washington”,5)返回“ ington”。 This is also the case if length is greater than the number of characters remaining in the string, counting from character number start. 从字符数开始算起,如果长度大于字符串中剩余的字符数,情况也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM