LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-19-2018, 10:25 AM   #1
Gcefalu
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Rep: Reputation: Disabled
How do I code "sudo cp"?


hello,

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?

Thanks
 
Old 02-19-2018, 10:56 AM   #2
Gcefalu
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
How to code sudo cp in R using ubuntu 17.04

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”
}

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)
}

Last edited by Gcefalu; 02-19-2018 at 01:45 PM. Reason: Tried some thing
 
Old 02-19-2018, 03:15 PM   #3
Lysander666
Senior Member
 
Registered: Apr 2017
Location: The Underearth
Distribution: Ubuntu, Debian, Slackware
Posts: 2,178
Blog Entries: 6

Rep: Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470
So you want to give a program self-executing root privileges? I don't even know if that's possible, but it sounds like a really bad idea.

This took me two seconds of Googling. It may help:

https://askubuntu.com/questions/4257...nside-a-script
 
1 members found this post helpful.
Old 02-19-2018, 03:15 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,797

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by Gcefalu View Post
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:
  1. 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.
  2. If you're running this program as root or with sudo...there is no NEED to use sudo again in your program.
 
Old 02-19-2018, 08:27 PM   #5
Gcefalu
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
How to code sudo cp in R using ubuntu 17.04

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()
 
  


Reply

Tags
linux command



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] When I type "sudo grub" it says "command not found" in Ubuntu 9.10 Live CD. msbstar Linux - Newbie 10 04-01-2020 11:54 PM
[code]sudo printf "..." >> text.etc[/code] ? jamison20000e Linux - Software 1 08-07-2015 01:07 PM
[SOLVED] Error Class Code: 31 when trying to "sudo yum install php-mbstring" miroslavlav Red Hat 3 07-30-2014 02:51 PM
When I type "sudo grub" it says "command not found" in Ubuntu 10.10 poumtatalia Linux - Newbie 5 09-14-2010 05:21 PM
cannot "sudo apt-get uptate" or "sudo" anything! plz help mdguy21061 Linux - Newbie 7 04-13-2008 11:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration