0

in order to parse the arguments list I tried to implement a common top-down parsing scheme.

I wondered why my eat() function wouldn't work. Because the positional arguments $1, $2, $3, ... are in the context of that particular function. Not the ones of the actual script. Bummer. I'm didn't yet grow accustomed to bash.

So my question is, if it is therefore correct, that also the scope of shift is the context of my eat() function, yes?

Is there really no way of shifting the script arguments from inside another function? : (

[That means I have to implement ALL logic (that normally adheres to a top down parsers') in a case statement ! (!?)]

von spotz
  • 405
  • 3
  • 12
  • 1
    Does this answer your question? [Use of positional parameters inside function defintion](https://unix.stackexchange.com/questions/429211/use-of-positional-parameters-inside-function-defintion), but don't rely on the accepted answer there, see other answers. – αғsнιη Jun 03 '21 at 06:37
  • I lack any form of code in this question. Why would you try to implement your own argument parsing when there is `getopts`? There is no indication as to how you call your script or your `eat` function within it, nor what the script or function needs in terms of parameters, or what their function is. – Kusalananda Jun 03 '21 at 07:13
  • 1
    @Kusalananda i think what he's trying to do is shove all the option processing into its own function, whether done manually or with getopts. Which is a perfectly reasonable thing to want to do, according to structured programming principles. The problem is that those principles are best suited to general purpose programming languages, and sh, bash, ksh, zsh, etc are definitely not one of those (no matter how they pretend otherwise these days) - shells are specialised tools for coordinating the execution of other programs and redirecting input and output. – cas Jun 03 '21 at 07:32
  • I think I found a workaround. I just make a copy of the script arguments and let my `eat` function work on that, to set my `shove all the option processing into its own function` scheme into motion. :) – von spotz Jun 03 '21 at 07:54
  • @vonspotz This is more or less (or very close to) what [cas' answer](https://unix.stackexchange.com/a/652579/116858) does. You may want to accept that answer. – Kusalananda Jun 03 '21 at 07:58
  • 1
    related: [how to shift array value in bash](https://unix.stackexchange.com/questions/413976/how-to-shift-array-value-in-bash) – ilkkachu Jun 03 '21 at 08:36
  • @ilkkachu Thanks – von spotz Jun 03 '21 at 11:03
  • Does this answer your question? [how to shift array value in bash](https://unix.stackexchange.com/questions/413976/how-to-shift-array-value-in-bash) – AdminBee Jun 09 '21 at 08:42

1 Answers1

3

A function can return all (or a subset) of the arguments it was passed (e.g. via a global array), and the calling script can then use that to set its own args. e.g.

#!/bin/bash

declare -a args

nofoo() {
  # we don't like "--foo", it is forbidden!
  for a in "$@"; do
    [ "$a" != "--foo" ] && args+=("$a")
  done
}

nofoo "$@"
set -- "${args[@]}"

From help set in bash:

set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

Set or unset values of shell options and positional parameters.

Change the value of shell attributes and positional parameters, or display the names and values of shell variables.

[...]

-- Assign any remaining arguments to the positional parameters. If there are no remaining arguments, the positional parameters are unset.

cas
  • 1
  • 7
  • 119
  • 185
  • 1
    This would unfortunately break on arguments containing characters in `$IFS` (space, tab, newline), as well as arguments containing filename globbing characters. – Kusalananda Jun 03 '21 at 07:05
  • @Kusalananda In that case use a global array instead of trying to return the arg subset with printf. This was only meant to be a very simple and silly example to demonstrate in principle how to modify an argument list from within a function, it was not meant to be perfect. – cas Jun 03 '21 at 07:21
  • It looks better now. – Kusalananda Jun 03 '21 at 07:29