Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am writing an R program and as part of the program, I need to create a file. If I use the terminal, I need to use sudo.For instance, sudo cp file.tsv file_copy.txt.But I do not know how to create the sudo command in the code.
if (paste(dirname(inFileName), "/", sep="") != OUTpath ) { # Save a copy of params file to OUTpath for records
OS<- Sys.info()["sysname"] # Get OS type
if (OS=="Windows") { # Use OS-specific copy command
copyCommand<- "copy"
} else {
copyCommand<- "cp"
}
How would I include sudo in the code above? Or is there another way to do it?
I have included the all thew code here. I have tried copyCommand<- paste("sudo", "", "cp"), but I get the message below
sudo: no tty present and no askpass program specified
s
if (paste(dirname(inFileName), “/”, sep="") != OUTpath ) {
OS<- Sys.info()[“sysname”] # Get OS type
if (OS==“Windows”) { # Use OS-specific copy command
copyCommand<- “copy”
} else {
copyCommand<- “cp”
}
execSystemCmd<- function(cmd, OS=NULL) {
if (is.null(OS)) OS<- Sys.info()["sysname"] # Get OS type
if (OS=="Windows") { # use OS-secific system shell command wrapper
shell(cmd, translate=TRUE)
} else {
system(cmd)
}
Last edited by Gcefalu; 02-19-2018 at 01:45 PM.
Reason: Tried some thing
I have included the all thew code here. I have tried copyCommand<- paste("sudo", "", "cp"), but I get the message below
sudo: no tty present and no askpass program specifieds
Code:
if (paste(dirname(inFileName), “/”, sep="") != OUTpath ) {
OS<- Sys.info()[“sysname”] # Get OS type
if (OS==“Windows”) { # Use OS-specific copy command
copyCommand<- “copy”
} else {
copyCommand<- “cp”
}
paramsCopyFn<- sub(".tsv", "_copy.txt", paramsCopyFn, fixed=T)
cmd1<- paste(copyCommand, shQuote(inFileName), shQuote(paramsCopyFn) , sep=" ")
execSystemCmd(cmd1)
}
execSystemCmd<- function(cmd, OS=NULL) {
if (is.null(OS)) OS<- Sys.info()["sysname"] # Get OS type
if (OS=="Windows") { # use OS-secific system shell command wrapper
shell(cmd, translate=TRUE)
} else {
system(cmd)
}
Please post code within CODE tags to make it easier to read. And there are two things that come immediately to mind:
Read the man page on the sudo command for the correct syntax, and make sure the user you have in mind is in the sudoers file, and that sudo is set up to not prompt that user for a password. Limit that user to a single command (your program), for better security.
If you're running this program as root or with sudo...there is no NEED to use sudo again in your program.
Thanks for your replies. I have been reading about sudo and its security issues and I realized that I was using the wrong approach. One does not need to prefix the "cp" command with sudo. On the other hand, I can copy/rename a file in R using the functions copy() and rename()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.