本文介绍了解析/将命令行参数传递给 bash 脚本 - “$@"之间有什么区别?和“$*"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 bash 脚本从任何位置调用和执行 .jar 文件,而无需不断输入其显式路径.

I am using a bash script to call and execute a .jar file from any location without having to constantly enter its explicit path.

.jar 需要在执行时指定额外的变量参数,因为这些参数可以是任何东西,它们不能硬编码到脚本中.

The .jar requires additional variable parameters to be specified at execution, and as these can be anything, they cannot be hard coded into the script.

总共有 3 个变量,第一个指定 .jar 要执行的 2 个动作中的一个,第二个指定执行此动作的目标文件,第三个指定名称操作要创建的文件.

There are 3 variables in total, the first specifies 1 of 2 actions that the .jar is to make, the second specifies a target file to enact this action on and the third specifies the name of a file that the action is to create.

我目前使用的脚本是:

#!/bin/bash
java -jar "C:path	ofile.jar" "$1" "$2" "$3"

我对 bash 脚本知之甚少,但在寻找解决我的问题的另一个答案(现已修复)时,我遇到了 "$@""$*"引用命令行参数时.做更多的搜索把我带到了这个网站:How To Wiki: How to read command line arguments in a bash script,但是我无法找到任何关于这些论点的可靠信息,而不必费力地阅读大量超出我想象的高级 bash 编程.

I know very little about bash scripting, but while searching for another answer to my woes (now fixed) I came across "$@" and "$*" when referencing command line arguments. Doing more searching brought me to this site: How To Wiki: How to read command line arguments in a bash script, but I can't find any solid information about those arguments without having to wade through tons of advanced bash programming that is way above my head.

既然我一直在胡说八道,我的问题就比较简单了:

So now that I have rambled on forever, my question is relatively simple:

我能否将 "$1" "$2" "$3" 替换为 "$@""$*",如果可以哪个更好用?

Can I replace "$1" "$2" "$3" with "$@" or "$*", and if so which is the better one to use?

如果有的话,这些命令之间的区别是什么?

Also what, if any, is the difference between those commands?

推荐答案

"$@" 对应 "$1" "$2" "$3">"$*" 对应于您似乎不需要的 "$1 $2 $3".

"$@" corresponds to "$1" "$2" "$3" etc. "$*" corresponds to "$1 $2 $3" which you do not seem to need.

不带引号没有区别,都对应$1 $2 $3

Without quotes, there is no difference, they both correspond to $1 $2 $3 etc.

这篇关于解析/将命令行参数传递给 bash 脚本 - “$@"之间有什么区别?和“$*"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 10:21