s-n-ushakov / rename-efi-entry

A Bash script to rename EFI boot entries
BSD 2-Clause "Simplified" License
84 stars 15 forks source link

Output of `sfdisk -d` in Ubuntu 20.04 does not end in a comma #2

Closed craigfrancis closed 4 years ago

s-n-ushakov commented 4 years ago

Hi Craig, and thank you for your patch. The only reason why I have not accepted it yet, is that I do not understand why/how it works for me without that comma on my Ubuntu 18.04... :) Just to have things clarified: could you share your output of sfdisk -d ... command? Or maybe just a single line for a relevant partition... Mine is the following: /dev/sda1 : start= 2048, size= 1048576, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=2FFCC127-F6CE-40F0-9932-D1DFD14E9462, name="EFI System Partition" And the only current guess of mine why your patch works for me, and why BASH_REMATCH[2] does not include the "name=..." trailer, is that it may be a result of a kind of side effect in the shell, when the space before "]]" in the condition is treated as a part of regular expression too. Any ideas of yours are most welcome... :)

s-n-ushakov commented 4 years ago

@craigfrancis ... or maybe I rather do not understand why it works for you, in particular why ([^,]+) does not get the "name=..." trailer included. Do you have that trailer?

craigfrancis commented 4 years ago

First, thanks for the script, it's been really useful.

As to my issue, I simply don't have the ", name="EFI System Partition" at the end (the UUID is the last thing).

I'm hoping that sfdisk will either finish the UUID with a comma, or end of line; but I suppose there could be other things?

So maybe something like uuid=([a-fA-F0-9-]+) would be better?

Here are some tests, the first one is the original, second is the one in this patch, and two other options:

^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([^,]+),
1) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'
2) -
3) -

^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([^,]+)
1) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'
2) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'
3) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462 made up example'

^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([^,[:blank:]]+)
1) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'
2) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'
3) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'

^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([a-fA-F0-9-]+)
1) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'
2) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'
3) '2FFCC127-F6CE-40F0-9932-D1DFD14E9462'

Test script...

declare -A lines;
lines[0]='/dev/sda1 : start= 2048, size= 1048576, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=2FFCC127-F6CE-40F0-9932-D1DFD14E9462, name="EFI System Partition"';
lines[1]='/dev/sda1 : start= 2048, size= 1048576, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=2FFCC127-F6CE-40F0-9932-D1DFD14E9462';
lines[2]='/dev/sda1 : start= 2048, size= 1048576, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=2FFCC127-F6CE-40F0-9932-D1DFD14E9462 made up example';

declare -A regexps;
regexps[0]='^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([^,]+),'; # Does not handle UUID being last.
regexps[1]='^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([^,]+)';
regexps[2]='^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([^,[:blank:]]+)';
regexps[4]='^([^[:blank:]]+)[[:blank:]]:[[:blank:]].*[[:blank:]]uuid=([a-fA-F0-9-]+)';

for regexp in "${regexps[@]}"; do
  echo;
  echo "$regexp"
  i=0;
  for line in "${lines[@]}"; do
    i=$((i+1));
    if [[ "${line}" =~ $regexp ]] ; then
      echo "$i) '${BASH_REMATCH[2]}'";
    else
      echo "$i) -";
    fi
  done
  echo;
done
s-n-ushakov commented 4 years ago

Well... what about [^,[:blank:]] ? It looks least restrictive... Do you agree? If yes, and if you agree to update your patch, I'm ok with merging it... :)

craigfrancis commented 4 years ago

Yep, done.

s-n-ushakov commented 4 years ago

Merged, thanks :)

craigfrancis commented 4 years ago

np, and thanks for writing this, made it much easier to re-name.