简体   繁体   English

将*作为命令行参数传递

[英]Passing * as a command line argument

I wrote a C program to evaluate reverse polish notation by passing the expression as a command line argument, but when I pass * (for multiplication) then it is passing all the file names in that folder. 我编写了一个C程序,通过将表达式作为命令行参数传递来评估反向修饰符号,但是当我传递*(用于乘法)时,它将传递该文件夹中的所有文件名。

For example I passed this : 例如我通过了:

./rpn 10 20 30 + * ./rpn 10 20 30 + *

and when I print all the arguments result is, 当我打印所有参数时,

 10
 20
 30
 +
 gcd
 gcd.c
 gcd.c~
 rpn
 rpn.c
 rpn.c~
 swapmacro
 swapmacro.c argc :12

This is not a C problem. 这不是C问题。 You're using Bash (or some equivalent shell), where * is automatically expanded (before it gets anywhere near your program). 您正在使用Bash(或某些等效的Shell),其中*会自动展开* (在到达程序附近的任何位置之前)。 You'll need to do something like this: 您需要执行以下操作:

./rpn 10 20 30 + "*"

You need to escape the * eg by quoting it like "*" or by escaping it like \\* 您需要对*进行转义,例如通过像"*"一样将其引号或通过像\\*那样转义它

The expansion of * is done by the shell (before starting your program). *的扩展由外壳程序完成(在启动程序之前)。 Read eg the Advanced Bash Scripting guide . 阅读例如《 高级Bash脚本指南》

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM