Print Boolean value from Rscript to Bash variable

I have an R script that outputs TRUE or FALSE. In R, it uses a valid T / F data type, but when I return its return value in bash, it looks like a string, saying:

"[1] TRUE"

or

"[1] FALSE"

They are preceded by [1]. Also no [0], this is not a typo. Anyway, the result of this is that when I try to check the output of this Rscript to run the subsequent script, I need to do a string comparison with "[1] TRUE" as shown below, instead of comparing with "TRUE" or "1 "that feels cleaner and better.

A=$(Rscript ~/MyR.r)
echo $A
if [ "$A" == "[1] TRUE" ]; then
    bash SecondScript.sh
fi

How can I do R, either print true Boolean, or Bash accept the output string and convert it to 0/1 for comparison? That is, I would rather check ...

if [ $A == TRUE ];

than

if [ "$A" == "[1] TRUE" ];

, , ?

* *

Rscript, ...

myinput <- TRUE #or FALSE

FunctionName <- function(test){
if (test == TRUE){
val <- TRUE
} else {
val <- FALSE
}
return(val)
}

FunctionName(myinput)
+5
3

print() print() - ing, , R, cat(), "[1]" TRUE, FALSE. 0/1, cat(as.numeric(.)). :

myinput <- TRUE #or FALSE
FunctionName <- function(test){
   if (test == TRUE){
              val <- TRUE
                    } else {
              val <- FALSE
            }
   cat(as.numeric(val))
}
FunctionName(myinput)

, :

cat(as.numeric(val)); invisible(val)
+9

, R script, Bash:

if [[ $A == *TRUE* ]]

if [[ $A =~ TRUE ]]

, , , :

pattern=TRUE
if [[ $A =~ $pattern ]]

, .

0

You can also define a function in bash that converts the R output to TRUE / FALSE:

r_to_bash() {
  echo "$1" | cut -d' ' -f2
}

Now you can use it as:

A=$(r_to_bash "$A")
if [ $A == TRUE ];
0
source

All Articles