如何在 Linux 中使用终端命令将文件参数传递给我的 bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3534280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:23:10  来源:igfitidea点击:

How can I pass a file argument to my bash script using a Terminal command in Linux?

linuxbashshellcommand-lineterminal

提问by AZorin

So my question is how can I pass a file argument to my bash script using a Terminal command in Linux? At the moment I'm trying to make a program in bash that can take a file argument from the Terminal and use it as a variable in my program. For example I run myprogram --file=/path/to/filein Terminal.

所以我的问题是如何在 Linux 中使用终端命令将文件参数传递给我的 bash 脚本?目前,我正在尝试在 bash 中创建一个程序,该程序可以从终端获取文件参数并将其用作程序中的变量。例如我myprogram --file=/path/to/file在终端运行 。

My Program

我的程序

#!/bin/bash    
File=(the path from the argument)  
externalprogram $File (other parameters)

How can I achieve this with my program?

我怎样才能通过我的程序实现这一目标?

回答by David Z

It'll be easier (and more "proper", see below) if you just run your script as

如果您只是将脚本运行为

myprogram /path/to/file

Then you can access the path within the script as $1(for argument #1, similarly $2is argument #2, etc.)

然后你可以访问脚本中的路径$1(对于参数 #1,同样$2是参数 #2,等等)

file=""
externalprogram "$file" [other parameters]

Or just

要不就

externalprogram "" [otherparameters]

If you want to extract the path from something like --file=/path/to/file, that's usually done with the getoptsshell function. But that's more complicated than just referencing $1, and besides, switches like --file=are intended to be optional. I'm guessing your script requiresa file name to be provided, so it doesn't make sense to pass it in an option.

如果你想从类似的东西中提取路径--file=/path/to/file,那通常是用getoptsshell 函数来完成的。但这比引用更复杂$1,此外,像这样--file=的开关是可选的。我猜你的脚本需要提供一个文件名,所以在option 中传递它是没有意义的。

回答by Dan Moulding

Bash supports a concept called "Positional Parameters". These positional parameters represent arguments that are specified on the command line when a Bash script is invoked.

Bash 支持一种称为“位置参数”的概念。这些位置参数表示调用 Bash 脚本时在命令行上指定的参数。

Positional parameters are referred to by the names $0, $1, $2... and so on. $0is the name of the script itself, $1is the first argument to the script, $2the second, etc. $*represents all of the positional parameters, except for $0(i.e. starting with $1).

位置参数由名称$0$1$2... 等引用。$0是脚本本身的名称,是脚本$1的第一个参数,$2第二个等$*表示所有位置参数,除了$0(即以 开头$1)。

An example:

一个例子:

#!/bin/bash
FILE=""
externalprogram "$FILE" <other-parameters>

回答by Cascabel

Assuming you do as David Zaslavsky suggests, so that the first argument simply isthe program to run (no option-parsing required), you're dealing with the question of how to pass arguments 2 and on to your external program. Here's a convenient way:

假设你做大卫Zaslavsky建议,从而使第一个参数仅仅运行(分析选项,不需要),你处理的是如何来传递参数2关于这个问题到您的外部程序的程序。这里有一个方便的方法:

#!/bin/bash
ext_program=""
shift
"$ext_program" "$@"

The shiftwill remove the first argument, renaming the rest ($2becomes $1, and so on).$@` refers to the arguments, as an array of words (it must be quoted!).

shift将删除的第一个参数,重命名其余的($2成为$1, and so on).$ @`指参数,如字(它必须被引用的一个数组中!)。

If you must have your --filesyntax (for example, if there's a default program to run, so the user doesn't necessarily have to supply one), just replace ext_program="$1"with whatever parsing of $1you need to do, perhaps using getopt or getopts.

如果你必须有你的--file语法(例如,如果有一个默认程序要运行,所以用户不必提供一个),只需用你需要做的ext_program="$1"任何解析替换$1,也许使用 getopt 或 getopts。

If you want to roll your own, for just the one specific case, you could do something like this:

如果您想推出自己的产品,仅针对一种特定情况,您可以执行以下操作:

if [ "$#" -gt 0 -a "${1:0:6}" == "--file" ]; then
    ext_program="${1:7}"
else
    ext_program="default program"
fi

回答by lesmana

you can use getopt to handle parameters in your bash script. there are not many explanations for getopt out there. here is an example:

您可以使用 getopt 来处理 bash 脚本中的参数。关于 getopt 的解释并不多。这是一个例子:

#!/bin/sh

OPTIONS=$(getopt -o hf:gb -l help,file:,foo,bar -- "$@")

if [ $? -ne 0 ]; then
  echo "getopt error"
  exit 1
fi

eval set -- $OPTIONS

while true; do
  case "" in
    -h|--help) HELP=1 ;;
    -f|--file) FILE="" ; shift ;;
    -g|--foo)  FOO=1 ;;
    -b|--bar)  BAR=1 ;;
    --)        shift ; break ;;
    *)         echo "unknown option: " ; exit 1 ;;
  esac
  shift
done

if [ $# -ne 0 ]; then
  echo "unknown option(s): $@"
  exit 1
fi

echo "help: $HELP"
echo "file: $FILE"
echo "foo: $FOO"
echo "bar: $BAR"

see also:

也可以看看: