Running a Windows executable file from within R with command line options - r

I am trying to call a Windows program called AMDIS from within R using the call
system("C:/NIST08/AMDIS32/AMDIS_32.exe /S C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF")
in order to carry out an analysis (specified using the /S switch) on a file called CI23_Q_120828_01.CDF, but it seems that no matter what I try the file is not loaded in correctly, presumably because the options are not passed along. Does anyone have a clue what I might be doing wrong?
Right now this command either
doesn't do anything,
makes AMDIS pop up, but it doesn't load the file I specify
gives me the error
Warning message:
running command 'C:/NIST08/AMDIS32/AMDIS_32.exe /S
C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF'
had status 65535
(I have no idea what results in these different outcomes of the same command)
(the AMDIS command line options are described here at the page 8)
Cheers,
Tom
EDIT:
Found it had to do with forward vs backslashes - running
system("C:\\NIST08\\AMDIS32\\AMDIS_32.EXE C:\\Users\\Ento\\Documents\\GCMS\\test_cataglyphis_iberica\\queens\\CI23_Q_120828_01.CDF /S /E")
seems to work - thank you all for the suggestions!

You've heard of bquote , noquote , sQuote, dQuote , quote enquote and Quotes, well now meet shQuote!!! :-)
This little function call works to format a string to be passed to an operating system shell. Personally I find that I can get embroiled in backslash escaping hell, and shQuote saves me. Simply type the character string as you would on the command line of your choice ('sh' for Unix alikes like bash , csh for the C-shell and 'cmd' for the Windows shell ) wihtin shQuote and it will format it for a call from R using system:
shQuote("C:/NIST08/AMDIS32/AMDIS_32.exe /S C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF" , type = "cmd" )
#[1] "\"C:/NIST08/AMDIS32/AMDIS_32.exe /S C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF\""
More generally, you can use shQuote like this:
system( shQuote( "mystring" , type = c("cmd","sh") ) , ... )

Related

Pass a path argument having brackets to a MS DOS Batch file

I need to achieve the below using Robotframework script:
c:\>runbatch "C:\Program Files (x86)\tool\bin\test.exe" C:\tool\get.ini
where runbatch is a MS DOS Batch and "C:\Program Files (x86)\tool\bin\test.exe" and C:\tool\get.ini are parameters to the batch file. The first argument contains path of a tool that has "(" and ")" in its path.
So in my Robot script I have a variable like below:
${tool_path} "C:\\Program Files (x86)\\tool\\bin\\test.exe"
${tool_ini} "C:\tool"
And invoke like below:
${RC}= Run Process ${CURDIR}/../scripts/runbatch.bat ${tool_path} ${tool_ini}\\get.ini
The execution fails but note when I run it via the same param thru the command line as standalone batch it works fine.
In the batch I added comments to just log the arguments and I found that they are completely distorted, the tool_path value is completely distorted ("\"C:\Program) and second argument becomes (Files ) - how can I fix the issue in robot script such that when a path is passed having braces are not modified?
You need to also escape the backslashes in ${tool_ini} - make its value c:\\tool; that's not the culprit thought, just something else to change.
Remove the double quotes in the arguments' values - Run Process does not need them in the way you are calling it, with a keyword argument per script argument. E.g.:
${tool_path} C:\\Program Files (x86)\\tool\\bin\\test.exe
${tool_ini} C:\\tool
${RC}= Run Process ${CURDIR}/../scripts/runbatch.bat ${tool_path} ${tool_ini}\\get.ini
The way you've put them, they have become a part of the value itself.
Alternatively, keeping the double quotes there, you can call the script with all arguments in the call line:
${tool_path} "C:\\Program Files (x86)\\tool\\bin\\test.exe"
${tool_ini} C:\\tool
${RC}= Run Process ${CURDIR}/../scripts/runbatch.bat ${tool_path} "${tool_ini}\\get.ini"
(the second one doesn't really need quotes, but I've added them for consistency)
By the way, not really an issue, yet - the script path uses slashes (/), which is a bit unorthodox for Windows. Contrary to the popular believe, the OS does support this path delimiter pretty much the same way as it supports backslashes (\), it's just not widely used and looks a bit out of place.

How to execute a command with multiple parameters from a file in gradle?

I have below command in a .txt-file:
java -jar /path/to/something.jar --classpath="/path/to/something/other.jar" --url="something:#127.0.0.1:1234:TEST12" --driver=some.driver update
As can be seen multiple parameters with different syntax (with -, --, and/or with and without "") are used.
I tried the following code:
task test(type: Exec) {
workingDir '/path/to/working/dir'
String commandFromFile = new File('/path/to/file/with/command' + 'filewithcommand.txt').getText('UTF-8')
commandLine commandFromFile
}
On windows platforms this code is working but on unix it doesn't.
As you can see in the documentation of the Exec task, you should split up your command into its parts. So doing commandLine commandFromFile.split(' ') should work if you do not have spaces in your arguments. If you have, you need a more sophisticated way to split the command that takes quotes into account.
Or you change the format of your command file so that it has one argument per line and you use .readLines('UTF-8') instead of .getText('UTF-8').
I'm not 100% sure about the following, but it could be that you also have to remove the quoting around arguments even if they contain spaces, as you give the arguments as single entities to the commandLine call and thus need no quoting for escaping spaces here. Depending on OS and tool you call it could even break the command if there are quotes that it cannot handle.
Alternatively, but that is the worse method imho, you can also do something like
if (windows) {
commandLine 'cmd', '/c', commandFromFile
} else {
commandLine 'sh', '-c', commandFromFile
}
where then the command processor does the splitting and so on. There you need the quotes and stuff of course. The windows variable in this example of course needs to be determined, e. g. from system properties.

Unix SQLLDR scipt gives 'Unexpected End of File' error

All, I am running the following script to load the data on to the Oracle Server using unix box and sqlldr. Earlier it gave me an error saying sqlldr: command not found. I added "SQLPLUS < EOF", it still gives me an error for unexpected end of file syntax error on line 12 but it is only 11 line of code. What seems to be the problem according to you.
#!/bin/bash
FILES='ls *.txt'
CTL='/blah/blah1/blah2/name/filename.ctl'
for f in $FILES
do
cat $CTL | sed "s/:FILE/$f/g" >$f.ctl
sqlplus ID/'PASSWORD'#SERVERNAME << EOF sqlldr SCHEMA_NAME/SCHEMA_PASSWORD control=$f.ctl data=$f EOF
done
sqlplus will never know what to do with the command sqlldr. They are two complementary cmd-line utilities for interfacing with Oracle DB.
Note NO sqlplus or EOF etc required to load data into a schema:
#!/bin/bash
#you dont want this FILES='ls *.txt'
CTL_PATH=/blah/blah1/blah2/name/'
CTL_FILE="$CTL_PATH/filename.ctl"
SCHEMA_NM=SCHEMA_NAME
SCHEMA_PSWD=SCHEMA_PASSWORD
for f in *.txt
do
# don't need cat! cat $CTL | sed "s/:FILE/$f/g" >"$f".ctl
sed "s/:FILE/$f/g" "$CTL_FILE" > "$CTL_PATH/$f.ctl"
#myBad sqlldr "$SCHEMA_NAME/$SCHEMA_PASSWORD" control="$CTL_PATH/$f.ctl" data="$f"
sqlldr $SCHEMA_USER/$SCHEMA_PASSWORD#$SERVER_NAME control="$CTL_PATH/$f.ctl" data="$f" rows=10000 direct=true errors=999
done
Without getting too philosophical, using assignments like FILES=$(ls *.txt) is a bad habit to get into. By contrast, for f in *.txt will deal correctly for files with odd characters in them (like spaces or other syntax breaking values). BUT the other habit you do want to get into is to quote all variable references (like $f), with dbl-quotes : "$f", OK? ;-) This is the otherside of protection for files with spaces etc embedded in them.
In the edit update, I've varibalized your CTL_PATH and CTL_FILE. I think I understand your intent, that you have 1 std CTL_FILE that you pass thru sed to create a table specific .ctl file (a good approach in my experience). Note that you don't need to use cat to send a file to sed, but your use to create a altered file via redirection (> $f.ctl) is very shell-like too.
In 2nd edit update, I looked here on S.O. and found an example sqlldr cmdline that has the correct syntax and have modified to work with your variable names.
To finish up,
A. Are you sure the Oracle Client package is installed on the machine
that you are running your script on?
B. Is the /path/to/oracle/client/tools/bin included in your working
$PATH?
C. try which sqlldr. If you don't get anything, either its not
installed or its not in the path.
D. If not installed, you'll have to get it installed.
E. Once installed, note the directory that contains the sqlldr cmd.
find / -name 'sqlldr*' will take a long time to run, but it will
print out the path you want to use.
F. Take the "path" part of what is returned (like
/opt/oracle/11.2/client/bin/ (but not the sqlldr at the end), and
edit script at 2nd line with
(Txt added to appease the S.O. Formatter ;-) )
export ORCL_PATH="/path/you/found/to/oracle/client"
export PATH="$ORCL_PATH:$PATH"
These steps should solve any remaining issues. If this doesn't work, see if there is someone where you work that understands your local computing environment that can help explain any missing or different steps.
IHTH

Julia: dollar sign in command without single quotes

I am trying to run the following command from Julia:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/user/.julia/v0.3/Smile/deps/downloads
When I run it as-is it tries to replace $LD_LIBRARY_PATH with a local variable.
When I escape the $, it puts quotes around the command, which invalidates it.
julia> cmd = `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/tim/.julia/v0.3/Smile/deps/downloads`
ERROR: LD_LIBRARY_PATH not defined
julia> cmd = `export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/home/tim/.julia/v0.3/Smile/deps/downloads`
`export 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/tim/.julia/v0.3/Smile/deps/downloads'`
I would like to run the command in a form similar to:
run(`export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$newpath`)
How do I properly handle the dollar sign?
Thank you
*note: pasting the command directly into terminal and running it does work
In Julia, backticks are not completely equivalent to running the corresponding command at the shell. You can't interpolate environmental variables with $ (although $(get(ENV, "varname", "") should match the shell's behavior), and export is a shell built-in, not a command, so I don't think you can run it. Also, even if backticks shelled out, export would only change the environment of the subshell, not the calling process.
You should be able to set LD_LIBRARY_PATH from Julia as:
ENV["LD_LIBRARY_PATH"] = "$(get(ENV, "LD_LIBRARY_PATH", "")):$newpath"
but you should avoid this if possible. If your intent is to ccall a specific library, you can pass the library path directly to ccall, perhaps using find_library as you indicated in a comment if you don't know the full path advance. If you need to set LD_LIBRARY_PATH because the library needs to load other libraries, I'm not sure if there's a better way, but note that LD_LIBRARY_PATH is platform-specific. You might be able to dlopen the dependent libraries first, but I haven't tested that.

Problems with spaces in paths when calling a batch file from R

I have some problems to call a command line program called molconvert from R using system() in Windows. molconvert is located in "C:\Program Files\ChemAxon\MarvinBeans\bin"
I would then like to invoke system() or shell() to mimick what I would achieve by typing
molconvert pdb "C:\molecule conversions\cembrene A.mol"
at the command prompt and collect the resulting output back to R as in
out=system(...,intern=T)
I seem to have trouble though with the backslashes and the spaces in the paths.
I tried with
dirmolconvert="C:\\Program Files\\ChemAxon\\MarvinBeans\\bin"
shell(shQuote(paste(dirmolconvert,"\\molconvert pdb "C:\\cembrene A.mol",sep="")))
but that gives me "Error: unexpected symbol in ..." and escaping the " also doesn't help. Any thoughts on how I should resolve this?
or
system(paste(dirmolconvert,"\\molconvert pdb \"C:\\cembrene A.mol\"",sep=""), intern=T)
but that gives me
'C:\Program' not found
Any thoughts?
Edit:
Based on the answer below the right way to do this apparently is
inputdir="C:/Users/Ento/Documents/GCMS/molconvert test"
molconvertdir="C:/Program Files/ChemAxon/MarvinBeans/bin"
molecule="cembrene A.mol"
out=system(paste(shQuote(file.path(molconvertdir, "molconvert.bat")),
"pdb",
shQuote(file.path(inputdir,molecule))),intern=T)
You want to use shQuote to quote the path to the executable, not the entire command line. Depending on what your molconvert program expects, you may also want to quote paths that are arguments to it.
system(paste(shQuote(file.path(dirmolconvert, "molconvert.exe")),
"pdb",
shQuote("C:\\molecule conversions\\cembrene A.mol"))
the easiest way to fix this is to use short paths:
for %%a in ("C:\molecule conversions\cembrene A.mol") set "sh_path=%%~dpfnxsa"
or
for %%a in ("C:\Program Files\ChemAxon\MarvinBeans\bin") do set "pf_sh_path=%%~dpfnxsa"
and then to pass %sh_path% and %pf_sh_path% as parameter.

Resources