Execute combine multiple Linux commands in one line

后端 未结 9 2474
误落风尘
误落风尘 2020-12-04 04:32

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          


        
相关标签:
9条回答
  • 2020-12-04 04:52
    cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install
    
    0 讨论(0)
  • 2020-12-04 04:54

    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?

    0 讨论(0)
  • 2020-12-04 04:55

    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...

    0 讨论(0)
  • 2020-12-04 04:58

    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
    
    0 讨论(0)
  • 2020-12-04 05:01

    You can use as the following code;

    cd /my_folder && \
    rm *.jar && \
    svn co path to repo && \
    mvn compile package install
    

    It works...

    0 讨论(0)
  • 2020-12-04 05:10

    If you want to execute each command only if the previous one succeeded, then combine them using the && operator:

    cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install
    

    If one of the commands fails, then all other commands following it won't be executed.

    If you want to execute all commands regardless of whether the previous ones failed or not, separate them with semicolons:

    cd /my_folder; rm *.jar; svn co path to repo; mvn compile package install
    

    In your case, I think you want the first case where execution of the next command depends on the success of the previous one.

    You can also put all commands in a script and execute that instead:

    #! /bin/sh
    cd /my_folder \
    && rm *.jar \
    && svn co path to repo \
    && mvn compile package install
    

    (The backslashes at the end of the line are there to prevent the shell from thinking that the next line is a new command; if you omit the backslashes, you would need to write the whole command in a single line.)

    Save that to a file, for example myscript, and make it executable:

    chmod +x myscript
    

    You can now execute that script like other programs on the machine. But if you don't place it inside a directory listed in your PATH environment variable (for example /usr/local/bin, or on some Linux distributions ~/bin), then you will need to specify the path to that script. If it's in the current directory, you execute it with:

    ./myscript
    

    The commands in the script work the same way as the commands in the first example; the next command only executes if the previous one succeeded. For unconditional execution of all commands, simply list each command on its own line:

    #! /bin/sh
    cd /my_folder
    rm *.jar
    svn co path to repo
    mvn compile package install
    
    0 讨论(0)
提交回复
热议问题