在SH而不是bash中如何从字符串中删除子字符串:

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

How to remove a substring from string in SH not bash

问题

以下是翻译好的部分:

我想要修改一个以 #!/bin/sh 开始的 SH 脚本的输入参数。我找到了一些解决方案,但它们在这里不起作用,需要使用 `bash`。它们会报错 `bad substitution`。因此,我正在寻找一个在 SH(或它叫什么都可以)中有效的解决方案。

`bash_params` 可以是类似于 "_learn _vil=bar _meet=foo" 的内容。这里 "_learn" 充当一个标志。我想要根据此标志设置一些变量,然后将其删除,以便我可以使用 `eval` 设置其他变量。

另外,如果您知道更好的方法,请告诉我。

case $bash_params in
  *" _learn"*) # 启用 learn
      _learn_sp=True
      tt="_learn"
      bash_params="${bash_params%"$tt"}" # 这不起作用
      bash_params="${bash_params/_learn/}" # 这会报错 Bad substitution

      _lsp=False
    ;;

eval ${bash_params}

希望这对您有所帮助。

英文:

I would like to modify input parameters of an SH script (it begins with #!/bin/sh). I found some solutions but they don't work here and need bash. They give me bad substitution error. so I look for a solution that works in SH (or whatever it is called)

The bash_params could be like "_learn _vil=bar _meet=foo". Here "_learn" acts as a flag. I want to set some variabels based on this flag and then remove it so that I can set other variables with eval.

Also, if you know better approaches please let me know

case $bash_params in
  *"_learn"*) # learn is enabled
      _learn_sp=True
      tt="_learn"
      bash_params="${bash_params%"$tt"}" # it doesn't work
      bash_params="${bash_params/_learn//}" # this gives Bad substitution error

      _lsp=False
    ;;

eval ${bash_params}

答案1

得分: 2

bash_params=$(echo "$bash_params" | sed "s/$tt//")
英文:

Pipe to sed.

bash_params=$(echo "$bash_params" | sed "s/$tt//")

huangapple
  • 本文由 发表于 2023年2月7日 02:24:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365199.html
匿名

发表评论

匿名网友

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

确定