如何使用前导井号(octothorpe)获取 bash 参数 [英] How to get bash arguments with leading pound-sign (octothorpe)

查看:37
本文介绍了如何使用前导井号(octothorpe)获取 bash 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理一个 bash 脚本的参数,该参数可能有也可能没有前导井号 (octothorpe).最简单的例子是:

I need to process an argument to a bash script that might or might not have a leading pound sign (octothorpe). The simplest example is:

echo #1234

什么都不返回

这可能是因为它将文本作为命令处理并假定它是注释.

It might be because it processes the text as a command and assumes it is a comment.

$@、$* 等不起作用.getopts 似乎没有解决这类问题.

$@, $*, etc. do not work. getopts does not seem to address this sort of thing.

欢迎提出建议

推荐答案

这完全不可能,因为有问题的参数"被解析为注释,根本没有传递给命令.

This is completely impossible, because the "argument" in question is parsed as a comment and never passed to the command at all.

请记住,C 中的程序对其 main 函数具有以下调用约定:

Keep in mind that programs in C have the following calling convention for their main function:

int main(int argc, char *argv[])

这意味着程序会被传递一个单独的、单独的参数列表,而不是一个尚未解析的字符串.解析该参数向量的原始字符串根本没有提供给被调用的程序;通常,甚至不存在原始字符串".因此,被调用的程序无法打开铃声"并从已解析的字符串列表返回到生成它的原始字符串.

This means that programs are passed a list of individual, separate arguments, not a single string that isn't yet parsed. The original string from which that vector of arguments was parsed is not given to the invoked program at all; often, no "original string" even exists. Consequently, a program that was invoked has no way to "unring the bell" and go back from the parsed list of strings to the original string from which it was generated.

因此,如果您的脚本作为外部命令(而不是 shell 函数)调用,操作系统运行它的 shell 的调用将通过 execve syscall,它的参数是 (1) 要执行的文件;(2) 传递它的参数向量(也就是说,前面提到的单个 C 字符串的列表);(3) 环境变量列表.未解析的 shell 命令行没有参数,因此子进程无法使用此类内容.

Consequently, if your script is invoked as an external command (as opposed to a shell function), the invocation of the shell that runs it by the operating system will go through the execve syscall, which takes as its arguments (1) the file to execute; (2) the argument vector to pass it (which is to say, the aforementioned list of individual C strings); and (3) a list of environment variables. There is no argument for an unparsed shell command line, so no such content is available to the subprocess.

培训您的用户使用适当的引用.就 yourscript 而言,以下所有内容都将具有完全无法区分的行为:

Train your users to use appropriate quoting. All of the below will have completely indistinguishable behavior, insofar as yourscript is concerned:

yourscript '#1234'  # single quotes prevent content from being parsed as shell syntax
yourscript ''#1234  # "#" only begins a comment at the front of a string
yourscript '#'1234  # note that shell quoting is character-by-character
yourscript #1234   # ...so either quoting or escaping only that single character suffices.

...以上任何一个都将传递一个 argv 包含(在 C 语法中)char[][]{ "yourscript", "#1234", NULL }

...any of the above will pass an argv containing (in C syntax) char[][]{ "yourscript", "#1234", NULL }

这篇关于如何使用前导井号(octothorpe)获取 bash 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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