简体   繁体   English

如何将参数传递给Shell脚本

[英]How to pass an argument to shell script

I'm kinda new in bash, the thing is I need to run several set of data in a program, I make a bash script that allow me to do it automatically... the problem is when the program start it needs some inputs and I don't know how to give it inside the bash? 我对bash有点陌生,问题是我需要在一个程序中运行几组数据,我制作了一个bash脚本,该脚本可以自动执行...问题是程序启动时需要一些输入,我不知道如何在bash中使用它? I execute the program: 我执行程序:

$./bin/mg5_aMC

The program opens and I need to pass some input to run (mg5> is the enviroment of the program): 程序打开,我需要传递一些输入才能运行(mg5>是程序的环境):

mg5>launch file.lhe

the program runs, and ask for something: 该程序运行,并要求:

mg5> 1

and then again, ask for something and i need to press enter.. 然后再问一问,我需要按Enter。

mg5> (enter)

PD.: I edited the question because I suppose didn't express myself very well.. PD .:我编辑了问题,因为我想自己表现得不太好。

如果程序可以将其输入作为从命令行传递的参数来获取,则可以将参数写入文件中,将其命名为params ,然后执行

./bin/mg5_aMC $(<params)

From what I see, you rather want to catch user's input, than simply pass args to executed script. 从我的角度来看,您宁愿捕获用户的输入,而不是简单地将args传递给执行的脚本。 read command allows you to catch user's input. read命令允许您捕获用户的输入。 Did it answers your question? 它回答了您的问题吗?

From what I understood from your question, you either want 1) to pass parameters when calling your Bash script, or 2) some sort of interactive shell within your script. 据我对您的问题的理解,您要么希望1)在调用Bash脚本时传递参数,要么2)在脚本中使用某种交互式shell。

For #1, passing parameters to a Bash script, these links may be helpful: 对于#1,将参数传递到Bash脚本,这些链接可能会有所帮助:

Basically, you need to use the following call for your script: 基本上,您需要对脚本使用以下调用:

./bin/mg5_aMC launch xxx.lhe 1

And within your script, you refer to each argument using a numbered variable: 在脚本中,您使用数字变量引用每个参数:

action=$1
file=$2
n=$3

As for #2, consult the following links/examples: 至于#2,请参考以下链接/示例:

There are quite a few resources online and on this site that will provide you more example and explanations. 此站点上有很多在线资源,这些资源将为您提供更多示例和解释。

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

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