Home > database >  Executing Linux/Unix Command From Within R Using Variables
Executing Linux/Unix Command From Within R Using Variables

Time:11-09

I'm trying to make a call from within R to execute BASH commands, to get my feet wet:

  • I wanted to simply capture a listing of my current files located in a specific directory through use of the "ls -al" command. The output would be sent to text file called a01_test.txt.
  • The directory I would like to capture the contents of is "C:\Users\user00\a01_TEST" which is referenced as "/mnt/c/Users/user00/a01_TEST/" from a WSL Ubuntu 20.04.5 LTS perspective.
  • The directory contains five (5) files: file_01.txt, file_02.txt ,..., file_05.txt.
  • FYI, I am running R (R version 4.2.0 (2022-04-22 ucrt)) via RStudio (2022.07.1 Build 554) on Windows 11 (Version 10.0.22000 Build 22000).

I tried:

PATH_UNIX <- "/mnt/c/Users/user00/a01_TEST/"
FILENAME_TEST <-"a01_test.txt"

paste0("system(\"bash -c \'ls -al ",PATH_UNIX," >",PATH_UNIX,FILENAME_TEST,"\'\")")

However that only returned a command prompt -- nothing else:

> paste0("system(\"bash -c \'ls -al ",PATH_UNIX," >",PATH_UNIX,FILENAME_TEST,"\'\")")
[1] "system(\"bash -c 'ls -al /mnt/c/Users/user00/a01_TEST/ >/mnt/c/Users/user00/a01_TEST/a01_test.txt'\")"
> 

I thought one could test the code using:

cat(print(paste0("system(\"bash -c \'ls -al ",PATH_UNIX," >",PATH_UNIX,FILENAME_TEST,"\'\")")))

which resulted in:

> cat(print(paste0("system(\"bash -c \'ls -al ",PATH_UNIX," >",PATH_UNIX,FILENAME_TEST,"\'\")")))
[1] "system(\"bash -c 'ls -al /mnt/c/Users/user00/a01_TEST/ >/mnt/c/Users/user00/a01_TEST/a01_test.txt'\")"
system("bash -c 'ls -al /mnt/c/Users/user00/a01_TEST/ >/mnt/c/Users/user00/a01_TEST/a01_test.txt'")

If I do not use variables, such as, PATH_UNIX and FILENAME_TEST and code the entire path manually, I can create a text file (a01_test.txt) giving me the desired listing of the directory's contents:

system("bash -c 'ls -al /mnt/c/Users/user00/a01_TEST > /mnt/c/Users/user00/a01_TEST/a01_test.txt'")

which results in:

> system("bash -c 'ls -al /mnt/c/Users/user00/a01_TEST > /mnt/c/Users/user00/a01_TEST/a01_test.txt'")
[1] 0
> 

giving me the file called "a01_test.txt" containing the directory's contents:

total 0
drwxrwxrwx 1 user00 user00 4096 Nov  3  2022 .
drwxrwxrwx 1 user00 user00 4096 Nov  3 05:07 ..
-rwxrwxrwx 1 user00 user00    0 Nov  3  2022 a01_test.txt
-rwxrwxrwx 1 user00 user00    0 Nov  3 05:26 file_01.txt
-rwxrwxrwx 1 user00 user00    0 Nov  3 05:26 file_02.txt
-rwxrwxrwx 1 user00 user00    0 Nov  3 05:26 file_03.txt
-rwxrwxrwx 1 user00 user00    0 Nov  3 05:26 file_04.txt
-rwxrwxrwx 1 user00 user00    0 Nov  3 05:26 file_05.txt

Any assistance to make use of the variables PATH_UNIX & FILENAME_TEST to make a call to Linux/Unix to obtain a directory listing would be appreciated.

CodePudding user response:

sprintf (?sprintf for further details) is a convenient way to create format strings that can subsequently be passed to system:

PATH_UNIX <- '/mnt/c/Users/user00/a01_TEST/'
FILENAME_TEST <- 'a01_test.txt'
cmdstr <- sprintf('bash -c \'ls -al %s > %s\'', PATH_UNIX, FILENAME_TEST)
message('bash command string = ', cmdstr)
system(command = cmdstr)

CodePudding user response:

Expanding on the solution provided by br00t, and doing some testing, one could also use the paste0() function:

# DESIRED CMD TO BE PASSED VIA BASH
cat(paste0("system(bash -c \'ls -al ",PATH_UNIX," >",PATH_UNIX,FILENAME_TEST,"\')"))

# OUTPUT:
# system(bash -c 'ls -al /mnt/c/Users/user00/a01_TEST/ >/mnt/c/Users/user00/a01_TEST/a01_test.txt')

# PLACE DESIRED CMD IN A VAR:
cmdstr_test <- paste0("bash -c \'ls -al ",PATH_UNIX," > ",PATH_UNIX,FILENAME_TEST,"\'")

# CHECK VAR:
message('bash command string = ', cmdstr_test)

# OUTPUT:
# bash command string = bash -c 'ls -al /mnt/c/Users/user00/a01_TEST/ > /mnt/c/Users/user00/a01_TEST/a01_test.txt'

# RUN COMMAND USING system() function:
system(command = cmdstr_test)

# OUTPUT (Will get "0", if successful)
> system(command = cmdstr_test)
[1] 0
> 

  • Related