in reply to Read UUID on macOS from script
The "correct" answer to your question would be to use a module to run the external command, and as far as I understand ioreg has an -a switch to give its output in the more parseable XML format, and then use a module such as XML::LibXML to parse that.
But this might be one of those rare cases where a "quick and dirty" solution is enough. (Update: To clarify: using backticks is probably "ok" here if the command doesn't output anything on STDERR, you don't care about the command's exit value, and because the command below doesn't contain any shell metacharacters or interpolated variables. I explain this more in the link above.)
my ($UUID) = `ioreg -d2 -c IOPlatformExpertDevice` =~ /^.*\bIOPlatformUUID\b.*"([^"]+)"\s*$/m or die "failed to parse ioreg";
Note I can't fully test this because I don't have a Mac handy. Also, this will match the first of any lines with the string IOPlatformUUID, so you have to be sure of the command's output, and the above code is correspondingly brittle.
It is complaining with sh: -c: line 0: unexpected EOF while looking for matching `"'. Why?
The -F\" is being interpreted by Perl instead of the shell, and the shell is only seeing -F", which is an unmatched quote - this is one of the many pitfalls of backticks. Also, one shouldn't call sed or awk from Perl because native Perl code can replace these two commands and plenty others.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Read UUID on macOS from script
by stevieb (Canon) on Mar 30, 2022 at 21:01 UTC |