how to ignore specific argument in bash?

17 views
Skip to first unread message

andrew mcelroy

unread,
Sep 21, 2014, 5:06:37 PM9/21/14
to nlug...@googlegroups.com
Greetings all,

This is probably a dumb questions, but I am writing a basic shell
script that is passing $@ to a command. The issue is I'd like to
filter $@ so that it does not pass a specific flag ( in this case
--xcode-dir) ? How can I drop this in a bash script?

Andrew McElroy

Jack Coats

unread,
Sep 21, 2014, 5:43:16 PM9/21/14
to NLUG
in other languages using substring and index string functions would
make it easy, but bash has few unified shell functions.

csh or ksh might be a better option, but it can be done in bash or sh
with a lot of effort.
> --
> --
> You received this message because you are subscribed to the Google Groups "NLUG" group.
> To post to this group, send email to nlug...@googlegroups.com
> To unsubscribe from this group, send email to nlug-talk+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/nlug-talk?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups "NLUG" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nlug-talk+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
><> ... Jack

"Whatever you do, work at it with all your heart"... Colossians 3:23
"If you are not part of the solution, you are part of the precipitate"
- Henry J. Tillman
"Anyone who has never made a mistake, has never tried anything new." -
Albert Einstein
"You don't manage people; you manage things. You lead people." -
Admiral Grace Hopper, USN
"a nanosecond is the time it takes electrons to propigate 11.8 inches"
- " - http://youtu.be/JEpsKnWZrJ8
"Life is complex: it has a real part and an imaginary part." - Martin Terma

Greg Donald

unread,
Sep 21, 2014, 6:15:06 PM9/21/14
to nlug...@googlegroups.com
On Sun, Sep 21, 2014 at 4:06 PM, andrew mcelroy <soph...@gmail.com> wrote:
Assuming you want to drop the argument (value) after --xcode-dir as well:

#!/usr/bin/env bash

args=("$@")
myargs=()
nextarg=-1

for ((i=0; i<$#; i++)) {
if [ $nextarg == $i ]; then continue; fi
case ${args[$i]} in
--xcode-dir) nextarg=$((i+1)) ;;
*) myargs+="${args[$i]} "
esac
}

echo $myargs

./remove_xcode --xcode-dir foo --bar baz
--bar baz


--
Greg Donald

andrew mcelroy

unread,
Sep 21, 2014, 8:21:28 PM9/21/14
to nlug...@googlegroups.com
On Sun, Sep 21, 2014 at 5:14 PM, Greg Donald <gdo...@gmail.com> wrote:
> On Sun, Sep 21, 2014 at 4:06 PM, andrew mcelroy <soph...@gmail.com> wrote:
>> Greetings all,
>>
>> This is probably a dumb questions, but I am writing a basic shell
>> script that is passing $@ to a command. The issue is I'd like to
>> filter $@ so that it does not pass a specific flag ( in this case
>> --xcode-dir) ? How can I drop this in a bash script?
>
> Assuming you want to drop the argument (value) after --xcode-dir as well:
>

This is what I was looking for.
I ended up going with $1 through $7 (since it is going to be the same
input and is automated).

Thanks Greg.
This should be October's "Asleep At The Prompt".

> #!/usr/bin/env bash
>
> args=("$@")
> myargs=()
> nextarg=-1
>
> for ((i=0; i<$#; i++)) {
> if [ $nextarg == $i ]; then continue; fi
> case ${args[$i]} in
> --xcode-dir) nextarg=$((i+1)) ;;
> *) myargs+="${args[$i]} "
> esac
> }
>
> echo $myargs
>
> ./remove_xcode --xcode-dir foo --bar baz
> --bar baz
>
>
> --
> Greg Donald
>

John F. Eldredge

unread,
Sep 22, 2014, 8:45:30 AM9/22/14
to nlug...@googlegroups.com
I would suggest placing comments, in both the calling script and called
script, that changes to the number of arguments will require editing both
scripts. Otherwise, it would be be easy to overlook the interdependence
during some future edit.
Reply all
Reply to author
Forward
0 new messages