Declare BASH Array Using R System Function

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Declare BASH Array Using R System Function

Dario Strbenac-2
Hello,

It is difficult searching for previous posts about this since the keywords are short and ambiguous, so I hope this is not a duplicate question.

I can easily declare an array on the command line.

$ names=(X Y)
$ echo ${names[0]}
X

I am unable to do the same from within R.

> system("names=(X Y)")
sh: Syntax error: "(" unexpected

Reading the documentation for the system function, it appears to only be relevant for executing commands. What can I do instead to declare a BASH array ? Thanks.

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Declare BASH Array Using R System Function

Jeff Newmiller
You seem confused. You are programming in R, and asking questions about bash on an R mailing list. You seem to need to learn the difference between environment variables and bash variables and how processes acquire and transfer environment variables, which is really an operating system concept and off topic here. Once you do understand this difference, you might be interested in reading the R help file on Sys.setenv().
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

Dario Strbenac <[hidden email]> wrote:

>Hello,
>
>It is difficult searching for previous posts about this since the
>keywords are short and ambiguous, so I hope this is not a duplicate
>question.
>
>I can easily declare an array on the command line.
>
>$ names=(X Y)
>$ echo ${names[0]}
>X
>
>I am unable to do the same from within R.
>
>> system("names=(X Y)")
>sh: Syntax error: "(" unexpected
>
>Reading the documentation for the system function, it appears to only
>be relevant for executing commands. What can I do instead to declare a
>BASH array ? Thanks.
>
>--------------------------------------
>Dario Strbenac
>PhD Student
>University of Sydney
>Camperdown NSW 2050
>Australia
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Declare BASH Array Using R System Function

Peter Dalgaard-2

On Jul 29, 2013, at 08:27 , Jeff Newmiller wrote:

> You seem confused.

Not particularly, but he needs to be aware of _which_ shell R is executing in system() calls. These things work for me:

> system("foo=(bar baz); echo ${foo[1]}")
baz

Dario's issue is suggested by his error message

>>> system("names=(X Y)")
>> sh: Syntax error: "(" unexpected

The shell is (Bourne) "sh", not "bash", so bash extension won't work.

This is highly system dependent: On OSX Snow Leopard, e.g., /bin/sh really is GNU bash, which is why it works for me. Others have the more sane setup where /bin/sh really is Bourne sh.

Next question is of course how to ensure that bash gets used. I must admit that I have long forgotten...

-Peter D.


> You are programming in R, and asking questions about bash on an R mailing list. You seem to need to learn the difference between environment variables and bash variables and how processes acquire and transfer environment variables, which is really an operating system concept and off topic here. Once you do understand this difference, you might be interested in reading the R help file on Sys.setenv().
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> Dario Strbenac <[hidden email]> wrote:
>> Hello,
>>
>> It is difficult searching for previous posts about this since the
>> keywords are short and ambiguous, so I hope this is not a duplicate
>> question.
>>
>> I can easily declare an array on the command line.
>>
>> $ names=(X Y)
>> $ echo ${names[0]}
>> X
>>
>> I am unable to do the same from within R.
>>
>>> system("names=(X Y)")
>> sh: Syntax error: "(" unexpected
>>
>> Reading the documentation for the system function, it appears to only
>> be relevant for executing commands. What can I do instead to declare a
>> BASH array ? Thanks.
>>
>> --------------------------------------
>> Dario Strbenac
>> PhD Student
>> University of Sydney
>> Camperdown NSW 2050
>> Australia
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Declare BASH Array Using R System Function

Prof Brian Ripley
On 29/07/2013 08:49, peter dalgaard wrote:

>
> On Jul 29, 2013, at 08:27 , Jeff Newmiller wrote:
>
>> You seem confused.
>
> Not particularly, but he needs to be aware of _which_ shell R is executing in system() calls. These things work for me:
>
>> system("foo=(bar baz); echo ${foo[1]}")
> baz
>
> Dario's issue is suggested by his error message
>
>>>> system("names=(X Y)")
>>> sh: Syntax error: "(" unexpected
>
> The shell is (Bourne) "sh", not "bash", so bash extension won't work.

See below: the shell should always be 'sh'.

> This is highly system dependent: On OSX Snow Leopard, e.g., /bin/sh really is GNU bash, which is why it works for me. Others have the more sane setup where /bin/sh really is Bourne sh.

On recent OS X /bin/sh is *a variant of* bash.  E.g. shopt xpg_echo is
different if it gets invoked as sh or bash.  Where sh is a link to bash
the behaviour is usually different depending on how it is invoked.

There are quite a lot of systems for which /bin/sh is not based on
either bash or Bourne sh.  As I understand it, Debian/Ubuntu nowadays
use dash by default, and some other Linuxen use ash.  zsh is also seen
as a system shell.  And in many cases this is configurable

Note too that there is quite a lot of flexibility in how bash is configured.

> Next question is of course how to ensure that bash gets used. I must admit that I have long forgotten...

 From ?system

      ‘command’ is parsed as a command plus arguments separated by
      spaces.  So if the path to the command (or an argument) contains
      spaces, it must be quoted e.g. by ‘shQuote’.  Unix-alikes pass the
      command line to a shell (normally ‘/bin/sh’, and POSIX requires
      that shell), so ‘command’ can be anything the shell regards as
      executable, including shell scripts, and it can contain multiple
      commands separated by ‘;’.

So you do not have a choice of shell, and the command-line you pass
needs to invoke a different shell if that is what you want.


But apart from knowing that R's system calls the system(1) OS call (on a
Unix-alike) there is nothing relevant to R-help here.

> -Peter D.
>
>
>> You are programming in R, and asking questions about bash on an R mailing list. You seem to need to learn the difference between environment variables and bash variables and how processes acquire and transfer environment variables, which is really an operating system concept and off topic here. Once you do understand this difference, you might be interested in reading the R help file on Sys.setenv().
>> ---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go Live...
>> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
>>                                       Live:   OO#.. Dead: OO#..  Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>> ---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>> Dario Strbenac <[hidden email]> wrote:
>>> Hello,
>>>
>>> It is difficult searching for previous posts about this since the
>>> keywords are short and ambiguous, so I hope this is not a duplicate
>>> question.
>>>
>>> I can easily declare an array on the command line.
>>>
>>> $ names=(X Y)
>>> $ echo ${names[0]}
>>> X
>>>
>>> I am unable to do the same from within R.
>>>
>>>> system("names=(X Y)")
>>> sh: Syntax error: "(" unexpected
>>>
>>> Reading the documentation for the system function, it appears to only
>>> be relevant for executing commands. What can I do instead to declare a
>>> BASH array ? Thanks.



--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Declare BASH Array Using R System Function

arun kirshna
In reply to this post by Dario Strbenac-2
Hi,
 system("names=(X Y); echo ${names[0]}")
#sh: 1: Syntax error: "(" unexpected


#this worked for me:
 system("bash -c 'names=(X Y); echo ${names[0]}'")
#X

A.K.



----- Original Message -----
From: Dario Strbenac <[hidden email]>
To: "[hidden email]" <[hidden email]>
Cc:
Sent: Sunday, July 28, 2013 10:00 PM
Subject: [R] Declare BASH Array Using R System Function

Hello,

It is difficult searching for previous posts about this since the keywords are short and ambiguous, so I hope this is not a duplicate question.

I can easily declare an array on the command line.

$ names=(X Y)
$ echo ${names[0]}
X

I am unable to do the same from within R.

> system("names=(X Y)")
sh: Syntax error: "(" unexpected

Reading the documentation for the system function, it appears to only be relevant for executing commands. What can I do instead to declare a BASH array ? Thanks.

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Declare BASH Array Using R System Function

Dario Strbenac-2
Thank you. This answers my question. I am using Linux, too.

________________________________________
From: arun [[hidden email]]
Sent: Monday, 29 July 2013 11:11 PM
To: Dario Strbenac
Cc: R help
Subject: Re: [R] Declare BASH Array Using R System Function

Hi,
 system("names=(X Y); echo ${names[0]}")
#sh: 1: Syntax error: "(" unexpected


#this worked for me:
 system("bash -c 'names=(X Y); echo ${names[0]}'")
#X

A.K.



----- Original Message -----
From: Dario Strbenac <[hidden email]>
To: "[hidden email]" <[hidden email]>
Cc:
Sent: Sunday, July 28, 2013 10:00 PM
Subject: [R] Declare BASH Array Using R System Function

Hello,

It is difficult searching for previous posts about this since the keywords are short and ambiguous, so I hope this is not a duplicate question.

I can easily declare an array on the command line.

$ names=(X Y)
$ echo ${names[0]}
X

I am unable to do the same from within R.

> system("names=(X Y)")
sh: Syntax error: "(" unexpected

Reading the documentation for the system function, it appears to only be relevant for executing commands. What can I do instead to declare a BASH array ? Thanks.

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.