How to run multiple commands in one line using cmd

2019-08-27 16:24发布

as the title said I need to run exactly two commands in one line using cmd in windows 10. How is it possible?

标签: cmd
3条回答
孤傲高冷的网名
2楼-- · 2019-08-27 16:50

cmd1;cmd2

cmd1&cmd2

cmd1|cmd2

查看更多
劳资没心,怎么记你
3楼-- · 2019-08-27 16:59

Easy to pick one from general syntax:

Run multiple commands (cmd1, cmd2, cmd3) in one line:

cmd1 & cmd2 & cmd3 // run all commands from left to right (& => all)
cmd1 && cmd2 && cmd3 // run all commands from left to right, stop at 1st fail (&& => till fail)
cmd1 | cmd2 | cmd3 // run all commands from left to right, stop at 1st fail, also | is pipe which sends cmd1 output to cmd2 & so on, so use when if you want to pass outputs to other commands - (| => till fail + pass output of left to right)
cmd1 || cmd2 || cmd3 // run all commands from left to right, stop at 1st success (|| => till good)

Summary:

&  => run all  
&& => run L2R till fail  
|  => run L2R till fail + pass output of left to right  
|| => run L2R till good
   where, L2R is left to right

Hope that helped.

查看更多
再贱就再见
4楼-- · 2019-08-27 17:02

to run two commands use &. Both commands will be executed:

dir file.txt & echo done

Use && to execute the second command only if the first command was successful:

dir existentfile.txt && echo done

Use || to run the second command only if the first command failed:

dir nonexistentfile.txt || echo not found

You can combine:

dir questionablefile.txt && (echo file exists) || (echo file doesn't exist)
查看更多
登录 后发表回答