Michael Stone <mstone at mathom.us> writes:> On Sun, Mar 20, 2016 at 08:30:33PM +0000, Colin Watson wrote: >>How about something like: >> >> if [ "$(sed -n '${s/.*//;p}' ~/.ssh/authorized_keys | wc -l)" = 0 ]; then >> echo >> ~/.ssh/authorized_keys >> fi >> >>I feel like there must be a neater but still portable way to do this, > > Maybe > > if [ ! -z `tail -c 1 ~/.ssh/authorized_keys` ] ; thenAh, thanks for that, I'd forgotten about the -c option. I think that adding the following, just before the 'mkdir .ssh', should do the trick: [ "`tail -c1 .ssh/authorized_keys 2>/dev/null`" ] && echo > .ssh/authorized_keys ; anyone know of portability issues with that? Cheers, Phil. -- |)| Philip Hands [+44 (0)20 8530 9560] HANDS.COM Ltd. |-| http://www.hands.com/ http://ftp.uk.debian.org/ |(| Hugo-Klemm-Strasse 34, 21075 Hamburg, GERMANY -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 818 bytes Desc: not available URL: <http://lists.mindrot.org/pipermail/openssh-unix-dev/attachments/20160322/b9239fa8/attachment.bin>
Hi, On Tue, Mar 22, 2016 at 08:34:46AM +0100, Philip Hands wrote:> I think that adding the following, just before the 'mkdir .ssh', should > do the trick: > > [ "`tail -c1 .ssh/authorized_keys 2>/dev/null`" ] && echo > .ssh/authorized_keys ; > > anyone know of portability issues with that?I'm not sure what the test will do, but the "echo" should be an "echo >>", no? gert -- USENET is *not* the non-clickable part of WWW! //www.muc.de/~gert/ Gert Doering - Munich, Germany gert at greenie.muc.de fax: +49-89-35655025 gert at net.informatik.tu-muenchen.de -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 291 bytes Desc: not available URL: <http://lists.mindrot.org/pipermail/openssh-unix-dev/attachments/20160322/8345b40f/attachment.bin>
Gert Doering <gert at greenie.muc.de> writes:> Hi, > > On Tue, Mar 22, 2016 at 08:34:46AM +0100, Philip Hands wrote: >> I think that adding the following, just before the 'mkdir .ssh', should >> do the trick: >> >> [ "`tail -c1 .ssh/authorized_keys 2>/dev/null`" ] && echo > .ssh/authorized_keys ; >> >> anyone know of portability issues with that? > > I'm not sure what the test will do, but the "echo" should be an > "echo >>", no?Yes. Doh! ;-) The test gets the last character of the file, and puts it in quotes after removing new-lines -- which basically means that test fails if there's a newline there, or if the file's empty, since that gives you: [ "" ] which means that the second half of the && does not need to be evaluated -- the result being that a newline is only added if the last character of the file is not already a newline. The only thing that's wrong with that is if the file is empty it'll end up with a blank line at the start, but a) why would there be an empty file there? and b) who cares? Cheers, Phil. -- |)| Philip Hands [+44 (0)20 8530 9560] HANDS.COM Ltd. |-| http://www.hands.com/ http://ftp.uk.debian.org/ |(| Hugo-Klemm-Strasse 34, 21075 Hamburg, GERMANY -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 818 bytes Desc: not available URL: <http://lists.mindrot.org/pipermail/openssh-unix-dev/attachments/20160322/2125d3e8/attachment-0001.bin>
On 22/03/16 08:34, Philip Hands wrote:> > [ "`tail -c1 .ssh/authorized_keys 2>/dev/null`" ] && echo > .ssh/authorized_keys ; > > anyone know of portability issues with that? >On Solaris 10 and older /usr/bin/tail does not understand the -c1 syntax, it needs -1c instead. The XPG4 version in /usr/xpg4/bin/tail can understand either syntax. I did not have Solaris 11 available to check. -tgc
"Tom G. Christensen" <tgc at jupiterrise.com> writes:> On 22/03/16 08:34, Philip Hands wrote: >> >> [ "`tail -c1 .ssh/authorized_keys 2>/dev/null`" ] && echo > .ssh/authorized_keys ; >> >> anyone know of portability issues with that? >> > > On Solaris 10 and older /usr/bin/tail does not understand the -c1 > syntax, it needs -1c instead.OK, it seems that tail from coreutils on Debian can deal with -1c as well -- odd -- is this actually the portable option despite not being in the manual? BTW can Solaris 10's tail handle it with a space after the c, thus: tail -c 1 Cheers, Phil. -- |)| Philip Hands [+44 (0)20 8530 9560] HANDS.COM Ltd. |-| http://www.hands.com/ http://ftp.uk.debian.org/ |(| Hugo-Klemm-Strasse 34, 21075 Hamburg, GERMANY -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 818 bytes Desc: not available URL: <http://lists.mindrot.org/pipermail/openssh-unix-dev/attachments/20160322/cc6afc3f/attachment.bin>