如何在R中运行一个多命令Linux shell脚本?

时间:2021-07-04 22:22:57

I am spinning up a linux virtual environment which is missing the needed drivers, so I am using the shell to install. I am doing this manually but would like to automate it from within R, where the rest of my code is.

我正在派生一个linux虚拟环境,它缺少所需的驱动程序,因此我正在使用shell进行安装。我是手工做的,但希望在R中实现自动化,我的代码的其余部分是这样的。

I am able to open the shell in R by clicking on Tools>Shell...

我可以通过点击工具> shell打开R的外壳…

I then have multiple lines of Bash script to run in order to install the "ODBC Driver 17 for SQL Server"

然后我要运行多行Bash脚本,以便安装“SQL Server的ODBC驱动程序17”

sudo su 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17

I would like to save these commands in some sort of readable file and run this from within R.

我想把这些命令保存在某种可读文件中,然后从R中运行。

After some search I have seen posts that use System to run a single line but I have not been able to find information on if/how this can be extended to run multiple lines or how to pull these commands from some sort of saved file.

在一些搜索之后,我看到一些文章使用System运行单行,但是我无法找到关于是否/如何将其扩展为运行多行或如何从某种保存的文件中提取这些命令的信息。

Context: I am new to linux and Bash/Shell commands Thank you!

上下文:我是linux和Bash/Shell命令的新手,谢谢!

2 个解决方案

#1


1  

You can separate commands using ; and just store the entire script as a single character string. If you have special characters in your script (you don't currently) you are going to have to be very careful about correctly using escape characters.

您可以使用以下命令分隔命令;将整个脚本存储为一个字符串。如果您的脚本中有特殊字符(目前还没有),您将不得不非常小心地使用转义字符。

For your bash script something like this should work

对于您的bash脚本,类似这样的东西应该可以工作

command="sudo su ; 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -; 
curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list; 
exit;
sudo apt-get update;
sudo ACCEPT_EULA=Y apt-get install msodbcsql17;"

system(command)

Another workaround is to save your script as a .sh bash script and just call the script filename within command(). Note that to make the bash script executable from the command line, you have to add the she-bang line (#!/bin/bash) at the very top.

另一个解决方案是将脚本保存为.sh bash脚本,并在command()中调用脚本文件名。注意,要使bash脚本从命令行可执行,您必须在最顶端添加she-bang行(#!/bin/bash)。

#2


1  

Actually multi-line commands work just fine:

实际上,多行命令可以很好地工作:

command <- "cd
pwd
ls"
system(command)

The problem with the commands provided by MS is the (unnecessary) use of su. You can rewrite the commands to use elevated privileges only where necessary:

MS提供的命令的问题是(不必要的)使用su。只有在必要时,您才可以重写命令来使用高级特权:

command="curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/debian/8/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17"
system(command)

see for example here for sudo tee.

看这里的sudo tee。

#1


1  

You can separate commands using ; and just store the entire script as a single character string. If you have special characters in your script (you don't currently) you are going to have to be very careful about correctly using escape characters.

您可以使用以下命令分隔命令;将整个脚本存储为一个字符串。如果您的脚本中有特殊字符(目前还没有),您将不得不非常小心地使用转义字符。

For your bash script something like this should work

对于您的bash脚本,类似这样的东西应该可以工作

command="sudo su ; 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -; 
curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list; 
exit;
sudo apt-get update;
sudo ACCEPT_EULA=Y apt-get install msodbcsql17;"

system(command)

Another workaround is to save your script as a .sh bash script and just call the script filename within command(). Note that to make the bash script executable from the command line, you have to add the she-bang line (#!/bin/bash) at the very top.

另一个解决方案是将脚本保存为.sh bash脚本,并在command()中调用脚本文件名。注意,要使bash脚本从命令行可执行,您必须在最顶端添加she-bang行(#!/bin/bash)。

#2


1  

Actually multi-line commands work just fine:

实际上,多行命令可以很好地工作:

command <- "cd
pwd
ls"
system(command)

The problem with the commands provided by MS is the (unnecessary) use of su. You can rewrite the commands to use elevated privileges only where necessary:

MS提供的命令的问题是(不必要的)使用su。只有在必要时,您才可以重写命令来使用高级特权:

command="curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/debian/8/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17"
system(command)

see for example here for sudo tee.

看这里的sudo tee。