Linux:如何使用脚本委派异常的命令行参数? [英] Linux: How do I delegate exotic commandline arguments using a script?
问题描述
我想写一个包装器bash脚本,并将所有参数传递给一个被调用的程序。我非常确定,这个工作正确:
#!/ bin / sh
someProgam $ @
但是当传递异常参数(空,未转义,引号,...)
例如:没有包装器脚本, someProgram1 23
会导致参数
[1 2]
和 [3]
。
但是从脚本调用, code> [1] , [2]
, [3]
。
大括号只是用于显示。
Java程序,它被调用。
#!/ bin / sh
someProgram$ @
另请参阅 bash docs on special parameters 。
BTW1,$ @
不特定于bash。你可以依靠$ @
在跨平台 sh
脚本中运行几乎任何地方。 p>
BTW2,如果这恰好是该脚本中的最后一行,您可以通过将行更改为某个值来将操作系统保存为几个字节和一个条目像
exec someProgram$ @
I want to write a wrapper bash script, and to pass all arguments to a called program. I was very sure, that this works correctly:
#!/bin/sh
someProgam $@
But when passing exotic arguments (empty, unescaped, in quotes, ...) this fails.
For example: without the wrapper script, someProgram "1 2" 3
results in the arguments
[1 2]
and [3]
.
But called from the script, I get [1]
, [2]
, [3]
.
Braces are just for visualization.
NOTE: It's a Java program, which is called. But I think it doesn't matter.
#!/bin/sh
someProgram "$@"
See also the bash docs on special parameters.
BTW1, "$@"
is not specific to bash. You can probably rely on "$@"
in cross-platform sh
scripts to be run just about anywhere.
BTW2, in case this happens to be the last line in that script, you can save your operating system a few bytes and an entry in the process table by changing the line to something like
exec someProgram "$@"
这篇关于Linux:如何使用脚本委派异常的命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!