I know similar questions have been asked already but I cannot adapt the previous answers to my specific situation, so I'd greatly appreciate any help.
I use the following code in Windows and it works perfectly fine.
try
{
Process protSpec = Runtime.getRuntime().exec(new String[]{"cmd", "/c", "obspectrophore -i Rv0001.fasta > Rv0001.spec" });
protSpec.waitFor();
}
catch (Exception e1)
{
System.out.println(e1.toString());
e1.printStackTrace();
}
Trying to adapt it to Linux, I cut the "cmd", "/c" of the above code to get
try
{
Process protSpec = Runtime.getRuntime().exec(new String[]{"obspectrophore -i Rv0001.fasta > Rv0001.spec" });
protSpec.waitFor();
}
catch (Exception e1)
{
System.out.println(e1.toString());
e1.printStackTrace();
}
Now I am getting the error message
java.io.IOException: Cannot run program "obspectrophore -i Rv0001.fasta > Rv0001.spec": java.io.IOException: error=2, No such file or directory
Any ideas for solving this problem would be greatly appreciated. Thanks in advance!
The Linux equivalent of cmd /c is sh -c. The command syntax is the same in this case:
Runtime.getRuntime().exec(new String[]{
"sh", "-c", "obspectrophore -i Rv0001.fasta > Rv0001.spec"
});
Your attempted Linux command doesn't run the command through a command interpretter, which is why it fails (this is the equivalent of pasting Java code into a DOS prompt, hoping that DOS will figure it out and run it anyways).
Set obspectrophore in environment so that it can be accessed from any directory, as well make sure user having executing permission.
Related
I'm trying to execute commands that are passed from the terminal to argv seperated by : to be more specific cat nevermind : grep left : wc -c.
tabCommand is an array that contains each command so cat nevermind,grep left,wc -c
With printf I can confirm that tabCommand[i-1] is indead equal to cat nevermind but the output I get is Error: No such file or directory
if (execl(tabCommande[i-1],tabCommande[i-1], (char *)NULL) == -1) {
error_and_exit();
}
If someone can help me find the issue I would really appreciate it.
With the comments I got in my post I managed to find my problem
execlp("/bin/sh","sh","-c",tabCommande[i], (char *)NULL) works because I need to use the full path.
If I do execlp(tabCommande[i],tabCommande[i], (char *)NULL) it won't work because im not using the full path of each command so simply giving cat to execlp won't work.
found this answer thanks to waltinator I'm new to stack so i dont know how to give you the credit
This command works fine:
json -I -f ./src/environments/build.json -e 'this.patch++'
I'm trying to create a custom build NPM command in my package.json file that runs this command before the actual build, but first I just tried to run the json command, just to see if it's working, but it doesn't :/
package.json
{
...
"scripts": {
...
"svrge-build-dev": "json -I -f ./src/environments/build.json -e 'this.patch++'",
...
}
then I get this output (which is exactly the same as when I run the JSON code by itself) which means that the command is definitely running
> web-client#1.0.0 svrge-build-dev D:\repos\test\web-client
> json -I -f ./src/environments/build.json -e 'this.patch++
json: updated "./src/environments/build.json" in-place //<- this is exactly the same'
However, the build.json file is not being updated
No errors in the terminal
any idea how I can get it to work? I can't seem to find anything regarding that.
I would appreciate any help, been scratching my head for hours
Tom
Re-stating what I said in the comment: try to replace the single quotes with double quotes:
{
// ...
"scripts": {
// ...
"svrge-build-dev": "json -I -f ./src/environments/build.json -e \"this.patch++\"",
// ...
}
What triggered that thought was this line in the output:
> json -I -f ./src/environments/build.json -e 'this.patch++
After doing some digging, I think it depends on the OS and/or command line interpreter. It shows the command that was run, but not with the finishing single quote.
After some searching, it appears that is indeed a bug: see this issue and this issue. You might want to give the maintainers a heads up about it: the last issue I've linked to, makes it seem like it's a Windows issue.
I am using Gradle for AOSP, I would like to check if a command exists in my build environment.
task printCommand{
doLast{
def command = "git --version"
println command.execute().text
}
}
Above code run perfect, it will print the output from command "git --version".
But I try another command according to Check if a program exists from a Bash script
task printCommand{
doLast{
def command = "command -v docker"
println command.execute().text
}
}
It always show the wrong message like this.
Execution failed for task ':printCommand'.
java.io.IOException: Cannot run program "command": error=2, No such file or directory
Why I can't use "command -v docker" in this way ?
Are there any better ways to check if a command exists in Gradle ?
command is a builtin bash command, not a binary.
groovy's String.execute will start a process. The binary that the process is started from has to be given fully qualified (e.g. "/usr/bin/docker --version") or must be found on your $PATH (or %PATH%)
Get back to the subject, I find the way to ensure that a command exists while using Gradle, this code can avoid Gradle script terminated by non-zero exitValue, and print the appropriate information.
task checkCommand{
doLast{
result = exec{
def command = "command -v docker"
ignoreExitValue = true
executable "bash" args "-l", "-c", command
}
if(result.getExitValue()==0){
println "Has Docker"
}else{
print "No Docker"
}
}
}
Update 2019/02/23
If you get this error:
Could not set unknown property 'result' for task ':checkCommand' of
type org.gradle.api.DefaultTask
Add def in front of result will fix this issue.
EDIT: So ultimately what I ended up doing here is running it straight in a mysql.expect script. Any variables that need to be updated would be replaced via sed in standard bash script used to launch mysql.expect.
I have a bash script that runs expect, and automates the MySQL installation process, as you can see here. The reason expect needs to be called in this script is to source local bash variables, so I can't just run it via expect, but rather it needs to be called it as follows:
if [ catch "spawn /bin/bash ./mysql.sh" error ] {
puts "Could not spawn mysql.sh $error"
}
I know this works, because there's another script I have called "test.sh" that does the following:
#!/bin/bash
source ./myvars.rc
echo "CONNECTED" >> "./out.html"
echo "$MYVARIABLE" >> "./out.html"
This works fine, the variable is correctly added to out.html. The mysql.sh script works when called directly, but not through expect, and there are no errors. Any ideas? Thanks.
I'm not an expect expert, but you may have a syntax error in the spawn command. This seems to work:
#!/usr/bin/expect
if { [ catch {spawn /bin/bash ./mysql.sh} error ] } {
puts "Could not spawn mysql.sh $error"
}
# This is the important part
interact
catch returns 0 (OK) if your command succeeds. You were seeing "success" because it really errored out, but you were testing the other condition.
So, I did some more testing and updated that a bit. You don't want the ! there, but you do want the interact. Once you spawn something with expect, you want to either use expect to process the output, or if there is none to process, just let the script run with interact.
The interesting thing is that if you use /bin/bash ./mysql.sh without interact, this will just do nothing but not actually run the script. If you use just ./mysql.sh, it will hang. I assume there is something with standard in/out that happens differently between the two.
Anyway, I hope this helps and actually solve your problem. Extra added stuff because I'm geeking out here -- you probably want exec instead of spawn if you don't want to interact with your script:
#!/usr/bin/expect
if { [ catch {puts [exec /bin/bash ./mysql.sh]} error ] } {
puts "Could not spawn mysql.sh $error"
}
The puts is there because otherwise you will lose the output of the script. If you don't care about the output, you can use:
#!/usr/bin/expect
if { [ catch {exec /bin/bash ./mysql.sh} error ] } {
puts "Could not spawn mysql.sh $error"
}
I need to be able to execute some shell commands such as moving to the right directory where I have some files I need to decode and then decoding them using another command. I read some stuff about using popen but I didnt really understand how to use it for executing multiple commands.
Any pointers will be greatly appreciated :)
Thanks
FILE *pf;
char command[150];
char data[512];
// Execute a process listing
sprintf(command, "cd");
pf = _popen(command,"r");
sprintf(command, "cd Test_copy");
pf = _popen(command,"r"); */
sprintf(command, "java -jar Tool.jar -b x.fit x.csv");
pf = _popen(command,"r");
if(!pf){
fprintf(stderr, "Could not open pipe for output.\n");
return;
}
// Grab data from process execution
fgets(data, 512 , pf);
// Print grabbed data to the screen.
fprintf(stdout, "-%s-\n",data);
if (_pclose(pf) != 0)
fprintf(stderr," Error: Failed to close command stream \n");
Use ShellExecute to play with files (open with default application etc.). Use system to run shell commands.
No, don't. That would be like using a sledgehammer to knock on a door. Furthermore, it is "evil": http://www.cplusplus.com/forum/articles/11153/