0

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}
13
  • What shell are you using? Commented Feb 6, 2023 at 18:33
  • @konsolebox I use Ubuntu 20.4 default terminal. I also can't change it to bash because it gives me some other errors. Commented Feb 6, 2023 at 18:45
  • 1
    Note that using eval here is a bad idea: bash gives you tools like printf %q and ${var@Q} that can be used to generate eval-safe strings with untrusted data, whereas sh doesn't have the tools necessary to make eval safe unless the strings you're passing it contain only text that your script contains strict control over (no filenames, no user-provided arguments, etc). Commented Feb 6, 2023 at 18:50
  • 1
    And even when using eval is appropriate, eval "$params" is safer than eval $params; the latter subjects you to word-splitting, globbing, and then pasting of the results back into a single string before the parsing process starts, so it's an easy way for an unexpected glob character in your arguments to cause filenames (and any code they may contain) to be injected into the code that's then run through the parser. Commented Feb 6, 2023 at 18:51
  • 1
    % only removes suffixes, not arbitrary substrings. Commented Feb 6, 2023 at 18:57

1 Answer 1

2

Pipe to sed.

bash_params=$(echo "$bash_params" | sed "s/$tt//")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.