Java执行操作系统命令-Process抽象类(3)-cmd、bash

序章

在前面两篇随笔中,提到了添加下面的前缀才可以执行命令:

Windows:cmd /C

Linux:/bin/bash -c

本文介绍这两个命令。ben发布于博客园

 

Windows之cmd

cmd 即 cmd.exe。

查询其位置:where cmd

D:\>where cmd
C:\Windows\System32\cmd.exe

ben发布于博客园

查看 cmd 的详情:cmd /? 或者 help cmd

D:\>cmd /?
Starts a new instance of the Windows command interpreter
翻译:启动Windows命令解释器的一个新实例

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
        翻译:执行字符串指定的命令并终止。
/K      Carries out the command specified by string but remains
/S      Modifies the treatment of string after /C or /K (see below)
/Q      Turns echo off
/D      Disable execution of AutoRun commands from registry (see below)
/A      Causes the output of internal commands to a pipe or file to be ANSI
/U      Causes the output of internal commands to a pipe or file to be
        Unicode
...省略...
Note that multiple commands separated by the command separator '&&'
are accepted for string if surrounded by quotes.  
翻译:请注意,由命令分隔符“&&”分隔的多个命令如果被引号包围,则接受字符串。
Also, for compatibility
reasons, /X is the same as /E:ON, /Y is the same as /E:OFF and /R is the
same as /C.  Any other switches are ignored.
...省略...

 

cmd /C 示例

ben发布于博客园

Linux之bash

 前文介绍过,除了 bash,还有sh、dash。

三者的关系及区别见 参考资料#1ben发布于博客园

 

命令所在位置:使用 whereis 命令

ben@ben-u:~$ whereis bash
bash: /usr/bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

ben@ben-u:~$ whereis sh
sh: /usr/bin/sh /usr/share/man/man1/sh.1.gz

ben@ben-u:~$ whereis dash
dash: /usr/bin/dash /usr/share/man/man1/dash.1.gz

 

查看 bash 命令详情:1)--help;2)man bash

ben@ben-u:~$ bash --help
GNU bash, version 5.0.17(1)-release-(x86_64-pc-linux-gnu)
Usage:  bash [GNU long option] [option] ...
        bash [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --pretty-print
        --rcfile
        --restricted
        --verbose
        --version
Shell options:
        -ilrsD or -c command or -O shopt_option         (invocation only)
        -abefhkmnptuvxBCHP or -o option
Type `bash -c "help set"' for more information about shell options.
Type `bash -c help' for more information about shell builtin commands.
Use the `bashbug' command to report bugs.

bash home page: <http://www.gnu.org/software/bash>
General help using GNU software: <http://www.gnu.org/gethelp/>
ben@ben-u:~$

还可以输入 bash -c "help set" 查看选项信息;输入 bash -c help 查看更多信息(真多)。

 

使用 man 命令:

其中关于 -c 选项的解释:ben发布于博客园

 -c
 
If the -c option is present, then commands are read from the first non-option argument command_string.
 翻译:如果存在-c选项,则从第一个非选项参数命令_string中读取命令。
If there are arguments  after the  command_string, the first argument is assigned to $0 
 and any remaining arguments are assigned to the positional parameters.
 翻译:如果command_strint后面有参数,则将第一个参数分配给$0而任何剩下的参数都被分配给了位置参数。
The assignment to $0 sets the name of the shell, which is used in warning and error messages.
 翻译:分配为$0将设置shell的名称,用于发送警告和错误消息。

注,内容很多,孤未全阅。

 

bash -c 示例

从测试结果来看,在终端执行时,需要使用 && 替代 管道运算符ben发布于博客园

 

本文链接:ben发布于博客园

https://www.cnblogs.com/luo630/p/16996774.html

 

参考资料

1、linux中的sh、dash、bash的区别

https://www.cnblogs.com/macrored/p/11548347.html

posted @ 2019-09-19 12:03  Macrored

2、linux shell 执行多个命令的方法

https://www.cnblogs.com/youxin/p/3181120.html

2013-07-09 23:32  youxin

3、

 

ben发布于博客园

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章