I am trying to merge multiple linux commands in one line to perform deployment operation. For example
cd /my_folder
rm *.jar
svn co path to repo
mvn compile package install
This question is related to
linux
command-line
You can separate your commands using a semi colon:
cd /my_folder;rm *.jar;svn co path to repo;mvn compile package install
Was that what you mean?
You can use as the following code;
cd /my_folder && \
rm *.jar && \
svn co path to repo && \
mvn compile package install
It works...
I've found that using ; to separate commands only works in the foreground. eg :
cmd1; cmd2; cmd3 &
- will only execute cmd3
in the background, whereas
cmd1 && cmd2 && cmd3 &
- will execute the entire chain in the background IF there are no errors.
To cater for unconditional execution, using parenthesis solves this :
(cmd1; cmd2; cmd3) &
- will execute the chain of commands in the background, even if any step fails.
To run them all at once, you can use the pipe line key "|" like so:
$ cd /my_folder | rm *.jar | svn co path to repo | mvn compile package install
I find lots of answer for this kind of question misleading
Modified from this post: https://www.webmasterworld.com/linux/3613813.htm
The following code will create bash window and works exactly as a bash window. Hope this helps. Too many wrong/not-working answers out there...
Process proc;
try {
//create a bash window
proc = Runtime.getRuntime().exec("/bin/bash");
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
BufferedReader err = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
//input into the bash window
out.println("cd /my_folder");
out.println("rm *.jar");
out.println("svn co path to repo");
out.println("mvn compile package install");
out.println("exit");
String line;
System.out.println("----printing output-----");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
while((line = err.readLine()) != null) {
//read errors
}
proc.waitFor();
in.close();
out.close();
err.close();
proc.destroy();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you want to execute all the commands, whether the previous one executes or not, you can use semicolon (;) to separate the commands.
cd /my_folder; rm *.jar; svn co path to repo; mvn compile package install
If you want to execute the next command only if the previous command succeeds, then you can use && to separate the commands.
cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install
In your case, the execution of consecutive commands seems to depend upon the previous commands, so use the second example i.e. use && to join the commands.
What is the utility of an only one Ampersand? This morning, I made a launcher in the XFCE panel (in Manjaro+XFCE) to launch 2 passwords managers simultaneously:
sh -c "keepassx && password-gorilla"
or
sh -c "keepassx; password-gorilla"
But it does not work as I want. I.E., the first app starts but the second starts only when the previous is closed
However, I found that (with only one ampersand):
sh -c "keepassx & password-gorilla"
and it works as I want now...
cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install
Source: Stackoverflow.com