run 7zip commands in sas data set eg call system() - unix

I want to use 7zip command line from sas. I confirmed these commands work from the command line, but I can't get it to work in a sas program. I think this has to do with setting the unix path in SAS. Does anyone know how I could accomplish this?
data _null_;
length command1 command2 $1000;
command1 = 'set PATH=%PATH%;C:\Program Files\7-zip\';
command2 = '7z x "\\mypath\zip.zip" -pmypassword -y -o"\\mypath"';
call system(command1);
call system(command2);
run;

I use it all the time. Here is an example that zips a file from my work folder and adds a password to the zip. Windows example obviously. If running in a corporate environment you may want to ensure you have access to execute shell commands from within SAS (such as %sysexec).
%sysexec "C:\Program Files\7-Zip\7z.exe" a -y -pMyPassword %sysfunc(pathname(work))\DestinationFilename.zip %sysfunc(pathname(work))\SourceFilename.csv;

Related

Calling bash from within R

I have R generating some .csv files for another python program to run in another folder, I know it is possible to call bash from R but how could I call the command make in my ubuntu virtual machine in another directory?
The simple way is creating an script to cd to your dir and exec make after that
script <- tempfile()
fhandle <- file(script)
writeLines("( cd /your_directory && make )",con=fhandle)
system2("/bin/bash",args=c(script))
You may need to find the correct path to /bin/bash, mine is from MacOs
You can work with system2 parameters to control what happens with output from make command and if you want to run the process in parallel with your R task or wait for completion.

Running system() with git-bash in R

I checked
How to execute git-bash command with system() or shell() in R but this didn't exactly solve the problem for me. I'm very new to R, I'm using it on Windows and I'm modifying another project. I suspect originally this project was written for a different OS.
At one point, the main script calls a .sh file from inside R in a for loop, and uses system() to run it. The sh file creates a new directory, copies files from one directory to the other and modifies them slightly (removes first row & adds another).
The part in the code that calls the file goes like this: (run_this is the sh file we want to run)
directory_name = "data/clean"
for (i in 1:n) {
filename = sprintf("%s.json",i)
cmd = sprintf("run_this.sh %s %s", filename, directory_name)
system(cmd)
}
I suspect system() calls command prompt in Windows, which I've checked doesn't run this sh file. But I've found I can run them from Git Bash. Unfortunately I'd have to do them one by one if I choose this option and since n is large this doesn't work very well for me.
So, I'm wondering
1) is there any way to direct system() or system2() to use Git Bash from inside R? (I have Git Bash to my environment variables.)
2) any other possible solutions to run sh files from command prompt?

Schedule multiple R scripts to run sequentially

I have multiple scripts naming R001.r, R002.r, and so on. I need to schedule them so that they run in a sequential manner one after the other. What would be the best approach to do this.
I think you want to wrap your r scripts in a caller sh file and then invoke it through terminal. Here is what I would do.
Open up a terminal or any text editor available and fill it up with the following commands:
Rscript R0001.r
Rscript R0002.r
Rscript R0003.r
...
Save this file into something like call_my_scripts. You can then execute it via standard unix shell commands as follows:
./call_my_scripts
This will run sequentially by definition. Make sure you give exec permissions to the file before you invoke it as follows:
chmod u+x call_my_scripts

Write a file using System Command

I am trying to create a file using the system command but it does not work for some reason. It just echoes the command back.
system("echo 'Hello2222, world.' >foo2.txt");
Hello2222, world. >foo2.txt
When I run the echo command in CMD, the file is created.
Notice that the documentation for system says
On Windows, system does not use a shell and there is a separate
function shell which passes command lines to a shell.
shell("echo 'Hello2222, world.' >foo2.txt")
will do what you want.

SSH key not found when using system() to send git commands to command prompt in windows

I am trying to execute git commands with a public SSH key using the system() function in R (on Windows 7 64 bit). When I try to execute a git command I get the following error:
ssh_askpass: exec(rpostback-askpass): No such file or directory
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I can run the exact same command on the command prompt and it works. I can also run the exact same command on the git bash shell and it works. I am not sure why cmd can't find the key when run from R using the system() command. Is there a way to specify the key location for the command prompt used by the system() command?
Update:
An example call that won't work:
system("git push --set-upstream git#gitlab.mygit.com:MyGroup/testGitlab.git master")
also
system("git clone git#gitlab.mygit.com:MyGroup/gitLabTest.git")
Both of these calls get the same error above.
Update 2: It appears that the ssh keys are not visible from R using list.files()
> list.files("C:/Users/Me/Documents/.ssh/", all.files = TRUE)
[1] "." ".."
I have verified that there are multiple ssh keys in this directory which are visible from the git bash shell using ls -a.
As it turns out, on my Windows system (7, 64 bit), when creating SSH keys with the git bash shell they were saved to "C:\Users\MyAcount\.ssh". However, the cmd.exe called by system() in R was looking in "C:\Users\MyAccount\Documents\.ssh".
For some reason, when using the cmd prompt called directly from the OS it looks in the same .ssh directory as the bash shell. Go Figure. Windows, I don't think I will ever understand you. Hope this helps someone out there.

Resources