r/linux_programming • • Oct 22 '20

Converting script to Posix compliant

Is there a way to make this section of a larger script Posix compliant?

options=$(lsblk | awk '/disk/ { print $1 }')
select opt in $options; do
  if [ -n "$opt" ]; then
    DEV_DISK_NAME="/dev/${opt}"
    DEV_PART_NAME="/dev/${opt}"
    break
  else  
    printf 'This option is invalid.\n\n'
  fi
done

4 Upvotes

6 comments sorted by

View all comments

1

u/balsoft Oct 22 '20

First iteration would be shellcheck -s sh

In - line 2:
select opt in $options; do
^----^ SC2039: In POSIX sh, select loops are undefined.


In - line 4:
    DEV_DISK_NAME="/dev/${opt}"
    ^-----------^ SC2034: DEV_DISK_NAME appears unused. Verify use (or export if used externally).


In - line 5:
    DEV_PART_NAME="/dev/${opt}"
    ^-----------^ SC2034: DEV_PART_NAME appears unused. Verify use (or export if used externally).

For more information:
  https://www.shellcheck.net/wiki/SC2034 -- DEV_DISK_NAME appears unused. Ver...
  https://www.shellcheck.net/wiki/SC2039 -- In POSIX sh, select loops are und...

So, replace that select loop somehow.

Also, AFAIR lsblk is not POSIX, so you probably need to replace it with something POSIX.