传递在多个Shell脚本中存在的命名参数/参数,最终传递给Python脚本。

huangapple go评论70阅读模式
英文:

Passing naming arguments/parameters between shell functions (existing in multiple shell scripts) and finally to python script

问题

我有一个期望接受1、2或3个参数的Python脚本:

--name - 必需
--max  - 可选
--min  - 可选

我有一个在Bash脚本中调用Python脚本的函数:

script_1
```shell
filter() {
  .../python3 -E filtered_as.pyc --name $1 --min $2 --max $3

其他Bash脚本可以调用filter函数:

script_2

filter NAME_DEW 42 | while read id; do
.............

我的需求是:

  • 不再传递$1和$2,而是传递命名参数,与Python脚本中的期望相同(从一个Shell脚本到另一个Shell脚本,从Shell脚本到Python)。
  • 处理可选参数:
    • 可以是全部3个
    • 可以仅是name
    • 可以是name和min或max(2个)

此外,因为我有2个可选参数,我需要命名参数来知道哪个已传递。


<details>
<summary>英文:</summary>

I have a python script that is expecting 1, 2 or 3 arguments:


--name - mandatory
--max - optional
--min - optional

I have a function in bash script that calls the python script:

script_1

filter() {
.../python3 -E filtered_as.pyc --name $1 --min $2 --max $3


Other bash scripts can call the filter function:

script_2

filter NAME_DEW 42 | while read id; do
.............


What, I want:

- instead of passing $1, $2 I want to pass named arguments, the same as expected in python script (from the shell script to the other shell script, from shell script to python.
- work with optional arguments:
  - can be all 3
  - can be just name
  - can be name and min or max (2) 

Also because I have 2 optional arguments, I need named arguments to know which one is passed.

</details>


# 答案1
**得分**: 1

这个回答对你的问题进行了很多猜测,因为问题不够详细,否则无法回答。

假设你想知道如何使用命名前缀来控制从标准输入获取的参数是 `min` 还是 `max`,一种方法可能如下所示:

```bash
filter() {
  local name min= max=
  name=$1; shift || return
  while (( $# )); do
    case $1 in
      min:*) min=${1#min:};;
      max:*) max=${1#max:};;
      *)     echo "错误:参数 $1 应该以 min: 或 max: 开头" >&2;;
    esac
    shift
  done
  python3 -E filtered_as.pyc \
    --name "$name" \
    ${min:+ --min "$min"} \
    ${max:+ --max "$max"}
}

然后可以这样调用:

filter NAME_DEW min:42 | while read id; do ...
英文:

This answer is making a lot of guesses at what you mean by your question, because that question isn't detailed enough to answer otherwise.

Assuming you're asking how to use named prefixes to control whether arguments taken from stdin are min or max, ore way to do that might look like:

filter() {
  local name min= max=
  name=$1; shift || return
  while (( $# )); do
    case $1 in
      min:*) min=${1#min:};;
      max:*) max=${1#max:};;
      *)     echo &quot;ERROR: Argument $1 should start with min: or max:&quot; &gt;&amp;2;;
    esac
    shift
  done
  python3 -E filtered_as.pyc \
    --name &quot;$name&quot; \
    ${min:+ --min &quot;$min&quot;} \
    ${max:+ --max &quot;$max&quot;}
}

thereafter called as:

filter NAME_DEW min:42 | while read id; do ...

huangapple
  • 本文由 发表于 2023年6月29日 00:10:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定