Re: comment on RFE: 'shift'' [N] ARRAYNAME

2018-09-27 Thread Greg Wooledge
On Tue, Sep 25, 2018 at 05:17:27PM -0700, L A Walsh wrote:
> It struck me as it might be convenient if 'shift' could take an optional
> arrayname as an argument.  Would that be possible or would it cause some
> incompatibility?

The biggest issue here is how you specify the arguments.

Shift already takes one optional argument: the number of items to shift
from the argv list.  Adding a second optional argument leads to a quagmire.
Do you put the optional list name first, or do you put the optional number
first?  If only one argument is given, is it a list name, or is it a number?

(OK, granted, in bash it is not permitted to create an array whose name
is strictly digits, but still.)

If you wish to write an array-shifting builtin, it would be better to give
it a new name.  Don't blindly copy perl.  It's not always the best example
of language design.



Re: comment on RFE: 'shift'' [N] ARRAYNAME

2018-09-27 Thread Dennis Williamson
On Tue, Sep 25, 2018, 7:17 PM L A Walsh  wrote:

> It struck me as it might be convenient if 'shift' could take an optional
> arrayname as an argument.  Would that be possible or would it cause some
> incompatibility?
>
> i.e.
>
> >  set one two three four five
> >  dcl -a ARGV=("$@")
> >  shift ARGV
> >  echo "${ARGV[@]}"
> two three four five
> >  shift 2 ARGV
> four five
>
> I know it can be done with a function, but with more mess.
> I used (maybe there's a better way, but...):
>
> (in my lib file ArFuncs.shh, that I can include)
>
> [include stdalias]
> #[include Types] #if type-checking include Types+line below
> lshift () {
>   (($#)) || return 1
>   int nshift=1
>   if [[ $1 =~ ^[0-9]+$ ]]; then nshift=$1; shift;fi
>   #if ! isArr $1; then echo >&2 "Need arrayname"; return 1; fi
>   my ar=$1; shift
>   my h="$ar[@]"
>   set "${!h}"
>   shift $nshift
>   eval "${ar}=("$@")"
> }; export -f lshift
>
>
>
> "my" - What is this, Perl?

array_shift=2
arr=("${arr[@]:$array_shift}")

Done.


>


Re: comment on RFE: 'shift'' [N] ARRAYNAME

2018-09-27 Thread Chet Ramey
On 9/27/18 8:42 AM, Dennis Williamson wrote:

> array_shift=2
> arr=("${arr[@]:$array_shift}")
> 
> Done.

This is the simplest and most elegant solution.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: comment on RFE: 'shift'' [N] ARRAYNAME

2018-09-27 Thread Ilkka Virta

On 27.9. 15:35, Greg Wooledge wrote:

Shift already takes one optional argument: the number of items to shift
from the argv list.  Adding a second optional argument leads to a quagmire.
Do you put the optional list name first, or do you put the optional number
first?  If only one argument is given, is it a list name, or is it a number?

(OK, granted, in bash it is not permitted to create an array whose name
is strictly digits, but still.)


Can you make an array whose name even starts with a digit?

With no overlap between array names and valid numbers,
shift [arrayname] [n]   would be unambiguous, as you said.

Though  shift [n [arrayname]]   would be even more backward-compatible 
since the new behaviour would always require two arguments, which is now 
an error.



Even so, deciding how to handle sparse arrays might an interesting 
issue, too.


If one wants a command that looks like the current shift, Dennis's 
obvious slice-assignment could be wrapped in a function. Doing it this 
way of course collapses the indices to consecutive numbers starting at zero.


 ashift() {
 typeset -n _arr_="$1";
 _arr_=("${_arr_[@]:${2-1}}");
 }
 somearray=(a b c d)
 ashift somearray 2


--
Ilkka Virta / itvi...@iki.fi



Re: comment on RFE: 'shift'' [N] ARRAYNAME

2018-09-27 Thread Greg Wooledge
On Thu, Sep 27, 2018 at 06:47:33PM +0300, Ilkka Virta wrote:
> Can you make an array whose name even starts with a digit?

No, that's also disallowed.  Bash variable names (including arrays) must
begin with a letter or underscore.



Re: comment on RFE: 'shift'' [N] ARRAYNAME

2018-09-27 Thread L A Walsh




On 9/27/2018 5:42 AM, Dennis Williamson wrote:



[include stdalias]
#[include Types] #if type-checking include Types+line below
lshift () {
  (($#)) || return 1
  int nshift=1
  if [[ $1 =~ ^[0-9]+$ ]]; then nshift=$1; shift;fi
  #if ! isArr $1; then echo >&2 "Need arrayname"; return 1; fi
  my ar=$1; shift
  my h="$ar[@]"
  ...


"my" - What is this, Perl?


Um...it's a "std" alias! :-)...  It gets included from a bash
include file "stdalias.shh" the first time it is included.

It's really more perl than "int" (or "array" or "map")
that I use for other data types.

I certainly find 'my' and 'int' easier to type and read
than 'declare [-i]' or 'typeset [-i]'.

FWIW, if I wanted it to be more perl-like, I'd at least
use 'sub' before lshift (an alias for 'function') which
is slightly more clear than 'name()', as the empty
parens, at first glance, might indicate the function takes
no parameters, vs. being a syntax element.  The parens are
slightly more concise, so I usually use them.


array_shift=2
arr=("${arr[@]:$array_shift}")
  

---
   I don't think I was aware that slicing syntax worked with arrays.

   Certainly that's much more efficient (which was why I included my
code, cuz I figured my request was valid, OR there had to be a
tighter or better construct than what I'd thrown together

Done.
  

Much thanks!  Your example certainly lessens the need for
shift to handle other arrays, although it still would be
a nice, simple and unambiguous syntax, though:

"shift 2 arr"

still looks simpler and clearer than slice syntax.




Re: comment on RFE: 'shift'' [N] ARRAYNAME

2018-09-27 Thread L A Walsh




On 9/27/2018 5:35 AM, Greg Wooledge wrote:

On Tue, Sep 25, 2018 at 05:17:27PM -0700, L A Walsh wrote:
  

It struck me as it might be convenient if 'shift' could take an optional
arrayname as an argument.  Would that be possible or would it cause some
incompatibility?



The biggest issue here is how you specify the arguments.

Shift already takes one optional argument: the number of items to shift
from the argv list.  Adding a second optional argument leads to a quagmire.
Do you put the optional list name first, or do you put the optional number
first?  If only one argument is given, is it a list name, or is it a number?

(OK, granted, in bash it is not permitted to create an array whose name
is strictly digits, but still.)

If you wish to write an array-shifting builtin, it would be better to give
it a new name.  Don't blindly copy perl.  It's not always the best example
of language design.
  

---
   Why do you think I was blindly copying perl.  My function example
was/is named lshift to differentiate it from rshift (if wanted),
but if it was a builtin there'd be no need for a different
name as it works and has compatible syntax with current 'shift'.

shift [N] ARRAYNAME.  Without ARRAYNAME, it's is the 'shift [N]' that
is already in bash.  There is no quagmire.  If the parameter following 
'shift'

is a number, it's a count.  If it isn't a number, its an ID.  Even
though my example and the subject showed the 'count' first, the order
really doesn't matter if you think about it...





exec

2018-09-27 Thread Francis Gathea
Hi. Why is this function closing out my session if typed on the terminal?



Re: exec

2018-09-27 Thread Francis Gathea
Same thing in a shell script.

On 28/09/2018, Francis Gathea  wrote:
> Hi. Why is this function closing out my session if typed on the terminal?
>