News for hackers. Stuff that matters.
Re:Shell Script Best Practices, from a decade of scripting t... by lbhdc on Thursday October 27, 2022 @10:58AM (#33358523)
> But on the other side of that coin its alot easier in python to do a complex regular expression

I am not sure I would agree. Sed fills this role quite nicely.

cat README.md | grep # | tr '[:lower:] [:upper:]' | sed 's/something/something_else/'

Re:Shell Script Best Practices, from a decade of scripting t... by usrbinbash on Thursday October 27, 2022 @11:32AM (#33359112)
Now do that again, but this time the regular expression is controlled by 2 command line params, one which gives it the substitution, the other one is a boolean switch that tells it whether to ignore case. And the script has to give a good error if the substitution isn't a valid regular expression. It should also give me a helptext for its command line options if I ask it with `-h, --h`.

In python I can use `opt/argparse`, and use the error output from `re.compile` to do this.

Of course this is also possible in bash, but how easy is it to code in comparison, and how maintainable is the result?

Re:Shell Script Best Practices, from a decade of scripting t... by lbhdc on Thursday October 27, 2022 @12:00PM (#33359555)
In the example I gave I wouldn't write that in a script file, so I would just alter the command itself.

If I wanted to parse cli args I would use case on the input to mux out the args. I personally prefer writing cli interfaces this way (when using a scripting language).

    while test $# -gt 0; do  
      case "$1" in  
        -f|--flag) shift; FLAG="$1";;  
      esac  
      shift  
    done
Re:Shell Script Best Practices, from a decade of scripting t... by scbrg on Thursday October 27, 2022 @01:09PM (#33360704)
Man, you chose the wrong username, didn't you? ;-)
Re:Shell Script Best Practices, from a decade of scripting t... by usrbinbash on Friday October 28, 2022 @02:49AM (#33368404)
Not really, I love bash. I also love perl and vimscript btw. :D
Re:Shell Script Best Practices, from a decade of scripting t... by asicsp on Thursday October 27, 2022 @11:15AM (#33358803)
grep+tr can be done within sed too (or go with perl for more features and easier portability)
Re:Shell Script Best Practices, from a decade of scripting t... by sargstuff on Friday October 28, 2022 @11:40AM (#33372838)
one tool/command per 'concept' was a resource saving thing at one time.

sed is the thing that handles shell regular expressions for shellscripts.