我的程序需要接受以下三种输入命令:
./Myprogram input.txt
./Myprogram < input.txt
./Myprogram
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用argc检查解决前两种情况的参数数量(因为重定向不算作参数).但后来我坚持最后一个案例,它只是等待用户输入.
我想知道是否有办法判断shell命令中是否存在重定向?
对于更复杂的场景,例如重定向和argv形式的混合(见下文).有没有办法做到这一点,或者它只是一个糟糕的设计用于获取用户命令?
./Myprogram input1.txt input2.txt input3.txt
./Myprogram input1.txt < input2.txt input3.txt
./Myprogram
Run Code Online (Sandbox Code Playgroud)
任何帮助都感激不尽!
Z.Zen
您的程序永远不会将重定向视为参数.所以在:
./Myprogram input.txt
./Myprogram < input.txt
./Myprogram
Run Code Online (Sandbox Code Playgroud)
第二种和第三种形式是相同的.至于你的第二套可能性:
./Myprogram input1.txt input2.txt input3.txt
./Myprogram input1.txt < input2.txt input3.txt
./Myprogram
Run Code Online (Sandbox Code Playgroud)
第二行相当于:
./Myprogram input1.txt input3.txt < input2.txt
Run Code Online (Sandbox Code Playgroud)
它也与以下方面无法区分:
./Myprogram input1.txt input3.txt
Run Code Online (Sandbox Code Playgroud)
(标准输入实际来自的唯一不同之处).
一些程序处理来自stdin和命令行中指定的文件的混合输入的典型方式是接受"-"一个特殊的文件名,意思是"在参数列表中的这个位置使用stdin作为输入文件"."-"如果参数列表为空,许多此类程序将默认处理单个列表.
| 归档时间: |
|
| 查看次数: |
2060 次 |
| 最近记录: |