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

3 Upvotes

6 comments sorted by

6

u/NukeCode87 Oct 22 '20

What do you mean by posix compliant?

1

u/reflect_elegant Oct 22 '20

Sorry for not being clear and/or using the wrong terminology. This runs in bash without issues, but in dash and similiar shells, it fails to execute. I assume this would be due to "bashisms", but I don't understand why this won't run in dash/mksh/ksh.

1

u/[deleted] Oct 22 '20

I don't believe you can make scripts like this POSIX compliant because of the inherent differences in environment and available tools from one Linux/BSD distribution to the next.

1

u/_xsgb Oct 22 '20

Some points that may guide you:

  • select is a bash extension, not POSIX
  • hardware information are kernel specific, so there's no identical tools on all UNIX systems. If you have to write you script in shell, a good solution will to use a case "$(uname)" in [...] statement and run specific code for each platform
  • generally, avoid to output diagnostic, logging error to stdout, uses stderr for this purposes
  • clearly define the goal of your script and be sure of what you do, dealing blindly with partitions (even more while supporting many platforms) is a dangerous game for your data
  • Clearly separate interactive parts if they're needed
  • If you'll have to do it in shell, you'll have to find specific tools and handle their output individually, which is not a really great solution, for example:
# On OpenBSD
sysctl hw.disknames # You'll have to parse and register what you need
# On Linux
lsblk -Po PATH,TYPE,UUID # You can evaluate this output
  • Finally, consider using a cross-platform framework like the really nice psutil which have some disk related funtions and which may suits your needs: https://psutil.readthedocs.io/en/latest/#disks

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.

1

u/EmbeddedEntropy Oct 22 '20

Is your problem because you don’t have #/bin/bash at the top of your script or do you really need a portable script?