Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Peter Stuge
Mike Gilbert wrote:
> > [] is shorthand for test. Both test and [[]] in my man bash read:
> >
> > --8<--
> > Expressions are composed of the primaries described .. under
> > CONDITIONAL EXPRESSIONS.
> > -->8--

And the next sentence is exactly what you wrote. :)

"Word splitting and pathname expansion are not performed on the words
between the [[ and ]]"


> Word splitting does not occur within double brackets. This is
> documented in the bash manual.
> 
> http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs
> 
> This causes empty output from a command substitution within double
> brackets to be treated as a zero-length string.

Thanks for pointing me to the right place!


//Peter



Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Mike Gilbert
On Fri, Jun 15, 2012 at 10:05 AM, Peter Stuge  wrote:
> Fabian Groffen wrote:
>> > > >>> +       if [[ ! -n $(egetent passwd "${euser}") ]] ; then
>> > > >>
>> > > >> "! -n" -> "-z"
>> > > >
>> > > > Does the $() argument ever need to be double quoted, or do all
>> > > > versions of bash actually have the string argument optional even
>> > > > though that's not what the man page reads?
>> > >
>> > > Ever?  Yes, but only if what is being returned can contain spaces
>> >
>> > Sorry, I should have mentioned that I had the case of the empty
>> > string in mind.
>>
>> Here for the same reason, the difference between [[ and [ is essential.
>
> It's not clear to me why?
>
> [] is shorthand for test. Both test and [[]] in my man bash read:
>
> --8<--
> Expressions are composed of the primaries described .. under
> CONDITIONAL EXPRESSIONS.
> -->8--
>
> There it says:
> --8<--
> Conditional expressions are used by the [[ compound command and
> the test and [ builtin commands
> -->8--
>
> and:
> --8<--
>       -z string
>              True if the length of string is zero.
>       string
>       -n string
>              True if the length of string is non-zero.
> -->8--
>
> ..which does not at all make it clear that the string is actually
> optional?
>
> Under Command Substitution it says:
> --8<--
> Embedded newlines are not deleted, but they may be removed during
> word splitting.
> ..
> If the substitution appears within double quotes, word splitting
> and pathname expansion are not performed on the results.
> -->8--
>
> ..confirming that there is some processing of the substitution.
>
>
> I also did the tests before asking the question. I'm not trying to
> say that the code doesn't work on my system. I'm asking if it will
> work the same on every version of bash, in spite of what seems to
> be a conflict between real world and documentation.
>
>
> //Peter

Word splitting does not occur within double brackets. This is
documented in the bash manual.

http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

This causes empty output from a command substitution within double
brackets to be treated as a zero-length string.



Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Peter Stuge
Fabian Groffen wrote:
> > > >>> +   if [[ ! -n $(egetent passwd "${euser}") ]] ; then
> > > >> 
> > > >> "! -n" -> "-z"
> > > > 
> > > > Does the $() argument ever need to be double quoted, or do all 
> > > > versions of bash actually have the string argument optional even 
> > > > though that's not what the man page reads?
> > > 
> > > Ever?  Yes, but only if what is being returned can contain spaces
> > 
> > Sorry, I should have mentioned that I had the case of the empty
> > string in mind.
> 
> Here for the same reason, the difference between [[ and [ is essential.

It's not clear to me why?

[] is shorthand for test. Both test and [[]] in my man bash read:

--8<--
Expressions are composed of the primaries described .. under
CONDITIONAL EXPRESSIONS.
-->8--

There it says:
--8<--
Conditional expressions are used by the [[ compound command and
the test and [ builtin commands
-->8--

and:
--8<--
   -z string
  True if the length of string is zero.
   string
   -n string
  True if the length of string is non-zero.
-->8--

..which does not at all make it clear that the string is actually
optional?

Under Command Substitution it says:
--8<--
Embedded newlines are not deleted, but they may be removed during
word splitting.
..
If the substitution appears within double quotes, word splitting
and pathname expansion are not performed on the results.
-->8--

..confirming that there is some processing of the substitution.


I also did the tests before asking the question. I'm not trying to
say that the code doesn't work on my system. I'm asking if it will
work the same on every version of bash, in spite of what seems to
be a conflict between real world and documentation.


//Peter


pgp22DokPyk82.pgp
Description: PGP signature


Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Fabian Groffen
On 15-06-2012 15:41:03 +0200, Peter Stuge wrote:
> Ian Stakenvicius wrote:
> > > Mike Frysinger wrote:
> > >>> +   # lets see if the username already exists +   if [[
> > >>> ! -n $(egetent passwd "${euser}") ]] ; then
> > >> 
> > >> "! -n" -> "-z"
> > > 
> > > Does the $() argument ever need to be double quoted, or do all 
> > > versions of bash actually have the string argument optional even 
> > > though that's not what the man page reads?
> > 
> > Ever?  Yes, but only if what is being returned can contain spaces
> 
> Sorry, I should have mentioned that I had the case of the empty
> string in mind.

Here for the same reason, the difference between [[ and [ is essential.

Fabian

-- 
Fabian Groffen
Gentoo on a different level


signature.asc
Description: Digital signature


Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Peter Stuge
Ian Stakenvicius wrote:
> > Mike Frysinger wrote:
> >>> +   # lets see if the username already exists +   if [[
> >>> ! -n $(egetent passwd "${euser}") ]] ; then
> >> 
> >> "! -n" -> "-z"
> > 
> > Does the $() argument ever need to be double quoted, or do all 
> > versions of bash actually have the string argument optional even 
> > though that's not what the man page reads?
> 
> Ever?  Yes, but only if what is being returned can contain spaces

Sorry, I should have mentioned that I had the case of the empty
string in mind.


//Peter



Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Fabian Groffen
On 15-06-2012 09:35:38 -0400, Ian Stakenvicius wrote:
> On 15/06/12 09:27 AM, Peter Stuge wrote:
> > Mike Frysinger wrote:
> >>> +   # lets see if the username already exists +   if [[
> >>> ! -n $(egetent passwd "${euser}") ]] ; then
> >> 
> >> "! -n" -> "-z"
> > 
> > Does the $() argument ever need to be double quoted, or do all 
> > versions of bash actually have the string argument optional even 
> > though that's not what the man page reads?
> 
> Ever?  Yes, but only if what is being returned can contain spaces (and
> this matters in the way that it's used).  In the case of 'egetent
> passwd', afaict no as it doesn't return anything with whitespace in it.
> 
> Examples -- this works:
> 
> $ bubba="test thing" ; if [ -n "$(echo $bubba)" ]; then echo OK; fi
> OK
> 
> Example -- this fails:
> 
> $ bubba="test thing" ; if [ -n $(echo $bubba) ]; then echo OK; fi
> bash: [: test: binary operator expected

Yes, but this works:

$ bubba="test thing" ; if [[ -n $(echo $bubba) ]]; then echo OK; fi
OK

(and he's using [[, not [)

-- 
Fabian Groffen
Gentoo on a different level


signature.asc
Description: Digital signature


Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Ian Stakenvicius
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

On 15/06/12 09:27 AM, Peter Stuge wrote:
> Mike Frysinger wrote:
>>> +   # lets see if the username already exists +   if [[
>>> ! -n $(egetent passwd "${euser}") ]] ; then
>> 
>> "! -n" -> "-z"
> 
> Does the $() argument ever need to be double quoted, or do all 
> versions of bash actually have the string argument optional even 
> though that's not what the man page reads?
> 
> 
> //Peter


Ever?  Yes, but only if what is being returned can contain spaces (and
this matters in the way that it's used).  In the case of 'egetent
passwd', afaict no as it doesn't return anything with whitespace in it.

Examples -- this works:

$ bubba="test thing" ; if [ -n "$(echo $bubba)" ]; then echo OK; fi
OK

Example -- this fails:

$ bubba="test thing" ; if [ -n $(echo $bubba) ]; then echo OK; fi
bash: [: test: binary operator expected

-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.17 (GNU/Linux)

iF4EAREIAAYFAk/bOioACgkQ2ugaI38ACPAUegD+JPzG4oX25QcqXYSfp/c2IE5o
aydKUHZonedILskm5UoA/2bnn2PMFh5lm1rXh7H4/2d9MQaghAUlCmMv0/XORQtW
=7fD+
-END PGP SIGNATURE-



Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Peter Stuge
Mike Frysinger wrote:
> > +   # lets see if the username already exists
> > +   if [[ ! -n $(egetent passwd "${euser}") ]] ; then
> 
> "! -n" -> "-z"

Does the $() argument ever need to be double quoted, or do all
versions of bash actually have the string argument optional even
though that's not what the man page reads?


//Peter


pgp9d1zHonyTA.pgp
Description: PGP signature


Re: [gentoo-dev] RFC: esethome

2012-06-15 Thread Ian Stakenvicius
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

On 13/06/12 04:51 PM, Mike Frysinger wrote:
> On Wednesday 13 June 2012 15:35:40 Ian Stakenvicius wrote:
>> --- user.eclass   [some timestamp] +++
>> user.eclass.esethome  [some other timestamp] @@ -388,3 +388,63
>> @@ }
>> 
>> fi + +# @FUNCTION: esethome
> 
> has to be inside the giant if block.  so put this above the "fi".
> 
>> +# @USAGE:   +# @DESCRIPTION: +# Update the home
>> directory in a platform-agnostic way. +# Required parameters is
>> the username and the new home directory. +# Specify -1 if you
>> want to set home to the enewuser default +# of /dev/null. +# If
>> the new home directory does not exist, it is created. +# Any
>> previously existing home directory is NOT moved. +esethome() { +
>> _assert_pkg_ebuild_phase ${FUNCNAME} + +   # get the
>> username +   local euser=$1; shift +   if [[ -z ${euser}
>> ]] ; then +   eerror "No username specified !" +
>> die "Cannot call esethome without a username" +   fi + +
>> # lets see if the username already exists +   if [[ ! -n
>> $(egetent passwd "${euser}") ]] ; then
> 
> "! -n" -> "-z" -mike


So outside of syntax issues like above, anyone have any issues with my
adding this to user.eclass ?


-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.17 (GNU/Linux)

iF4EAREIAAYFAk/bNaoACgkQ2ugaI38ACPAFRgEApwLkrsqSIW0nVK4l/dUTRzV9
ijEnbfa+BLc1skwfW4QA+gKgpmsz/K5VqXnVWyXocGqO98RYJL8lL3qm7k0Hs6uZ
=9i38
-END PGP SIGNATURE-



Re: [gentoo-dev] RFC: esethome

2012-06-13 Thread Mike Frysinger
On Wednesday 13 June 2012 15:35:40 Ian Stakenvicius wrote:
> --- user.eclass   [some timestamp]
> +++ user.eclass.esethome  [some other timestamp]
> @@ -388,3 +388,63 @@
>  }
> 
>  fi
> +
> +# @FUNCTION: esethome

has to be inside the giant if block.  so put this above the "fi".

> +# @USAGE:  
> +# @DESCRIPTION:
> +# Update the home directory in a platform-agnostic way.
> +# Required parameters is the username and the new home directory.
> +# Specify -1 if you want to set home to the enewuser default
> +# of /dev/null.
> +# If the new home directory does not exist, it is created.
> +# Any previously existing home directory is NOT moved.
> +esethome() {
> +   _assert_pkg_ebuild_phase ${FUNCNAME}
> +
> +   # get the username
> +   local euser=$1; shift
> +   if [[ -z ${euser} ]] ; then
> +   eerror "No username specified !"
> +   die "Cannot call esethome without a username"
> +   fi
> +
> +   # lets see if the username already exists
> +   if [[ ! -n $(egetent passwd "${euser}") ]] ; then

"! -n" -> "-z"
-mike


signature.asc
Description: This is a digitally signed message part.


Re: [gentoo-dev] RFC: esethome

2012-06-13 Thread Ian Stakenvicius
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

On 13/06/12 03:14 PM, Mike Frysinger wrote:
> 
> eset{home,shell} don't exist today, so you should implement them
> :) -mike

RFC - heavily based on enewuser.

- --- user.eclass   [some timestamp]
+++ user.eclass.esethome  [some other timestamp]
@@ -388,3 +388,63 @@
 }

 fi
+
+# @FUNCTION: esethome
+# @USAGE:  
+# @DESCRIPTION:
+# Update the home directory in a platform-agnostic way.
+# Required parameters is the username and the new home directory.
+# Specify -1 if you want to set home to the enewuser default
+# of /dev/null.
+# If the new home directory does not exist, it is created.
+# Any previously existing home directory is NOT moved.
+esethome() {
+   _assert_pkg_ebuild_phase ${FUNCNAME}
+
+   # get the username
+   local euser=$1; shift
+   if [[ -z ${euser} ]] ; then
+   eerror "No username specified !"
+   die "Cannot call esethome without a username"
+   fi
+
+   # lets see if the username already exists
+   if [[ ! -n $(egetent passwd "${euser}") ]] ; then
+   ewarn "User does not exist, cannot set home. skipping."
+   return 1
+   fi
+
+   # handle homedir
+   local ehome=$1; shift
+   if [[ -z ${ehome} ]] ; then
+   eerror "No home directory specified !"
+   die "Cannot call esethome without a home directory"
+   fi
+
+   if [[ ${ehome} == "-1" ]] ; then
+   ehome="/dev/null"
+   fi
+   einfo " - Home: ${ehome}"
+
+   # update the home directory
+   case ${CHOST} in
+   *-darwin*)
+   dscl . change "/users/${euser}" home "${ehome}"
+   ;;
+
+   *-freebsd*|*-dragonfly*)
+   pw usermod "${euser}" -d "${ehome}" || die
+   ;;
+
+   *)
+   usermod -d "${ehome}" "${euser}" || die
+   ;;
+   esac
+
+   if [[ ! -e ${ROOT}/${ehome} ]] ; then
+   einfo " - Creating ${ehome} in ${ROOT}"
+   mkdir -p "${ROOT}/${ehome}"
+   chown "${euser}" "${ROOT}/${ehome}"
+   chmod 755 "${ROOT}/${ehome}"
+   fi
+}

-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.17 (GNU/Linux)

iF4EAREIAAYFAk/Y64wACgkQ2ugaI38ACPBZYQD9EzzmBDUon1YUNxaev5ONluAX
2GA32hOyvwGs2ylZPy8A/3RN8VNsa6XI++eHRdwjpsSZLw4sTVpa+fY2LZHSnWsh
=gLrd
-END PGP SIGNATURE-