argv的:消毒通配符 [英] argv: Sanitizing wildcards

查看:112
本文介绍了argv的:消毒通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在K&放一个例子; R C本书,它要求你基本上是建立一个RPN计算器,通过命令行参数获取输入。我的解决方案通过给定的参数基本上是迭代并吐出答案,但我注意到的东西:

I was working on an example in the K&R C book where it asks you to essentially build an RPN calculator that takes input through command line arguments. My solution essentially iterates through the given arguments and spits out the answer, but I noticed something:

如果我要奉献乘法字符(星号)'*'无单引号,GCC假设是一个通配符输入,所以我输入

If I were to give the multiplication character (an asterisk) '*' without single quotes, gcc assumes that to be a wildcard input, so my input of

$./rpn 5 10 *

给我的输出

read 5
read 10
read rpn
read rpn.c
= 0

结束语用单引号补救星号的问题。

Wrapping the asterisk with single quotes remedies the issue

$./rpn 5 10 '*'
read 5
read 10
read *
= 50

我的问题是会有净化输入让自己的程序不需要星号的方式被包裹在单引号,或者是这种行为所造成的一些更基本的(如Linux的/ POSIX / UNIX的二进制执行和论证处理)?

My question is would there be a way to sanitize input so that my program does not require the asterisk to be wrapped in single quotes, or is this behavior caused by something more fundamental (e.g. Linux/POSIX/UNIX binary execution and argument handling)?

推荐答案

外壳在执行程序之前扩大了水珠。你引用的水珠不是GCC的,而是因为外壳。如果你不希望这种行为,然后使用不兑现水珠壳。

The shell is expanding the glob before executing the program. You quote the glob not because of GCC, but because of the shell. If you don't want this behavior then use a shell that does not honor globs.

这篇关于argv的:消毒通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆