OT: posix sh problem

Teske, Devin Devin.Teske at fisglobal.com
Thu Apr 4 13:54:39 UTC 2013


On Apr 4, 2013, at 5:32 AM, Mark Felder wrote:

> Hi all,
> 
> Hopefully someone here is much more clever than I am. I've run out of ideas on how to cleanly convert this chunk of ksh to posix sh.

/me takes the challenge (and shame on some of the current responses; this is trivial in sh and there's actually nothing wrong with the OPs code -- it works)


> This is from a BB/Hobbit/Xymon monitoring script for ZFS. I'd really like to have this working cleanly on FreeBSD without requiring any funky shells or using any temporary files.
> 

Cool! After I help you fix whatever the issue is, I'd be interested in this a little more. ZFS monitoring would be nice.


> The following is supposed to be able to loop through the output of multiple zpools reading one line at a time and each line item is set as a variable:
> 
> 
> /sbin/zpool list -H | while read name size used avail cap dedup health altroot
> do
>  # do interesting things here
> done
> 
> Unfortunately you can't pipe through read in posix sh.

Wait, you can't? Then I've been doing something wrong all these years…

#!/bin/sh
printf "line1\nline2\n" | while read line
do
	echo "line=[$line]"
done

===

dteske at scribe9.vicor.com ~ $ sh bar
line=[line1]
line=[line2]

===

Just a side note, on my "zpool list -H" on my 8.1-R system doesn't provide the "dedup" column, so your mileage may vary (you may have to adjust the script to account for that on systems like mine).

Aside from that, I took your script as-is, copy/paste and it worked fine on 8.1-RELEASE-p6:

dteske at oos0a.lbxrich.vicor.com ~ $ cat bar
#!/bin/sh
/sbin/zpool list -H | while read name size used avail cap dedup health altroot
do
 echo $name
done
dteske at oos0a.lbxrich.vicor.com ~ $ sh bar
NEC1-RAID6-ARRAY1
NEC1-RAID6-ARRAY2
NEC1-RAID6-ARRAY3



> You also can't use process substitution: while read var1 var1 < <(/sbin/zpool list -H)
> 

I'll admit that one's unsupported.


> Any ideas are greatly appreciated. I know there's a python-based script floating on github but I cant guarantee every server will have python on it…
> 

Stick to /bin/sh if you can (like you say, portability and potability in using base utilities).



> Source of script is here: http://en.wikibooks.org/wiki/System_Monitoring_with_Xymon/Other_Docs/HOWTO#Hobbit_Client_and_ZFS_monitoring

The only things I saw that needed changing to go from ksh to /bin/sh were:

	if [ … == … ]; then

Needs to be

	if [ … = … ]; then

And optionally, a style nit would be to convert back-tick pairs into nestable $(…) syntax. For example, change:

	cap=`…`

to instead:

	cap=$(…)

Oh and of course, the HTML should go away since you're making a command-line tool and not a BB/Hobbit/Xymon module.
-- 
Devin

_____________
The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.


More information about the freebsd-questions mailing list