Reputation: 107
Can someone please take the following statements:
kernelHash=$(grep -m 1 -o '"uImage-[0123456789.]*-ts-armv7l": [^, }]*' $SecureBootJson | sed 's/^.*: //')
kernelHash=$(grep -oP '"uImage-[0123456789.]*-ts-armv7l.*?\btype\b"' $SecureBootJson | cut -f3 -d ' ' | sed 's/,//')
and break them down for me and explain what they are supposed to accomplish?
Upvotes: 0
Views: 70
Reputation: 4089
Both are setting a variable kernelHash
based on the value of the file whose name/path is stored in the variable $SecureBootJson
. I will assume you understand what the pipes |
are doing.
man grep
helps us parse the arguments to grep:
-m 1
: Stop reading a file after 1 matching line-o
: Print only the matched (non-empty) parts of a matching line-P
: Interpret the regex as a Perl regex'"uImage-[0123456789.]*-ts-armv7l": [^, }]*'
Matches a "key": value
pair
"uImage-
: literally[0123456789.]*
: zero or more digits or dots-ts-armv7l":
: literally[^, }]*
: zero or more characters that are not ,
,
or }
and
'"uImage-[0123456789.]*-ts-armv7l.*?\btype\b"'
Matches a "quoted" string
"uImage-
: literally[0123456789.]*
: zero or more digits or dots-ts-armv7l
: literally.*?
: lazily matches zero or more non-newline characters\btype\b"
: Matches the word 'type' using word boundariescut -f3 -d ' '
From man cut
:
-f3
: return the 3rd field-d ' '
: split on single spacesSo this cut command splits the string on spaces and returns the 3rd section.
sed 's/^.*: //'
Replaces all non-newline characters between the beginning of the line the rightmost ': ' with nothing. Assuming the grep
left us with a "key": value
line where value
does not contain ': '(which it cannot thanks to grep), we are left with value
.
sed 's/,//'
Replaces the first ,
found in the string with nothing.
There are a number of quirks and opportunities for improvement in these commands, but I have not included them in the main body of the answer, because the question was "what does this do?". My foremost suggestion would be the use of jq
to parse Json.
Upvotes: 3