Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 4 Apr 2013 13:54:30 +0000
From:      "Teske, Devin" <Devin.Teske@fisglobal.com>
To:        Mark Felder <feld@feld.me>
Cc:        "<freebsd-questions@freebsd.org>" <freebsd-questions@freebsd.org>
Subject:   Re: OT: posix sh problem
Message-ID:  <13CA24D6AB415D428143D44749F57D7201EF2411@ltcfiswmsgmb21>
In-Reply-To: <op.wu0nsgsf34t2sn@tech304.office.supranet.net>
References:  <op.wu0nsgsf34t2sn@tech304.office.supranet.net>

next in thread | previous in thread | raw e-mail | index | archive | help

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

> Hi all,
>=20
> Hopefully someone here is much more clever than I am. I've run out of ide=
as 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 i=
s 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.
>=20

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 multip=
le zpools reading one line at a time and each line item is set as a variabl=
e:
>=20
>=20
> /sbin/zpool list -H | while read name size used avail cap dedup health al=
troot
> do
>  # do interesting things here
> done
>=20
> Unfortunately you can't pipe through read in posix sh.

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

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

=3D=3D=3D

dteske@scribe9.vicor.com ~ $ sh bar
line=3D[line1]
line=3D[line2]

=3D=3D=3D

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 sc=
ript 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@oos0a.lbxrich.vicor.com ~ $ cat bar
#!/bin/sh
/sbin/zpool list -H | while read name size used avail cap dedup health altr=
oot
do
 echo $name
done
dteske@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/z=
pool list -H)
>=20

I'll admit that one's unsupported.


> Any ideas are greatly appreciated. I know there's a python-based script f=
loating on github but I cant guarantee every server will have python on it=
=85
>=20

Stick to /bin/sh if you can (like you say, portability and potability in us=
ing 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 [ =85 =3D=3D =85 ]; then

Needs to be

	if [ =85 =3D =85 ]; then

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

	cap=3D`=85`

to instead:

	cap=3D$(=85)

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

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?13CA24D6AB415D428143D44749F57D7201EF2411>