I have the following bash script (test.sh):
#!/bin/bash
printf '\e[36m%s\e[0m' "This is a test"; echo ""
function yes_or_no {
while true; do
read -p "$* [y/n]: " yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) echo "Aborted" ; exit 1 ;;
esac
done
}
yes_or_no "Do you wish to continue? Y/n"
And I want to run it from within R using the system() command, i.e.
system("./test.sh")
When I do this, I get the following output in the console:
> system("./test.sh")
This is a test
at which point it hangs indefinitely unless I stop R. It seems like there is an issue with interactivity using the read command in bash. When I run from the Terminal I get
$ ./test.sh
This is a test
Do you wish to continue? Y/n [y/n]: n
Aborted
Any ideas how to allow user input when calling bash scripts from R?
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)
Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.3.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] compiler stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] reticulate_1.31 stringi_1.7.12 data.table_1.14.7
loaded via a namespace (and not attached):
[1] Rcpp_1.0.10 pillar_1.8.1 R.methodsS3_1.8.2
[4] R.utils_2.12.2 tools_4.1.2 qs_0.25.5
[7] odbc_1.3.3 bit_4.0.5 jsonlite_1.8.4
[10] lattice_0.20-45 timechange_0.2.0 lubridate_1.9.1
[13] lifecycle_1.0.3 tibble_3.1.8 png_0.1-8
[16] pkgconfig_2.0.3 rlang_1.1.0 Matrix_1.5-3
[19] cli_3.6.0 DBI_1.1.3 rstudioapi_0.14
[22] filelock_1.0.2 yaml_2.3.7 withr_2.5.0
[25] dplyr_1.1.0 hms_1.1.2 generics_0.1.3
[28] edris_1.3.0 vctrs_0.5.2 rappdirs_0.3.3
[31] grid_4.1.2 rprojroot_2.0.3 bit64_4.0.5
[34] tidyselect_1.2.0 glue_1.6.2 here_1.0.1
[37] RApiSerialize_0.1.2 R6_2.5.1 fansi_1.0.4
[40] purrr_1.0.1 blob_1.2.3 magrittr_2.0.3
[43] ellipsis_0.3.2 assertthat_0.2.1 keyring_1.3.1
[46] utf8_1.2.3 stringfish_0.15.7 RcppParallel_5.1.7
[49] sodium_1.2.1 crayon_1.5.2 R.oo_1.25.0