Hi! I quite often use system() to run other programs from within R, but have just hitted a problem: For a given program, I need to set up its environment, which I normally do with source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile from the terminal. Now, when I try to do the same from within R, I get:> system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", intern=TRUE)sh: 1: source: not found Error in system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", : error in running command I need this command to set the environment before I actually run the program. My idea was saving a simple script from within R in which the first line would be source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile and then run the script with system(), but I get that odd error with source. I thought source was just a plain linux command, how can it be "not found" from within system()? Any help much appreciated, Thanks -- Agustin Lobo aloboaleu at gmail.com
Read the man page for bash... source is built-in to the shell interpreter. You should invoke a single shell instance that first sources the profile and then executes the program... but this has nothing to do with R... this would be true for any program invoking an external program on a POSIX-compatible operating system (i.e. not Windows), and so is OT here. On December 20, 2018 3:00:04 AM PST, Agustin Lobo <aloboaleu at gmail.com> wrote:>Hi! >I quite often use system() to run other programs from within R, but >have just hitted >a problem: > >For a given program, I need to set up its environment, which I normally >do with >source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile >from the terminal. >Now, when I try to do the same from within R, I get: > >> system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", >intern=TRUE) >sh: 1: source: not found >Error in system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", >: > error in running command > >I need this command to set the environment before I actually run the >program. My idea was saving a simple script from within R in which >the first line would be >source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > >and then run the script with system(), but I get that odd error with >source. I thought source was just >a plain linux command, how can it be "not found" from within system()? > >Any help much appreciated, >Thanks-- Sent from my phone. Please excuse my brevity.
Hi, I can tell you what the problem is: You're probably running bash at the terminal command line, as I am: [sarahg at localhost]$ echo $0 bash but the R system function uses sh > system("echo $0") sh The bash shell has a source command; the sh shell doesn't. See here for a possible solution: https://stackoverflow.com/questions/4732200/replacement-for-source-in-sh I don't know if there's a way to specify bash shell in system(); a very cursory googling didn't find anything. If you find a way, please report back. Sarah On Thu, Dec 20, 2018 at 10:00 AM Agustin Lobo <aloboaleu at gmail.com> wrote:> > Hi! > I quite often use system() to run other programs from within R, but > have just hitted > a problem: > > For a given program, I need to set up its environment, which I normally do with > source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > from the terminal. > Now, when I try to do the same from within R, I get: > > > system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", intern=TRUE) > sh: 1: source: not found > Error in system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", : > error in running command > > I need this command to set the environment before I actually run the > program. My idea was saving a simple script from within R in which > the first line would be > source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > > and then run the script with system(), but I get that odd error with > source. I thought source was just > a plain linux command, how can it be "not found" from within system()? > > Any help much appreciated, > Thanks > > > -- > Agustin Lobo > aloboaleu at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Sarah Goslee (she/her) http://www.numberwright.com
Isn't 'source' a csh (tcsh, etc.) command? The sh (bash, etc.) command is a period, but you probably will need to use sh constructs in the file (like VAR=value;exportVAR) instead of csh constructs (like setenv VAR value). Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Dec 20, 2018 at 7:00 AM Agustin Lobo <aloboaleu at gmail.com> wrote:> Hi! > I quite often use system() to run other programs from within R, but > have just hitted > a problem: > > For a given program, I need to set up its environment, which I normally do > with > source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > from the terminal. > Now, when I try to do the same from within R, I get: > > > system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", > intern=TRUE) > sh: 1: source: not found > Error in system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", : > error in running command > > I need this command to set the environment before I actually run the > program. My idea was saving a simple script from within R in which > the first line would be > source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > > and then run the script with system(), but I get that odd error with > source. I thought source was just > a plain linux command, how can it be "not found" from within system()? > > Any help much appreciated, > Thanks > > > -- > Agustin Lobo > aloboaleu at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Actually, here's another possibility: system('bash -c "source filename"') On Thu, Dec 20, 2018 at 10:13 AM Sarah Goslee <sarah.goslee at gmail.com> wrote:> > Hi, > > I can tell you what the problem is: > > You're probably running bash at the terminal command line, as I am: > > [sarahg at localhost]$ echo $0 > bash > > but the R system function uses sh > > > system("echo $0") > sh > > The bash shell has a source command; the sh shell doesn't. See here > for a possible solution: > > https://stackoverflow.com/questions/4732200/replacement-for-source-in-sh > > > I don't know if there's a way to specify bash shell in system(); a > very cursory googling didn't find anything. If you find a way, please > report back. > > Sarah > > On Thu, Dec 20, 2018 at 10:00 AM Agustin Lobo <aloboaleu at gmail.com> wrote: > > > > Hi! > > I quite often use system() to run other programs from within R, but > > have just hitted > > a problem: > > > > For a given program, I need to set up its environment, which I normally do with > > source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > > from the terminal. > > Now, when I try to do the same from within R, I get: > > > > > system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", intern=TRUE) > > sh: 1: source: not found > > Error in system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", : > > error in running command > > > > I need this command to set the environment before I actually run the > > program. My idea was saving a simple script from within R in which > > the first line would be > > source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > > > > and then run the script with system(), but I get that odd error with > > source. I thought source was just > > a plain linux command, how can it be "not found" from within system()? > > > > Any help much appreciated, > > Thanks > > > > > > -- > > Agustin Lobo > > aloboaleu at gmail.com > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > > > -- > Sarah Goslee (she/her) > http://www.numberwright.com-- Sarah Goslee (she/her) http://www.numberwright.com
On Thu, 20 Dec 2018 12:00:04 +0100 Agustin Lobo <aloboaleu at gmail.com> wrote:> For a given program, I need to set up its environment, which I > normally do with source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile > from the terminal.The problem with this approach is that in Unix-like systems, child processes cannot directly modify environment variables of their parents (or any other processes besides those they are about to spawn). In /bin/bash, `source` is a special builtin command that runs without spawning child processes - therefore being able to modify the environment of the *current* process.> Now, when I try to do the same from within R, I get: > > > system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile", > > intern=TRUE) > sh: 1: source: not foundMoreover, `source` is a bash-ism and R launches the `system` commands using /bin/sh, which might be different from /bin/bash. The POSIX-correct way to include a batch file in the current shell session is a dot: `. /home/alobo/OTB-6.6.0-Linux64/otbenv.profile` http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#dot Since this way you can only modify the environment of the temporary shell process spawned by `system`, you have set up the environment and launch the executable in a single `system` command: system(". /home/alobo/OTB-6.6.0-Linux64/otbenv.profile; exec otbcli_something") -- Best regards, Ivan